我有一个抽象类Person
。
public abstract class Person : Entity
{
public string PassportFirstName { get; set; }
public string PassportLastName { get; set; }
public string InternationalFirstName { get; set; }
public string InternationalLastName { get; set; }
public PersonSocialIdentity SocialIdentity { get; set; }
public PersonContactIdentity ContactIdentity { get; set; }
public DateTime ? BirthDate { get; set; }
protected Person()
{
}
}
来自Person
的派生具体课程包括Employee
和Student
public class Employee : Person
{
public Employee() : base()
{
}
}
由继承链接的配置类:
public abstract class PrincipalEntityConfiguration<T>
: EntityTypeConfiguration<T> where T : Entity
{
protected PrincipalEntityConfiguration()
{
HasKey(p => p.Id);
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
public abstract class PersonConfiguration<T> : PrincipalEntityConfiguration<Person> where T : Person
{
protected PersonConfiguration()
{
HasRequired(p=>p.ContactIdentity).WithRequiredPrincipal();
HasRequired(p=>p.SocialIdentity).WithRequiredPrincipal();
}
}
public class EmployeeConfiguration : PersonConfiguration<Employee>
{
public EmployeeConfiguration()
{
ToTable("Employees");
}
}
他们在上下文中被调用:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new EmployeeConfiguration());
modelBuilder.Configurations.Add(new StudentConfiguration());
base.OnModelCreating(modelBuilder);
}
我得到了一个例外:
类型&#39; EMIS.Entities.Domain.University.Person&#39;的配置。已被添加。要引用现有配置,请使用Entity()或ComplexType()方法。
显然,它发生的原因是在上下文中有双重称为人员配置。我该如何解决这个问题?
答案 0 :(得分:1)
使用EntityTypeConfiguration<Employee>
代替PersonConfiguration<Employee>
:
public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
public EmployeeConfiguration()
{
ToTable("Employees");
}
}