Now, let's learn how to configure entities using the Fluent API.
One. Configuring the Default data table schema
Student entity
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef4{ Public classStudent { Public intStudentID {Get;Set; } Public stringStudentname {Get;Set; } Public intStuaentage {Get;Set; } Public stringStudentemail {Get;Set; } PublicStandard Standard {Get;Set; } }}
Standard entity
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef4{ Public classStandard { Public intStandardid {Get;Set; } Public intStandardName {Get;Set; } PublicIcollection<student> Students {Get;Set; } }}
Context class:
usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef4{ Public classDbcontextclass:dbcontext { PublicDbcontextclass ():Base("ConnectionStrings") { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) { Modelbuilder.hasdefaultschema ( "hello"); Base. Onmodelcreating (ModelBuilder); } }}
And then the resulting data table schema is the "Hello" we configured.
Of course, you can also set the schema for each table separately.
Two. Mapping entities to tables
usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef4{ Public classDbcontextclass:dbcontext { PublicDbcontextclass ():Base("ConnectionStrings") { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//schema of the Configuration data table//modelbuilder.hasdefaultschema ("Hello"); //mapping entities to tables modelbuilder.entity<student> (). ToTable ("wahhaha"); Modelbuilder.entity<Standard> (). ToTable ("standardinfo","xxx"); Base. Onmodelcreating (ModelBuilder); } }}
Then the database is like this:
Open a table one at a glance:
Three. Splits an entity into multiple tables.
The following code splits a student entity into two tables.
usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef4{ Public classDbcontextclass:dbcontext { PublicDbcontextclass ():Base("ConnectionStrings") { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//schema of the Configuration data table//modelbuilder.hasdefaultschema ("Hello"); //mapping entities to tables//modelbuilder.entity<student> (). ToTable ("Wahhaha"); //modelbuilder.entity<standard> (). ToTable ("Standardinfo", "xxx"); //Map an entity to multiple tablesModelbuilder.entity<student>(). Map (M={m.properties (P=P.studentid); M.properties (P=p.studentname); M.totable ("Studentinfo"); }). Map (M={m.properties (P=P.studentid); M.properties (P=p.stuaentage); M.properties (P=p.studentemail); M.totable ("studentdetails"); }); Modelbuilder.entity<Standard> (). ToTable ("Standardinfo"); Base. Onmodelcreating (ModelBuilder); } }}
The generated amount database is:
In the above code, we will student entities, split into two tables, Studentinfo and Studentdetail.
Map method need the delegate method as a parameter. You can pass the Action delegate or lambda expression in Map method, as shown below.
The map method requires a delegate as a parameter. You can pass an action delegate or lambda expression in this map method.
Entity mapping using the Fluent API "Code-first series"