如果我在TPH配置中有一个基类和两个派生类,如何在流畅的API中配置派生类的标量属性(例如字符串的长度)?
实施例: 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 ?**
}
}
答案 0 :(得分:2)
您可以按照配置常规实体的方式配置派生实体 - 使用单独的流畅配置,例如
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);
// ...
}
}