How to insert Derived Class with Entity Framework (TPH approach)

c# entity-framework entity-framework-6

Accepted Answer

You may use one of three techniques to implement inheritance in a database.

This method proposes a single table for the complete class inheritance structure. Each-Table Hierarchy (TPH) The discriminator column in the table separates the various inheritance classes. In Entity Framework, this is the default inheritance mapping approach.

This strategy proposes a distinct table for each domain class. The Table by Type (TPT)

This method recommends one table for one concrete class, but not for the abstract class. Table for class of Concrete (TPC) The properties of the abstract class will thus be included in each table of the concrete class if the abstract class is inherited by several concrete classes.

here has further information.

I believe the Table per Concrete class (TPC), which looks like this, is what you're searching for.

enter image description hereenter image description here

Update:

I learn about your issue. The property doesn't need to be added.Person in yourUser due to the fact that it already derives from that class. It should operate if you remove it.

public class User : Person
{
    //[Required]
    //public Person Person { get; set; } Remove this line
    [Required]
    public string Username { get; set; }
}

[Table("Person")]
public class Person : Entity
{
    [Required]
    public List<User> Users { get; set; }
    public string Name { get; set; }
}
2
6/26/2016 9:10:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow