简体   繁体   中英

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. However, it creates the following columns:

  • Id
  • Name
  • Value
  • DropDownField_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.

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.

That is whenever you insert an option, FieldId must reference an existing DropDownField AND an existing RadioButtonField.

That is at least weird.

I don't think this can/should be achieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM