Entity Framework DB-First, implement inheritance

c# ef-database-first entity-framework inheritance

Question

Using the database first technique and entity framework 6.0, I'm attempting to build inheritance. Let's assume that I have aPerson and anOrganization something like this:

// a simplified version of organization entity
public class Organization
{
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string OfficialName { get; set; }
    public Guid CEOID { get; set; }
    public DateTime? RegisterDate { get; set; }
}

// a simplified version of person entity
public class Person
{
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public Guid PersonID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string NationalCode { get; set; }
    public DateTime? BirthDate { get; set; }
}

These two tables can be created in the database, but I want to use inheritance to make the fields that are repeated in both tables unique.Person and Organization Possibly in a different base class, as shown below:

public class Identity
{
    // These fields are the common fields between Person and Organization
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
}

How can I accomplish this using db-first methodology?

1
17
3/8/2014 12:30:37 AM

Accepted Answer

I personally like to use TPT (table-per-type), a viable alternative to one desk for each kind. To accomplish this, define your tables according to the model in the image below:

table hierarchy

Keep in mind that all common fields have been relocated to the base table and that the relationships between the child and base entities are one-to-one on their PK columns. Once your tables are ready, right-click the models page in Visual Studio and choose updating the model from the database. Then, on the add tab, choose to add these three tables. You should first see this model diagram, which needs to be modified slightly:

tables added at first

Take these actions forPerson and Organization separately:

  • To pick Properties, right-click on the entity.
  • Select the Type Base property.Identity
  • Choose this entity, then choose between it andIdentity
  • Choose this entity, then select Remove the PK. (ID column) (Inherits from base entity)

Following these actions, save your model. Your model should now appear as follows:

the changed model

Now finish up your project and have fun!

A few more sources
Designer of Entity Framework TPH Inheritance

26
5/18/2018 5:20:28 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