我有一个问题,我不知道最好的解决方案。希望有人在这里可以帮助=)流利的映射继承
我想解决的是: 我们必须在系统,个人和组织的用户类型。 我想要为这两个共享登录表(即用户可能不知道他们是哪种类型的用户,他们只是涉及到用户名和密码)。 所以我创建了一个用户名和密码的登录表。但是我需要知道登录人是谁,因此我需要提及任何人或组织。
考虑下面的类(简体):
public class Login
{
public string Username {get; set;}
public string Password {get;set;}
}
public class LoginPerson : Login
{
public Person Person {get;set;}
}
public class LoginOrg : Login
{
public Organization Organization {get;set;}
}
public class Person
{
public LoginPerson LoginPerson {get;set;}
//Lots of other properties. Removed for simplicity
}
public class Organization
{
public LoginOrg LoginOrg {get;set;}
//Lots of other properties. Removed for simplicity
}
的人员配置设置如下:
所有的public class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
HasRequired(p => p.LoginPerson).WithRequiredPrincipal(p => p.Person);
}
}
首先,这是行不通的。我收到一个异常说 “System.Data.EntityCommandCompilationException:System.Data.EntityCommandCompilationException:准备命令定义时发生错误。请参阅内部异常的详细信息---> System.Collections.Generic.KeyNotFoundException:给定的键没有出现在字典中..“ 所以我的第一个问题是为什么这不工作? 我的第二个问题是:哪种策略最适合这种继承? TPT,TPH还是TPC?
对不起,不包括主键。当我简化课程时,我将它们移除了。所有的类都有一个Id列,这是一个PK。循环引用是有意的,因为它们是导航属性。但是,我解决了异常问题。而我的第二个问题可以通过google =回答) – Zaphod