【发布时间】:2011-11-18 12:06:15
【问题描述】:
我有一个抽象超类 DataContent:
public abstract class DataContent
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Sheet")] //Foreign Key van Sheet
public int SheetId { get; set; }
public string Name { get; set; }
public DataContent(int i, string n)
{
Id = i;
Name = n;
}
和 3 个子类,例如 EmptyContent:
public class EmptyContent: DataContent
{
//TODO: Do these keys have to be here as well???
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Sheet")] //Foreign Key van Sheet
public int SheetId { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public EmptyContent (int i, string n) : base(i,n)
{
Text = "";
}
}
我的问题是:我是否必须为实体框架声明(外)键两次才能生成数据库?还是我可以只将它们放在超类 Datacontent 中?
[如果你觉得这个问题很愚蠢,我还是个学生很抱歉:)]
【问题讨论】:
标签: c# entity-framework-4.1 ef-code-first data-annotations subclass