EF Core (Entity Framework Core) is the. NET core version of EF. EF Core supports SQL Server very well,
You can also connect to SQL Server under Linux. But if MySQL is preferred under Linux, try MySQL this time.
Efcore Nuget:Microsoft.EntityFrameworkCore (usually not need to be installed separately, when installing the MySQL package will automatically download the dependent packages).
The official MySQL EF provider online to inform the netizen that there are many bugs (https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/)
It uses the third-party EF Core Provider for MySQL (http://www.1234.sh/post/pomelo-data-mysql), (dependent on POMELO.DATA.MYSQL)
- First install POMELO.ENTITYFRAMEWORKCORE.MYSQL with NuGet, it is recommended to install the latest version (added features, fixed bug, I previously installed the initial version does not support database migration migration). EF is no longer required in EF core to install in the UI project.
- To establish a model entity class, this project implements a rights management system, which establishes the following tables:
public class userentity:baseentity {public long ID {get; set;} public string Name {get; set;} public string Phonenum {get; set;} public string Email {get; set;} public string Pwdsalt {get; set;} public string Pwdhash {get; set;} public int Loginerrortimes {get; set;} Public DateTime? lastloginerrortime {get; set;} } public class roleentity:baseentity {public long ID {get; set;} public string Name {get; set;} } public class permissionentity:baseentity {public long ID {get; set;} public string Name {get; set;} public string Description {get; set;} }
EF core is similar to EF and has some default configurations
BOOL---"bit 1 (length)
String---> Longtext nullable
DateTime--->datetime 6
......... And so on, here are some examples of the APIs that are commonly used
var modelbuilder=new modelBuilder (); var userbuilder= modelbuilderentity<userentity> (). ToTable ("T_users");//map to which table userbuilder.property (U = u.name). Hasmaxlength (50). IsRequired (TRUE);//Set the length and whether it is nullable, calling isrequired default to True is not null userrolerelation.hasone (M = m.user). Withmany (). Hasforeignkey (M = m.userid). IsRequired ();//One-to-many relationship configuration
- Configure the database context, build your own Mydbcontext (name arbitrary), inherit DbContext, rewrite onmodelcreating and onconfiguring methods;
Since EF core has not yet been configured to support many-to-many relationships in 2.0, we are going to implement it through the configuration of two one-to-many relationships:
public class Mydbcontext:dbcontext {public dbset<userentity> Users {get; set;} Public dbset<permissionentity> Permissions {get; set;} Public dbset<roleentity> Roles {get; set;} protected override void Onmodelcreating (ModelBuilder ModelBuilder) {base. Onmodelcreating (ModelBuilder); var userbuilder= modelbuilder.entity<userentity> (). ToTable ("T_users"); Userbuilder.property (U = u.email). Hasmaxlength (50); Userbuilder.property (U = u.name). Hasmaxlength (50). IsRequired (); Userbuilder.property (U = u.phonenum). Hasmaxlength (20). IsRequired (); var permissionbuilder= modelbuilder.entity<permissionentity> (). ToTable ("T_permissions"); Permissionbuilder.property (p = p.name). IsRequired (). Hasmaxlength (50); Permissionbuilder.property (p = p.description). Hasmaxlength (50); Modelbuilder.entity<roleentiTy> (). ToTable ("T_roles"); var userrolerelation = modelbuilder.entity<userroleentity> (). ToTable ("T_userrolerelation"); Userrolerelation.hasone (M = m.user). Withmany (). Hasforeignkey (M = m.userid). IsRequired (); Userrolerelation.hasone (M = m.role). Withmany (). Hasforeignkey (M = M.roleid). IsRequired (); var rolepermissionrelation = modelbuilder.entity<rolepermissionentity> (). ToTable ("T_rolepermissionrelation"); Rolepermissionrelation.hasone (M = m.role). Withmany (). Hasforeignkey (M = M.roleid). IsRequired (); Rolepermissionrelation.hasone (M = m.permission). Withmany (). Hasforeignkey (M = M.permissionid). IsRequired (); } protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) {base. Onconfiguring (Optionsbuilder); Optionsbuilder.usemysql (Confighelper.sqlconnstr ()); } }
- Applying database migrations, building databases based on model classes
NuGet installs the Install-package Microsoft.EntityFrameworkCore.Tools package for database migrations
Then Add-migration Initdb, initialize the database
Finally, the database can be generated update-database:
Supplemental NOTE: Generated database I entered "administrator" when inserting roles role, "Insert failed, trace information display do not know where to come from some of the XE and other characters, by check this is because the automatically generated my database encoding for the latin1 character set, manually change the database encoding to UTF8."
(3) Use of the. Net Core EF Core