我见过许多 EF POCO 示例,其中每个 POCO 类都继承了一个基 Entity 类或实现了一个 IEntity 接口。
我有点理解为什么要使用它,但我看不出它在所有情况下都有效,除非我遗漏了一些东西。
Entity 基类可能如下所示:
public class Entity
{
#region Primitive Properties
[Key]
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
[Timestamp]
public byte[] rowversion { get; set; }
#endregion
}
...具体的 POCO 类看起来像这样:
public class BlogCategory : Entity
{
#region Properties
[Required(ErrorMessage = "Category Name is required.")]
public string CategoryName { get; set; }
public virtual ICollection<Blog> BlogList { get; set; }
#endregion
}
当我的所有类都包含一个主键属性时这很好,但是当我有一个多对多关系时会发生什么?通常在多对多关系中,实体具有表示该实体主键的双重属性。
如:
public class ClaimQuestionAnswer : Entity <-- this will not work, will it?
{
[Key]
public int QuestionId { get; set; }
[Key]
public int AnswerId { get; set; }
public string Answer { get; set; }
public byte[] rowversion { get; set; }
}
这个特定的 POCO 不会继承基类吗?
任何澄清表示赞赏。
谢谢。