[Selfless sharing: ASP. net core Project Practice (chapter 5)] Code First creates databases and data tables, asp. netcore
Directory Index
[Selfless sharing: ASP. net core project practice] Directory Indexing
Introduction
In this chapter, we will introduce how to use CodeFirst to create a database and a table in Asp.net Core through the console and dotnet ef.
Modify the EF context object and add a test class
I changed the name and Domains to wkmvc. Data.
Create a new folder named Models to store model classes.
Create a new folder SysModelsMange under Models for the region model class.
Create a test class SYS_USER
Namespace wkmvc. Data. Models
{
Public class SYS_USER
{
Public int ID {get; set ;}
Public string USERNAME {get; set ;}
}
}
Let's modify the context ApplicationDbContext (highlighted in yellow)
Using Microsoft. EntityFrameworkCore;
Using wkmvc. Data. Models;
Namespace wkmvc. Data
{
Public class ApplicationDbContext: DbContext
{
Public ApplicationDbContext (DbContextOptions <ApplicationDbContext> options)
: Base (options)
{
}
Public DbSet <SYS_USER> SYS_USER {get; set ;}
Protected override void OnModelCreating (ModelBuilder builder)
{
Base. OnModelCreating (builder );
}
}
}
Add a dependency package and run Add-Migration on the console.
Open project. json under the wkmvc. Data class library and add Microsoft. EntityFrameworkCore. SqlServer and Microsoft. EntityFrameworkCore. Tools.
1 { 2 "version": "1.0.0-*", 3 4 "dependencies": { 5 "Microsoft.EntityFrameworkCore": "1.0.0", 6 "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", 7 "NETStandard.Library": "1.6.0", 8 "System.ComponentModel.Annotations": "4.1.0" 9 },10 11 "frameworks": {12 "netstandard1.6": {13 "imports": "dnxcore50"14 }15 },16 "tools": {17 "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"18 }19 }
Choose tools> NuGet Package Manager> package management console and select the default project src \ wkmvc. Data.
Enter the command: Add-Migration Migrations
No recognition command? Read Microsoft's official documents. Most of them use the dotnet ef command (described later ).
Run the Install-Package Microsoft. entityFrameworkCore. tools-Pre, error, restore failed ,. NET Core CLI does not support running commands on class libraries as of Preview 2. despite being able to install EF tools, executing commands may throw this error message. that is, the class library is not supported. Here, Microsoft provides two solutions:
1. Reference https://github.com/dotnet/cli/issues/2645.
2. Modify the class library project to the "app" project, and use the context factory IDbContextFactory <TContext>, Microsoft document address: https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html? Highlight = migrations # common-errors
I tried the second method, but when I add a new class library, I reference wkmvc. when Data fails, the class library cannot reference the app project, but our context ApplicationDbContext is under this, what should we do?
However, I tried to add Microsoft under wkmvc. entityFrameworkCore. use Tools to change the default console project to src \ wkmvc and run Install-Package Microsoft. entityFrameworkCore. tools-Pre, and switch back to src \ wkmvc. data
Successful! Somehow, I don't know how it works. It's a success.
Then execute Update-Database-Verbose
This operation has no permission to create a database. we modify the user as the administrator and re-Execute
Done!
Using dotnet ef
How to execute this in the class library or refer to the two solutions provided by Microsoft above. I did not solve the problem, not that the command cannot be executed, but that my context is in wkmvc. this context is always not found in Data. If you have some tips, I will modify this article. If not, I will release it first. I have the opportunity to solve this problem and update it again. Let's introduce how to use both context and Migrations under src \ wkmvc (web), just like Microsoft's Demo:
Open the folder we want to add Migrations (src \ wkmvc), select this folder, press Shift and right-click, and select Open command window (W)
Enter dotnet ef -- help
Here, the document clearly states that we need to perform several operations:
① Add Microsoft. EntityFrameworkCore. Tools
② Add Microsoft. EntityFrameworkCore. Design
Run dotnet ef -- help again.
It indicates that the Migrations is successfully added.
Input: dotnet ef migrations add Migrations
After adding, we can execute other operations such as dotnet ef database update. For more information, see https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html? Highlight = migrations # usage
I hope to study Asp.net Core with you.
At the beginning, there was a limited level of contact, and many things were self-understanding and reading the information of the great gods on the Internet. If there is something wrong or incomprehensible, I hope you can correct it!
Although Asp.net Core is very popular now, many materials on the Internet are copied in the previous article, so I have not solved many problems for the time being. I hope you can help me together!
Original article reprinted please respect labor results http://yuangang.cnblogs.com