Entity Mappings using Fluent API:
Here, we'll learn how to configure an entity using the Fluent API.
We'll use the following Student and standard domain classes of the school application.
Public classstudent{ PublicStudent () {} Public intStudentID {Get;Set; } Public stringStudentname {Get;Set; } PublicDatetime? dateOfBirth {Get;Set; } Public byte[] Photo {Get;Set; } Public decimalHeight {Get;Set; } Public floatWeight {Get;Set; } PublicStandard Standard {Get;Set; }} Public classstandard{ PublicStandard () {} Public intStandardid {Get;Set; } Public stringStandardName {Get;Set; } PublicIcollection<student> Students {Get;Set; } }
Configure Default Schema:
First, let's configure a default schema for the tables in the database. However, you can change the schema while creating the individual tables. The following example sets the default Admin schema.
Public classSchoolcontext:dbcontext { PublicSchooldbcontext ():Base() { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//Configure Default SchemaModelbuilder.hasdefaultschema ("Admin"); }}
Map Entity to Table:
Code-first would create the database tables with the name of DbSet properties in the context class-students and standards In the case. You can override this Convention and can give a different table name than the DbSet properties, as shown below.
namespacecodefirst_fluentapi_tutorials{ Public classSchoolcontext:dbcontext { PublicSchooldbcontext ():Base() { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//Configure Default SchemaModelbuilder.hasdefaultschema ("Admin"); //Map entity to tableModelbuilder.entity<student> (). ToTable ("Studentinfo"); Modelbuilder.entity<Standard> (). ToTable ("Standardinfo","dbo"); } }}
As can see in the above example, we-start with the Entity<tentity> () method. Most of the time, you have to start with the Entity<tentity> () method to configure it using the Fluent API. We have the Used ToTable () method to map Student entity to studentinfo and the standard entity to Standardinfo table. Notice that studentinfo are in the Admin schema and Standardinfo table is in the DBO schema because we have specified dbo schema fo R standardinfo table.
Map Entity to multiple Table:
The following example shows how to maps Student entity to multiple tables in the database.
namespacecodefirst_fluentapi_tutorials{ Public classSchoolcontext:dbcontext { PublicSchooldbcontext ():Base() { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {modelbuilder.entity<Student> (). Map (M ={m.properties (P=New{p.studentid, p.studentname}); M.totable ("Studentinfo"); }). Map (M={m.properties (P=New{p.studentid, p.height, P.weight, P.photo, P.dateofbirth}); M.totable ("Studentinfodetail"); }); Modelbuilder.entity<Standard> (). ToTable ("Standardinfo"); } }}
As can see in the above example, we mapped some properties of Student entity to Studentinfo table and other properties To Studentinfodetail table using Map () method. Thus, Student entity would split into a tables, as shown below.
Map method need the delegate method as a parameter. You can pass the Action delegate or lambda expression in Map method, as shown below.
usingSystem.Data.Entity.ModelConfiguration.Configuration;namespacecodefirst_fluentapi_tutorials{ Public classSchoolcontext:dbcontext { PublicSchooldbcontext ():Base() { } PublicDbset<student> Students {Get;Set; } PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {modelbuilder.entity<Student> (). Map (Delegate(entitymappingconfiguration<student>studentconfig) {studentconfig.properties (P=New{p.studentid, p.studentname}); Studentconfig.totable ("Studentinfo"); }); Action<EntityMappingConfiguration<Student>> studentmapping = m = ={m.properties (P=New{p.studentid, p.height, P.weight, P.photo, P.dateofbirth}); M.totable ("Studentinfodetail"); }; Modelbuilder.entity<Student>(). Map (studentmapping); Modelbuilder.entity<Standard> (). ToTable ("Standardinfo"); } }}
Entity Framework Code-first (10.2): Entity Mappings