EntityFramework study Notes--008-inheritance relationship mapping based on Entity Data Modeling TPH

Source: Internet
Author: User

Table per Hierarchy Inheritance modeling

1. Let's assume that you have a table in 8-1 and the employee table contains rows for hourly employees and salaried employees. Column Employeetype is used as the identification column to identify the rows of the two employee types. When Employetype is 1 o'clock, this line represents a full-time employee (salaried or full-time employee) with a duty of 2 o'clock, a line of code that is an hourly (hourly employee).

Figure 8-1

2. Right-click your project and add the Poco entities in. as follows:

[Table ("Employee", Schema ="Example8")]     Public Abstract classEmployee {[Key] [databasegenerated (databasegeneratedoption.identity)] Public intEmployeeId {Get;protected Set; }  Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; } }
Employee
 Public class Fulltimeemployee:employee    {        publicdecimalgetset;}    }
Fulltimeemployee
 Public class Hourlyemployee:employee    {        publicdecimalgetset;}    }
Hourlyemployee

3. Create a context object that inherits from DbContext Ef6recipescontext;

 Public classEf6recipescontext:dbcontext { PublicDbset<employee> Employees {Get;Set; }  PublicEf6recipescontext ():Base("Name=ef6codefirstrecipescontext")        {        }        protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {Base.            Onmodelcreating (ModelBuilder); Modelbuilder.entity<Employee>()            . Map<FullTimeEmployee> (M = M.requires ("Employeetype"). HasValue (1))            . Map<HourlyEmployee> (M = M.requires ("Employeetype"). HasValue (2));//If you remove these two maps, you will see that the program also works, and an additional attribute "Discriminator" is generated in the employee table.        }    }

4. Test the code:

1 using(varContext =NewEf6recipescontext ())2             {3                 varFTE =NewFulltimeemployee4                 {5FirstName ="Jane",6LastName ="Doe",7Salary =71500M8                 };9 context. Employees.add (FTE);TenFTE =NewFulltimeemployee One                 { AFirstName ="John", -LastName ="Smith", -Salary =62500M the                 }; - context. Employees.add (FTE); -                 varHourly =NewHourlyemployee -                 { +FirstName ="Tom", -LastName ="Jones", +Wage =8.75M A                 }; at context. Employees.add (hourly); - context. SaveChanges (); -             } -             using(varContext =NewEf6recipescontext ()) -             { -Console.WriteLine ("---all Employees---"); in                 foreach(varEmpinchcontext. Employees) -                 { to                     BOOLfulltime = EMP isHourlyemployee?false:true; +Console.WriteLine ("{0} {1} ({2})", EMP. FirstName, EMP. LastName, -Fulltime?" Full time":"Hourly"); the                 } *Console.WriteLine ("--- full time---"); $                 foreach(varFteinchContext. Employees.oftype<fulltimeemployee>())Panax Notoginseng                 { -Console.WriteLine ("{0} {1}", FTE. FirstName, FTE. LastName); the                 } +Console.WriteLine ("---Hourly---"); A                 foreach(varHourlyinchContext. Employees.oftype()) the                 { +Console.WriteLine ("{0} {1}", hourly. FirstName, hourly. LastName); -                 } $}
View Code

The output is as follows:

---all Employees---jane Doe (full time), John Smith (full time), Tom Jones (Hourly)---full time---jane Doejohn Smith---Ho Urly---Tom Jones

5. Take note of the comments in step 3, if you comment out the " Onmodelcreating, query the SQL generated by the employee as follows:
SELECT     [extent1].[ Discriminator] As [discriminator],     [extent1].[ EmployeeId] As [EmployeeId],     [extent1].[ FirstName] As [FirstName],     [extent1].[ LastName] As [LastName],     [extent1].[ Salary] As [Salary],     [extent1].[ Wage] as [wage] from    [example8].[ Employee] as [Extent1]    WHERE [extent1].[ Discriminator] In (n ' Fulltimeemployee ', n ' hourlyemployee ')
In fact, the relationship between "TPT" and "TPH" can be easily understood as whether the subclass is mapped to the same table as the parent class. "TPT" is relatively flexible, single query with the use of join, the performance will naturally have a slight impact; "TPH" It stores the entire inheritance type in a separate table, which solves the join connection problem in TPT and provides good performance. But the flexibility of the database was sacrificed.

EntityFramework study Notes--008-inheritance relationship mapping based on Entity Data Modeling TPH

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.