Before learning EFCore, are created in the VS default template, according to the official document, can be directly done.

The new project is ready today. Net Core is going to layer the data access layer with EFCore in accordance with international convention, so we put this layer into a class library

1. Add a reference

The second and third libraries must be added, except if you don’t command migration…

2. Open the package management console

Add-Migration intidb
Copy the code

Then you’ll notice that you got an error

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

The database connection cannot be found, because normally the database connection will be read in the Startup ConfigureServices.

To find out why, rewrite DbContext’s OnConfiguring method

 

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer( "Data Source=.; Initial Catalog=dbname; Integrated Security=False; Persist Security Info=False; User ID=sa; Password=123456"); }Copy the code

 

And then re-execute it

Add-Migration intidb
Copy the code

Then perform

Update-Database -Verbose
Copy the code

Open the database and you can see the database automatically generated by EFCore