我首先将我的 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 配置。
最佳答案
尝试定义EntityBase
如abstract
,如果可能的话,把 NotMapped
...在您不想映射的属性上应该可以解决问题。
关于entity-framework - 先继承EF代码中的通用基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12141665/