1. Preface
The previous article is about the EF6 of the entity relationship design and migration, reserved problems are no longer added. Reason to write this series of articles, originally intended for the introduction of ASP. NET 5, EF6 does not support it, skip it.
EF's performance is never open to the topic, the garden teachers have been arguing. (Each time you see this type of post, the number of hits is high)
Do not know whether people understand: What is the platform? What is a framework? What is a class library? I may also say wrong here, also hope you correct.
EF is obviously not a platform because it cannot be relied upon to develop or run a single application.
What is it, a frame? Speaking of which, we still have to explain what the framework is? Simply say it rules or rules better shelves, you have to do the function on this basis, you need to write some code. Obviously EF is a framework.
So, what is a class library? You can understand that it is a code that has already been written, you can use it to make its way, and you generally don't need to modify its code.
There is no ambiguity about the platform, how is the framework and the class library differentiated? Otherwise, look at it. If the two frames together, generally not used together, and many kinds of library together there will be no conflict.
EF has a generation of SQL statement procedures and entity state tracking, with a slight performance penalty, think about whether this impact is otherwise improved? For example: can generate SQL statements be cached? Do you use data caching? ......
If you're not using EF as a CRUD , you can use it only as a build table (anyway, you have to object-oriented development, you always have to write entity classes), and it's a handy " tool ".
In summary, EF is based on ADO. NET Encapsulation Framework , on this basis to play their own coding level, perhaps more critical.
2. Features
What's so cool about EF7? It is still in the beta stage with little data. If you read the code level high, see the source https://github.com/aspnet/EntityFramework is the best.
EF7 native support for SQL Server(relational database) and sqllite(embedded database) will also be supported for Redis(NoSQL database), InMemory (Easy-to-test memory database) support;
EF7 mapping mode only Code Frist, that is, no longer support database Frist and model Frist;
Thinking:can EF7 cross the platform? Support any project type? Please think, here we only demonstrate the use of ASP. NET 5 projects. Since ASP. NET 5 can cross the platform, EF7 follow, you know drip!
3. Create a project
Also, for demonstration purposes, create an ASP. WEBAPI Project of the net 5 Type:
4. Entity classes
EntityBase.cs Code:
usingSystem;usingSystem.ComponentModel.DataAnnotations;namespacegivecase.entityframeworks.models{/// <summary> ///Entity base class/// </summary> /// <typeparam name= "TKey" >primary Key Type</typeparam> Public Abstract classEntitybase<tkey> { /// <summary> ///primary Key ID (the primary key type is determined based on inheritance)/// </summary> PublicTKey Id {Get;Set; } /// <summary> ///creation Time/// </summary> PublicDateTime Createtime {Get;Set; } =DateTime.Now; /// <summary> ///whether to delete (tombstone, not physical delete)/// </summary> Public BOOLIsdelete {Get;Set; } =false; /// <summary> ///row version (timestamp processing concurrency)/// </summary>1537759804 Public byte[] RowVersion {Get;Set; } }}
Role.cs Code:
usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;namespacegivecase.entityframeworks.models{/// <summary> ///role Entity/// </summary> Public classrole:entitybase<int> { /// <summary> ///Role Name/// </summary>[Required (errormessage ="cannot be empty")] Public stringRoleName {Get;Set; } /// <summary> ///Description/// </summary> Public stringDescription {Get;Set; } /// <summary> ///User entity collection (navigation properties)/// </summary> PublicIcollection<user> Users {Get;Set; } }}
User.cs Code:
usingSystem.ComponentModel.DataAnnotations;namespacegivecase.entityframeworks.models{/// <summary> ///User Entities/// </summary> Public classuser:entitybase<int> { /// <summary> ///User name/// </summary>[Stringlength ( -, Minimumlength =6, errormessage ="character length must be between 6-24")] Public stringUserName {Get;Set; } /// <summary> ///Password/// </summary>[Stringlength ( +, Minimumlength =6, errormessage ="character length must be between 6-32")] Public stringPassword {Get;Set; } /// <summary> ///Sex/// </summary> PublicSex Sex {Get;Set; } =Sex.secret; /// <summary> ///Email/// </summary>[EmailAddress (errormessage ="must conform to e-mail address format")] Public stringEmail {Get;Set; } /// <summary> ///role ID (foreign key)/// </summary> Public intRoleid {Get;Set; } /// <summary> ///Role entities (navigation properties)/// </summary> PublicRole role {Get;Set; } } /// <summary> ///gender (enum type)/// </summary> Public enumSex {man, Woman, Secret}}
5. Configuration
Open Project.json
Note: Commands is for migration!
6. Database context
EFContext.cs Code:
usingMicrosoft.Data.Entity;namespacegivecase.entityframeworks.models{ Public classEfcontext:D Bcontext { PublicDbset<role> Roles {Get;Set; } PublicDbset<user> Users {Get;Set; } protected Override voidonconfiguring (dbcontextoptions options) {options. Usesqlserver ("server=.; Database=eftestdb; Uid=sa; pwd=123456"); } }}
Note: The database connection string can be obtained by reading the configuration file, see Chapter No. 03 !
7. Migration
Enter the project directory (under System DOS, which is said in the " No. 02 Chapter "):
Enable EF commands:
To Add a migration:
agree to migrate (update to database):
To view a data table diagram :
ok! Description Migration Successful!
8. Summary
After the evening shift, I took the time to write this article. Just demonstrate the EF7 migration, other knowledge has no energy to pull, next time!
No. 06 Chapter ASP. 5:entityframework7