entity-framework - 先继承EF代码中的通用基类

标签 entity-framework ef-code-first ef4-code-only

我首先将我的 EF POCO 项目转换为代码。我更改了 T4 模板,因此我的所有实体都使用基类 EntityBase ,这为他们提供了一些与持久性无关的通用功能。

如果我使用 [NotMapped] EntityBase 上的属性,所有实体都继承这个属性,我得到一个 The type 'X.X.Person' was not mapped ,对于我尝试与 EF 一起使用的任何类型。

如果我使用 [NotMapped]EntityBase 的所有属性上,我得到一个 EntityType 'EntityBase' has no key defined. Define the key for this EntityType异常(exception)

仅供引用:我使用 Ef 4.3.1

编辑:部分代码:

[DataContract(IsReference = true)]
public abstract class EntityBase : INotifyPropertyChanged, INotifyPropertyChanging
{
    [NotMapped]
    public virtual int? Key
    {
        get { return GetKeyExtractor(ConversionHelper.GetEntityType(this))(this); }
    }
    //other properties and methods!
}

进而
[DataContract(IsReference = true), Table("Person", Schema = "PER")]
public abstract partial class Person : BaseClasses.EntityBase, ISelfInitializer
{
    #region Primitive Properties
    private int? _personID;
    [DataMember,Key]
    public virtual int? PersonID
    {
        get{ return _personID; }
        set{ SetPropertyValue<int?>(ref _personID, value, "PersonID"); }
    }
}

对于这两个类,没有流畅的 api 配置。

最佳答案

尝试定义EntityBaseabstract ,如果可能的话,把 NotMapped ...在您不想映射的属性上应该可以解决问题。

关于entity-framework - 先继承EF代码中的通用基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12141665/

相关文章:

entity-framework - 如何将一个代码第一个对象中的两个属性映射到相同的父类型

code-first - 从 IQueryable 访问 DbContext

linq - 按名称检索实体对象 - LINQ to Entities,EF 4.0

c# - ef代码第一个principal和dependent的区别

c# - 重新附加实体图并检测集合更改

c# - 如何使用外键属性的自定义名称映射相关对象,例如 PostId 而不是 Post_Id

c# - 基于连接字符串创建实体上下文

entity-framework - .net core - WebApp 和 EF 数据层的分离不起作用

entity-framework - EF 4.1 Code First 中的 XML 数据类型

c# - 在 Entity Framework 中使用三元条件运算符或表达式