I have two related classes which share a common interface and are both stored in the same underlying database table. However, the Entity Framework generates one common class, where I really need the two distinct classes. How do I resolve this? Is it best to use a base class rather than an interface? How do I change the EF model to provide two classes mapped over one table?
Edit: the AccountType property determines the type of class; user or group.
Some simple code:
public interface IAccount { string Name { get; set; } AccountType AccountType { get; set; } } public class GroupAccount : IAccount { public string Name { get; set; } public GroupType GroupType { get; set; } public AccountType AccountType { get; set; } } public class UserAccount : IAccount { public string Username { get; set; } public string Password { get; set; } public string Name { get; set; } public AccountType AccountType { get; set; } }
Is this data discriminated? i.e. does AccountType define which type it is? If so:
The account type then should completely disappear from the Account object (unmap it if not). To get just the UserAccount records, you use
The Account class should probably be abstract in this model. The interface stuff can be added via a partial class – i.e. in a separate file, define:
etc
A reasonable walkthrough is here.