【发布时间】:2012-06-28 14:42:33
【问题描述】:
我有以下代码。
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;
}
}
}
如果我运行应用程序,它会说模型已更改。 好的,我添加了一个 DB Migration,但是它出错了:
“姓氏”属性不是“员工”类型的声明属性。
使用
忽略方法或 NotMappedAttribute 数据注释。确保它是一个有效的原始属性。
我需要添加什么样的映射才能使其工作? 我将 EF 4.3 与迁移一起使用。
感谢任何提示。
【问题讨论】:
-
People 类真的只有一个属性 LastName 吗?当我尝试重现时,我需要指定一个键。
-
如果
Person上的LastName被声明为virtual会有区别吗?
标签: c# .net entity-framework entity-framework-4 ef-code-first