0

I have an abstract class 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()
    {

    }
}

And from Person there are derived concrete classes like Employee and Student

public class Employee : Person
{
    public Employee() : base()
    {
    }
}

And configuration classes chained by inheritance too:

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

And they are called in context :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new EmployeeConfiguration());
        modelBuilder.Configurations.Add(new StudentConfiguration());
        base.OnModelCreating(modelBuilder);
    }

and I got an exception:

A configuration for type 'EMIS.Entities.Domain.University.Person' has already been added. To reference the existing configuration use the Entity() or ComplexType() methods.

Clearly, it happens because in context there are doubly called Person Configuration. How can I fix this problem?

1 Answer 1

1

Use EntityTypeConfiguration<Employee> instead PersonConfiguration<Employee>:

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
    public EmployeeConfiguration()
    {
       ToTable("Employees");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Here arise another problem : Additional information: Unable to determine the principal end of an association between the types 'EMIS.Entities.Domain.University.Person' and 'EMIS.Entities.Domain.University.PersonContactIdentity'. Does it mean that mapping works only for Person but not for derived classes?
@IskanderRaimbayev: this one, is another problem: stackoverflow.com/questions/6531671/…

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.