yoursvsr
yoursvsr

Reputation: 121

EF Code First: Fluent API Configuration - How to configure properties of subclasses in TPH?

If I have a base class and two derived classes in a TPH configuration, how to configure the scalar properties of the derived classes (e.g. length of string) in fluent API?

Example: public enum PersonType { Employee, Manager }

public abstract class Person 
{ 
    public string FirstName {get; set;} 
    public string LastName {get; set;} 
} 

public class Employee : Person 
{ 
    public string Designation {get; set;} 
} 

public class Manager : Person 
{ 
    public string Division {get; set;} 
} 

//fluent api config 
internal class PersonConfig : EntityTypeConfiguration<Person>
{ 
    public PersonConfig() 
    { 
        Property(p => p.FirstName) .HasMaxLength(250); 
        Property(p => p.LastName) .HasMaxLength(250); 
        Map<Employee>(e => e.Requires(x => x.PersonType).HasValue()); 
        **//how to configure Designation ?** 
        Map<Manager>(m => m.Requires(y => y.PersonType).HasValue()); 
        **//how to configure Division ?** 
    } 
}

Upvotes: 2

Views: 164

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205599

You configure derived entities the same way you configure regular entities - by using separate fluent configuration, e.g.

internal class EmployeeConfig : EntityTypeConfiguration<Employee>
{ 
    public EmployeeConfig() 
    { 
        Property(e => e.Designation).HasMaxLength(250);
        // ... 
    } 
}

internal class ManagerConfig : EntityTypeConfiguration<Manager>
{ 
    public ManagerConfig() 
    { 
        Property(m => m.Division).HasMaxLength(250);
        // ... 
    } 
}

Upvotes: 2

Related Questions