我有以下代码。
public class Person
{
public string LastName { get; set; }
}
public class Employee : Person
{
}
有配置
Map(p => p.MapInheritedProperties());
Property(p => p.LastName).HasMaxLength(100).IsRequired();
并想把它改成
public class Person
{
public virtual string LastName {get; set;}
}
public class Employee : Person
{
public override string LastName
{
get { return base.LastName; }
set
{
//add validation here or throw exception
base.LastName = value;
}
}
}
如果我运行应用程序,它会说模型已更改。 很好,我添加了一个数据库迁移,但它出错了:
The property 'LastName' is not a declared property on type 'Employee'.
Verify that the property has not been explicitly excluded from the model by using the
Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.
我需要添加什么样的映射才能使其工作? 我将 EF 4.3 与迁移结合使用。
感谢任何提示。
最佳答案
这似乎是 EF 的限制 - 属性可以在基类或子类上,但不能同时在两者上。参见 How to share common column names in a Table per Hierarchy (TPH) mapping
关于c# - 通过覆盖属性的继承映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11247127/