我使用 EF 4.2(数据库优先)和 DBContext T4 模板来创建我的 POCO 类。这很好用,但现在我偶然发现了一个抽象基类的问题:
我需要我的 T4 生成的 POCO 类从具有抽象属性的自定义抽象类继承:
//my abstract base class (shortened)
public abstract class BaseClass {
public abstract int? Property1 { get; set; }
public abstract int? Property2 { get; set; }
// a lot of methods that work with above properties
}
//my T4 generated entity (shortened)
public partial class Entity {
public Nullable<int> Property1 { get; set; }
public Nullable<int> Property2 { get; set; }
}
//and a partial class to make the T4 entity inherit from my abstract class
public partial class Entity : BaseClass {
}
我的问题是编译器不会构建它,因为 T4 类中的属性没有标记为“覆盖”。LINQ 2 SQL 可以选择更改实体属性的继承修饰符,但我在 EF 模型设计器中找不到此选项。
有没有办法告诉 EF 模型设计器和 T4 模板将某些属性标记为覆盖(当然,我可以更改生成的 C# 代码,但是当 T4 再次运行时,这些更改会被覆盖)?有没有其他方法可以使这个编译和工作?
非常感谢,〜萨克斯
更新 1:修正了一个错字。