c# - 通过覆盖属性的继承映射

标签 c# .net entity-framework entity-framework-4 ef-code-first

我有以下代码。

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/

相关文章:

c# - 在声明点使用变量作为输出参数

c# - 压栈方式

c# - 如何在 C# 泛型中指定可以从字符串构造的 T ? (通用类型约束)

.net - 在 Visual Studio 中运行 Webpack-dev-server - .net core 项目

c# - 多线程线程安全读/写锁定的最佳 C# 解决方案?

c# - LINQ Join 仅适用于 Orderby

c# - 使用 SqlBulkCopy 在 asp.net 中上传 Excel 文件

c# - ExecuteCore() vs OnActionExecuting(ActionExecutingContext filterContext)?

asp.net-mvc - 测试 : Entity Framework by faking context

c# - EntityFramework 使用另一个值更新实体?