I. Creating a new MVC project for. NET Core
After you create a new project, you cannot add EF directly to the new item as you did before.
Need to use commands to add dependency on EF
Two. Core installation of EF core Entity Framework:
Tools > NuGet Package Manager > Package Manager console
Install-package Microsoft.EntityFrameworkCore.SqlServer
Install-package Microsoft.EntityFrameworkCore.Tools
Install-package Microsoft.VisualStudio.Web.CodeGeneration.Design
Once the installation is successful, you can see it in the NuGet dependencies
four. More than one command to generate model from the database
Mode one: (Create a model from an existing database)
Scaffold-dbcontext "server=.;D Atabase=food; Trusted_connection=true; " Microsoft.entityframeworkcore.sqlserver-outputdir Models
The command generates tables and context objects in the database under the Models folder
Note: There is a problem when performing this step because the system is Win7,powershell version is too low, does not support this command, need to install
More than 3.0 of PowerShell versions are OK
After adding success, you can see in models that the context object is generated and the model corresponding to the table
Now it's time to use EF.
Public Iactionresult Index () { Foodcontext fc = new Foodcontext (); List<protype> ptlist = FC. Protype.tolist (); Viewbag.ptlist = ptlist; return View (); }
mode two: (Create a database from a model)
1. Create a Context class
public class foodcontext:dbcontext{public foodcontext (dbcontextoptions<foodcontext> options) : Base ( Options) {} public dbset<blog> Blogs {get; set;} Public dbset<post> Posts {get; set;}} public class blog{Public int BlogId {get; set;} public string Url {get; set;} Public list< Post > Posts {get; set;}} public class post{Public int PostID {get; set;} public string Title {get; set;} public string Content {get; set;} public int BlogId {get; set;} Public Blog Blog {get; set;}}
2. Register the context class as a global service in the Configureservices method of Startup.cs:
Services. Adddbcontext (options = options. Usesqlserver (connection));
3. Add the connection string to the AppSetting file connection
4. Create a Database
Tools-> NuGet Package Manager-> Package Manager console
Create an initial table of the model
Add-migration initialcreate
Apply a new migration to a database
Update-database
Five. Using dependency injection to load the EF context object
There is a lot of dependency injection in. NET core, which is also recommended in official documentation.
1: Delete method
protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) { //#warning to protect Potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings. Optionsbuilder.usesqlserver (@ "server=.;D Atabase=food; Trusted_connection=true; "); }
2: Add method
Public Foodcontext (dbcontextoptions<foodcontext> options) : Base (Options) { }
A constructor is added to inject the
3: Add dependency Injection in Startup.cs's Configureservices method
public void Configureservices (iservicecollection services) { //ADD framework services. Services. Addmvc (); Services. adddbcontext<foodcontext> (option = { option. Usesqlserver ("Data Source =.; Initial Catalog = Efcore_dbfirst; User ID = sa; Password = sa.123 "); });
Official Microsoft Documentation:
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
EF Core installation, EF core and database integration