"Selfless sharing: ASP. NET CORE Project Combat" Catalog Index
Brief introduction
In this chapter we introduce the following ASP. NET Core using Codefirst to create databases and tables, through the console and dotnet EF two ways
To modify the EF context object, add a test class
I changed the name and Domains changed to WKMVC. Data
We create a new folder Models for storing model classes
We'll create a new folder under models. Sysmodelsmange for zone model classes
We create a new test class Sys_user
Namespace Wkmvc. Data.models
{
public class Sys_user
{
public int ID {get; set;}
public string USERNAME {get; set;}
}
}
Let's change the context Applicationdbcontext (yellow highlight)
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 execute add-migration from the console
Then open WKMVC. The Project.json below the Data class library adds 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" frameworks ": {" netstandard1.6 ": { " Imports ":" Dnxcore50 " }15 },16 " Tools ": { " Microsoft.EntityFrameworkCore.Tools ":" 1.0.0- Preview2-final " }19}
We open tools, NuGet Package Manager, package management console, select Default Project Src\wkmvc. Data
Input command: add-migration migrations
No recognition command? Looking through Microsoft's official documents, mostly using dotnet EF (described later) command, find an article
Execute this statement on the console 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 could throw this error message. That is, the class library is not supported, and here Microsoft gives two solutions:
First, reference https://github.com/dotnet/cli/issues/2645.
Second, modify the class library project as "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 took a second try and I did, but when I added a new class library, I referenced WKMVC. Data does not, the class library cannot reference the app project, but our context applicationdbcontext below this, what to do?
-pre, and then switch back to SRC\WKMVC. Data
Note: If the Windows Power Shell version of the system is 2 to upgrade to 3 or 4
It worked!
Then execute Update-database-verbose
This is not the right to create a database, we modify the user as an administrator, re-execute
Success, Done!
by Dotnet EF
How to do this in the class library, or refer to the above Microsoft two solutions, I did not solve the problem is not to execute the command, but my context is in WKMVC. Data inside, always can not find this context, if there is a big God pointing, I will revise this article, if not first put, have the opportunity to solve, and then update. Let's introduce today, just like Microsoft's Demo, context and migrations are all used under SRC\WKMVC (web):
We open the folder where we want to add migrations (SRC\WKMVC), select this folder, Shift right-click, choose here to open Command Window (W)
Input dotnet EF--help
Here, the documentation is clear and we need to take a few steps:
① adding Microsoft.EntityFrameworkCore.Tools
② adding Microsoft.EntityFrameworkCore.Design
Then execute dotnet EF--help again
The instructions are successful, we add migrations
Input: dotnet EF Migrations Add migrations
Add complete, we can perform other operations such as dotnet EF database Update, detailed reference https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html ? highlight=migrations#usage
I want to learn from you. ASP.
Just beginning to contact, the level is limited, a lot of things are their own understanding and flipping through the online god of information, if there are wrong places and do not understand the place, I hope you correct!
Although ASP. NET Core is now very hot, but many of the online information is the copy of the previous article, so there are a lot of problems I have not resolved, I hope we can help together!
"Selfless sharing: ASP. NET CORE Project Combat (Fourth)" Code first create database and data tables