0

I have a scenario where I need to implement Mutually Inclusive Inheritance and I am using Entity Framework Code First as ORM. I have an abstract User class, Customer and Employee classes which are inherited from User. I have currently implemented table-per-type to model inheritance in SQL database.

Now, whenever I try to fetch both of the entities with a single db context object it gives me this exception

All objects in the EntitySet 'Context.Users' must have unique primary keys. However, an instance of type 'Employee' and an instance of type 'Customer' both have the same primary key value, 'EntitySet=Users;Id=153'

I do understand the reason why this is causing issue, but I can't find any workaround for it.

The solution that might work is that I would create a separate Primary Key for Customer and Employee and keep UserAccountId as Foreign Key, but I can't find a way to configure it through Entity Framework.

Following is a dummy code for my scenario

public abstract class UserAccount
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
}

public class Employee : UserAccount
{

}

public class Customer : UserAccount
{

}

public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
    public CustomerConfiguration()
    {
        ToTable(nameof(Customer));
    }
}

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
    public EmployeeConfiguration()
    {
        ToTable(nameof(Employee));
    }
}
10
  • Show us your code please Commented Aug 10, 2017 at 13:10
  • Are you adding data manually to db, because EF won't allow to add data in both Employee and Customer table for a user entry. Commented Aug 10, 2017 at 13:20
  • No, I am using entity framework to insert the data too, and the error I am facing is while fetching and not while adding data. Thanks Commented Aug 10, 2017 at 13:21
  • Maybe show us the code for that? Commented Aug 10, 2017 at 13:22
  • Do you have a single DbSet for these entities? Also, please show how you are adding the data. Commented Aug 10, 2017 at 13:22

2 Answers 2

1

What you are trying to achieve is not right. When using ORM try to think in terms of Objects. A User cannot be converted have two concrete type when you fetch the data.

You need to look at the problem from a different perspective and design your object relation accordingly. You need to use composition here and not inheritance.

Sign up to request clarification or add additional context in comments.

2 Comments

Your ans is too harsh for him :D
Yeah, that's correct. But I had in mind that if I could configure EF to have a different primary key in Customer and Employee rather than making UserAccount.Id primary key and foreign key, then I might get a work around of this problem. Can you think of anyway to configure it like this. and of course composition is my fallback approach if i don't find any good solution.
0

At the moment User can have both a Customer and Employee entry with the same primary key. So when you tell Entity Framework to select all Users you are getting a clash on the User.Id column.

I presume that you want to have one User entry per Customer and Employee.

You could do this by removing the inheritance.

Example

public class UserAccount
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public Employee Employee {get;set;}
    public Customer Customer {get;set;}
}

public class Employee 
{
    public User User {get;set;}
}

public class Customer
{
    public User User {get;set;}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.