- Create a solution with Visual Studio 2017 and add a new .NET Class Library project.
- Select Windows Classic Desktop, Class Library
- Specify a .NET Framework version (for ex, 4.7)
- Add the EF6 NuGet package.
- Add a
Product
entity class to a Models folder. - Add a
SampleDbContext
to a Contexts folder and inherit fromDbContext
.- Add a
Products
property of typeDbSet<Product>
- Add a SampleDbContextFactory class to the Contexts folder.
- Implement IDbContextFactory
- Return a new SampleDbContext in the Create method
- Specify a connection string for the SampleDb database
- In Package Manager Console select the class library project
- Run the following command:
Enable-Migrations
- Add code to the
Seed
method in theConfiguration
class in the Migrations folder - Run the
Add-Migration Initial
command - Run the
Update-Database
command - Add a data connection to the Server Explorer
- Verify that the SampleDb database exists and that the Products table contains data
- Add a new Web project to the solution.
- Select .NET Core, ASP.NET Core Web Application
- Select Web API targeting the full .NET Framework
- Set the web project as the startup project for the solution
- Add the EF6 Nuget package to the web project
- Add a project reference to the Data class library project
- Build the solution.
- Open appsettings.json in the Web project and add the connection string
- In the Web project register the context for DI in Startup.cs.
- Add following code after
services.AddMvc()
in theConfigureServices
method
- Add a
ProductsController
that extendsController
- Pass
SampleDbContext
to the ctor - Add actions for GET, POST, PUT and DELETE
- Test the controller by running the app and submitting some requests.
- Use Postman or Fiddler
- Set Content-Type header to application/json for POST and PUT.
- The database should be created automatically
So far, we have created one Asp.Net Core MVC application and installed some required Entity Framework Core packages which are required. ASP.NET Core with Entity Framework Core – ASPNETCOREENVIRONMENT. See my other ASP.NET Core articles: How to deploy an ASP.NET Core 1.1 application to an Azure App Services Web App using Visual Studio 2017; ASP.NET Core (-4077,–4047,–4089, 4095).
- ASP.NET Core Tutorial
- ASP.NET Core Useful Resources
- Selected Reading
In this chapter, we will set up and configure our application to save and read data from a SQL Server database.
To work with a database, we are going to use the Entity Framework, which is freshly rewritten to work with the new .NET Framework. If you have worked with EF in the past, you will see many familiar pieces.
- In this application, we will use the SQL Server LocalDB. If you are not comfortable with the SQL Server, you can use any database that you like such as local database, remote database, as long as you have permission to create a new database on the instance.
- LocalDB is a special edition of SQL Server that is optimized for developers.
- Visual Studio 2015 and even its Community edition will install LocalDB by default.
To check the LocalDB, go to the View → SQL Server Object Explorer menu option in Visual Studio.
This is a great tool if you have to work with SQL Server because it allows you to explore databases and browse data and even create data inside a database. When you first open it, it might take a little time, but it should connect to the LocalDB automatically.
Install Entity Framework
The first step in using the Entity Framework is to install the Entity Framework NuGet package from the NuGet package manager or by editing the project.json file directly.
Let us now edit the project.json file directly by adding the following two packages.
The EntityFramework.Commands package helps us perform tasks with the Entity Framework like creating a database schema based on our C# Entity classes and these tasks are available from a command line tool where the logic lives inside the EntityFramework.Commands package.
In order to use this command line tool, we need to make an additional entry into the commands section of project.json as shown in the following screenshot.
We have just called it “ef” and that will map to this EntityFramework.Commands package. We can use this “ef” to get access to some of the logic that is available inside EntityFramework.Commands.
![Asp Net Core Entity Framework Asp Net Core Entity Framework](/uploads/1/2/6/3/126361562/625791340.png)
The following is the implementation of the project.json file.