TPH, TPT, TPC in EF

Source: Internet
Author: User

1. Table Per Hierarchy (TPH): Create only one table that maps all the properties in the base class and subclass to the columns in the table
2. Table Per Type (TPT): Establishes a table for the base class and each subclass, and each table corresponding to the child class contains only the columns for the attributes that are unique to the subclass
3. Table per concrete (TPC): Establishes a table for each subclass that corresponds to the column and subclass-specific properties of the table that corresponds to the child class that contains the properties of the base class

above excerpt from: Transmission array

TPH

Examples are as follows:

1   Public class resort:lodging 2     {3public         stringgetset;} 4 5          Public string Get Set ; } 6 7     }

1 namespaceMsdnblog2 {3      Public classMycontext:dbcontext4     {5          Publicmycontext ()6:Base("defaultconnection")7         { 8          9         }Ten          PublicDbset<lodging> Lodgings {Get;Set; } One  A         protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) -         { -Modelbuilder.entity<lodging>() the. Map<lodging> (L = l.requires (" from"). HasValue ("loding")) -. Map<resort> (r = r.requires (" from"). HasValue ("Resot")); -             Base. Onmodelcreating (ModelBuilder); -         } +          -     } +  A      Public classLodging at     { -          Public intLodgingid {Get;Set; } -          Public stringName {Get;Set; } -          Public stringOwner {Get;Set; }  -     } -  in      Public classresort:lodging -     { to          Public stringEntertainment {Get;Set; } +  -          Public stringActivities {Get;Set; } the  *     } $}
View Code

The resulting table structure

The role of discriminator is to identify whether the data is from a base class or a subclass, the default type is nvarchar (128), and the corresponding value is the name of the corresponding class

You can modify the name of the discriminator using the Fluent API:

1   protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder)2         {3Modelbuilder.entity<lodging>()4. Map<lodging> (L = l.requires (" from"). HasValue ("loding"))5. Map<resort> (r = r.requires (" from"). HasValue ("Resot"));6             Base. Onmodelcreating (ModelBuilder);7}

The structure of the corresponding table at this time is as follows:

TPT

In a TPT mapping scenario, all types are mapped to different tables, and only attributes that belong to a base type or derived type are stored in a table that is mapped to that type. Tables that are mapped to derived types also store a foreign key that joins the derived table to the base table.

1    //modelbuilder.entity<lodging> (). ToTable ("lodings");2             //modelbuilder.entity<resort> (). ToTable ("Restorts");3Modelbuilder.entity<lodging>()4. Map<lodging> (L = l.totable ("LODINGS2"))5. Map<resort> (R=>r.totable ("Resorts2"));6                 

The resulting table structure is as follows:

Of course, you can also use the data annotation method for processing

[Table ("Lodging")]     Public classLodging { Public intLodgingid {Get;Set; }  Public stringName {Get;Set; }  Public stringOwner {Get;Set; } } [Table ("Resort")]     Public classresort:lodging { Public stringEntertainment {Get;Set; }  Public stringActivities {Get;Set; } }
View Code

Tpc

Create a table for each subclass that contains the columns for the properties of the base class that correspond to the columns and properties that are specific to the subclass. TPC cannot use data annotation configuration

1   //modelbuilder.entity<lodging> (). ToTable ("lodgings");2             //modelbuilder.entity<resort> (). Map (r = {r.mapinheritedproperties (); R.totable ("Resorts");});3Modelbuilder.entity<lodging>()              4. Map<resort> (R = {5R.totable ("Resorts");6 r.mapinheritedproperties ();7});

The resulting table structure is as follows:

TPH, TPT, TPC in EF

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.