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 :)]
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.