首先在EF代码中继承公共基类

Ali*_*eza 7 entity-framework ef4-code-only ef-code-first

我正在将我的EF POCO项目转换为Code.我更改了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例外

仅供参考:我使用的是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!
}
Run Code Online (Sandbox Code Playgroud)

然后

[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"); }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Rap*_*aus 4

如果可能的话,尝试将其定义EntityBase为,然后将... 放在您不想映射的属性上应该可以解决问题。abstractNotMapped