EF Core installation, EF core and database integration

Source: Internet
Author: User

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:

    1. Tools > NuGet Package Manager > Package Manager console

    2. Install-package Microsoft.EntityFrameworkCore.SqlServer

    3. Install-package Microsoft.EntityFrameworkCore.Tools

    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.