Presentation is loading. Please wait.

Presentation is loading. Please wait.

Entity Framework: Relations

Similar presentations


Presentation on theme: "Entity Framework: Relations"— Presentation transcript:

1 Entity Framework: Relations
Database Applications SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

2 Table of Contents Configuring table relationships One-to-Zero-or-One
One-to-Many Many-to-Many  Inverse properties Inheritance Strategies Table per Hierarchy (TPH) Table per Type (TPT) Table per Concrete Type (TPC) © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

3 Questions sli.do #Entity

4 Table Relationships

5 One-to-Zero-or-One (using attributes)
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

6 One-to-Zero-or-One (using Fluent API)
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

7 One-to-Many (using attributes)

8 One-to-Many (using Fluent API)

9 Many-to-Many (using attributes)

10 Many-to-Many (using Fluent API)

11 Inverse properties The InverseProperty attribute is used when you have multiple relationships between classes. © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

12 Inheritance Strategies
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

13 Table per Hierarchy (TPH)
In the TPH mapping scenario, all types in an inheritance hierarchy are mapped to a single table. A discriminator column is used to identify the type of each row. When creating your model with Code First, TPH is the default strategy for the types that participate in the inheritance hierarchy. By default, the discriminator column is added to the table with the name “Discriminator” and the CLR type name of each type in the hierarchy is used for the discriminator values. You can modify the default behavior by using the fluent API. Additional information can be found here: © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

14 Modifying the default setting for TPH
modelBuilder.Entity<ParentClass>() .Map<ChildClassOne>(m => m.Requires(“NameOfDiscriminator") .HasValue(“valueForChildClassOne”)) .Map<ChildClassTwo>(m => m.Requires("NameOfDiscriminator") .HasValue(“valueForChildClassTwo”)); Advantages: fastest and most optimized of all the hierarchy table mappings. © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

15 Table per Type (TPT) In the TPT mapping scenario, all types are mapped to individual tables. Properties that belong solely to a base type or derived type are stored in a table that maps to that type. Tables that map to derived types also store a foreign key that joins the derived table with the base table. Additional information can be found here: © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

16 Modifying the classes to use TPT
We can create a TPT mapping simply by placing Table attribute on the subclasses to specify the mapped table name OR modelBuilder.Entity<ChildClassOne>().ToTable(“TableNameOfChildClassOne”); modelBuilder.Entity<ChildClassTwo>().ToTable(“TableNameOfChildClassTwo”); © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

17 Table per Concrete Type (TPC)
In Table per Concrete type (aka Table per Concrete class) we use exactly one table for each (nonabstract) class. All properties of a class, including inherited properties, can be mapped to columns of this table. Polymorphism and inheritance relationships completely from the SQL schema. Additional information can be found here: © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

18 Modifying the classes to use TPC
modelBuilder.Entity<ChildClassOne>().Map(m => { m.MapInheritedProperties(); m.ToTable(" TableNameOfChildClassTwo"); }); modelBuilder.Entity<ChildClassTwo>().Map(m => { m.MapInheritedProperties(); m.ToTable(“TableNameOfChildClassOne"); }); © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

19 Modifying the classes to use TPC
Attach [DatabaseGenerated(DatabaseGenerationOption.None)] to the key of the base class. OR modelBuilder.Entity<ParentClass>()             .Property(p => p.”Id”)             .HasDatabaseGenerationOption( DatabaseGenerationOption.None); © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

20 https://softuni.bg/courses/
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

21 License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

22 Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "Entity Framework: Relations"

Similar presentations


Ads by Google