简体   繁体   English

EF代码优先-如何为不同类型的父母使用的子对象表指定外键名称

[英]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: 但是,它将创建以下列:

  • Id ID
  • Name 名称
  • Value
  • DropDownField_Id DropDownField_Id
  • RadioButtonField_Id RadioButtonField_Id

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.

相关问题 EF Code First单个外键给多个父母 - EF Code First Single foreign key to multiple parents 如何在EF-Code-First中指定主键名称 - How to Specify Primary Key Name in EF-Code-First EF代码第一个外键相同的表 - EF Code First Foreign Key Same Table EF 4.3 Code First:具有复合主键和外键的每类型表(TPT) - EF 4.3 Code First: Table per Type (TPT) with Composite Primary Key and Foreign Key EF代码的第一个外键被忽略,得到“无效的列名” - EF code first foreign key ignored, getting “Invalid column name” 如何在EF数据注释中指定外表键? - How to specify foreign table key in EF Data Annotations? EF代码首先使用空查找表设置外键 - EF Code first set a foreign key with an empty lookup table EF的代码优先,如何在不设置数据库表之间外键的情况下定义导航属性关系 - Code first of EF, how to define navigation property relationship without setting foreign key between table in database 如何使用 EF 代码优先方法将单个记录添加到具有外键的表中 - how to add a single record in to table which has foreign key using EF code first approach EF Code First多对多:如何指定第三个表的名称并为其添加其他属性? - EF Code First many-to-many: how to specify name of th third table and add some other properties to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM