Entity mapping using the Fluent API "Code-first series"

Source: Internet
Author: User
Tags connectionstrings

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.