简体   繁体   中英

How to Implement CRUD using Entity Framework Core TPT Inheritance with SQL Server

Given the following...

Here is my object model:

public class Person
{
    public Person() { }
    public Guid PersonId { get; set; } = Guid.NewGuid();
    public String FirstName { get; set; } = string.Empty;
    public String MiddleName { get; set; } = string.Empty;
    public String LastName { get; set; } = string.Empty;
}

public class JobApplicant : Person
{
    public JobApplicant() { }
}

public class Employee : Person
{
    public Employee() { }
}

Here is what i have in my DbContext:

public DbSet<Employee> Employees { get; set;}

public DbSet<JobApplicant> JobApplicants { get; set; }

public DbSet<Person> Persons { get; set; }

var person = mb.Entity<Person>();
person.HasKey(aa => aa.PersonId);

Here is the short version of what i'm doing in my web api function:

Employee newEmployee = new Employee();
newEmployee.PersonId = "Guid value here";
db.Employees.Add(newEmployee);
db.SaveChanges();

In the database I have a Person , JobApplicant , and Employee table. Person table has a PersonId as PK. Employee and JobApplicant each have a PersonId field that is both the primary key of their table as well as a FK to the Person table. This is what EF generated via the migration using the Code First approach.

The scenario is that the person I am trying to add as an Employee already exists in the Person table because they were added to it via a JobApplicant but they do not already exist in the Employee table and I am trying to insert a record for them into the Employee table but with all this in place I currently get the following error when executing db.SaveChanges() :

SqlException: Violation of PRIMARY KEY constraint 'PK_Person'. Cannot insert duplicate key in object 'dbo.Person'. The duplicate key value is (f73190f2-d766-4199-9dcd-2831c0844141).

What is the proper way to handle this situation?

In your scenario, using inheritance, the real identities are the JobApplicant and the Employee. The Person entity is simply a convenience for storing similar information that can exist in a JobApplicant an Employee. Employee 中的类似信息。

I think you need to consider Person to be a unique identity in itself and use a relationship, instead of inheritance.

public class Person
{
    public Person() { }

    public Guid PersonId { get; set; } = Guid.NewGuid();

    public String FirstName { get; set; } = string.Empty;

    public String MiddleName { get; set; } = string.Empty;

    public String LastName { get; set; } = string.Empty;
}

public class JobApplicant
{
    public JobApplicant() { }

    public Guid JobApplicationId { get; set; } = Guid.NewGuid();

    public Person PersonalInfo { get; set; }
}

public class Employee
{
    public Employee() { }

    public Guid EmployeeId { get; set; } = Guid.NewGuid();

    public Person PersonalInfo { get; set; }
}

You can then use the same Person (PeronalInfo) in both the JobApplicant and Employee.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM