[英]EF Code First - How do you specify foreign key name for child object table used by different type of parents
I've got some objects that look like this: 我有一些看起来像这样的对象:
abstract public class Field
{
public int Id { get; set; }
public string Name { get; set; }
public int Ordinal { get; set; }
}
[Table("DropDownField")]
public class DropDownField : Field
{
public virtual List<FieldOption> Options { get; set; }
}
[Table("RadioButtonField")]
public class RadioButtonField : Field
{
public virtual List<FieldOption> Options { get; set; }
}
public class FieldOption
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
In my database, it ends up creating the a FieldOptions table using Code First. 在我的数据库中,最终使用Code First创建了一个FieldOptions表。 However, it creates the following columns:
但是,它将创建以下列:
What I'd like to see is just one Field_Id in this table since the Id of a field has to be unique across the different types of fields. 我想看到的只是此表中的一个Field_Id,因为字段的ID在不同类型的字段中必须是唯一的。
Is there a way to do this? 有没有办法做到这一点? I've done some searching but I must not know the right search terms to use to find the answer.
我已经做了一些搜索,但是我一定不知道要使用正确的搜索词来找到答案。
imho, what you want, from a relational database point of view, is a column (Option.FieldId) being a foreign key to 2 tables DropDownField and RadioButtonField. 恕我直言,从关系数据库的角度来看,您想要的是一列(Option.FieldId),它是2个表DropDownField和RadioButtonField的外键。
That is whenever you insert an option, FieldId must reference an existing DropDownField AND an existing RadioButtonField. 也就是说,无论何时插入选项,FieldId都必须引用现有的DropDownField和现有的RadioButtonField。
That is at least weird. 那至少很奇怪。
I don't think this can/should be achieved. 我认为这不可能/应该实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.