我是否必须首先通过继承为 EF 代码实现 key 两次
我有一个抽象超类 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 中吗?
[仍然是一名学生,如果这个问题对你来说很愚蠢,我很抱歉:)]
I have an abstract superclass 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;
}
and 3 subclasses, for example 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 = "";
}
}
My question is: Do I have to declare the (foreign) keys twice for the Entity Framework to generate the database? or can I just put them in the superclass Datacontent only?
[Still a student so sorry if this question seems stupid to you :)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个属性只能实现一次(名称在整个层次结构中必须是唯一的)。您的派生类不应具有基类的任何属性,除非它们重写它们。
Each property can be implemented only once (name must be unique in the whole hierarchy). Your derived classes shouldn't have any properties from the base class unless they override them.