0

我有一個抽象類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有派生具體類,如通過繼承過於鏈EmployeeStudent

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); 
    } 

,我得到一個例外:

爲「EMIS.Entities.Domain.University.Person」已經被添加型的配置。要引用現有配置,請使用Entity()或ComplexType()方法。

很明顯,這是因爲在上下文中有雙重稱爲人員配置。我該如何解決這個問題?

回答

1

使用EntityTypeConfiguration<Employee>代替PersonConfiguration<Employee>

public class EmployeeConfiguration : EntityTypeConfiguration<Employee> 
{ 
    public EmployeeConfiguration() 
    { 
     ToTable("Employees"); 
    } 
} 
+0

這裏出現另一個問題:其他信息:無法確定類型「EMIS.Entities.Domain.University.Person」和「EMIS之間的關聯的主要端。 Entities.Domain.University.PersonContactIdentity」。這是否意味着該映射僅適用於Person,但不適用於派生類? –

+0

@IskanderRaimbayev:這一個,是另一個問題:http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr – Masoud