Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF Code First-如何为不同类型的父对象使用的子对象表指定外键名称_C#_Sql Server_Entity Framework_Ef Code First_Code First - Fatal编程技术网

C# EF Code First-如何为不同类型的父对象使用的子对象表指定外键名称

C# EF Code First-如何为不同类型的父对象使用的子对象表指定外键名称,c#,sql-server,entity-framework,ef-code-first,code-first,C#,Sql Server,Entity Framework,Ef Code First,Code First,我有一些物体看起来像这样: 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<FieldOpt

我有一些物体看起来像这样:

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; }
}
抽象公共类字段
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共整数序号{get;set;}
}
[表(“下拉字段”)]
公共类DropDownField:字段
{
公共虚拟列表选项{get;set;}
}
[表格(“RadioButtonField”)]
公共类RadioButtonField:字段
{
公共虚拟列表选项{get;set;}
}
公共类字段选项
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
在我的数据库中,它首先使用代码创建FieldOptions表。但是,它会创建以下列:

  • 身份证
  • 名字
  • 价值观
  • DropDownField\u Id
  • RadioButtonField\u Id
我想看到的是这个表中只有一个字段的Id,因为在不同类型的字段中,字段的Id必须是唯一的


有办法做到这一点吗?我已经做了一些搜索,但我一定不知道用于查找答案的正确搜索词。

imho,从关系数据库的角度来看,您想要的是一个列(Option.FieldId),它是两个表DropDownField和RadioButtonField的外键

也就是说,无论何时插入选项,FieldId都必须引用现有的DropDownField和现有的RadioButtonField

这至少很奇怪


我认为这是不可能/不应该实现的。

在您的情况下,您可以尝试将列表放在基类上。我可以这样做,但还有其他字段类型(例如TextBoxField)不需要包含选项。我只是没有将它们包括在示例中。对,这会很奇怪,但是Option.FieldId可能会引用字段表的Id。在当前数据库结构中,创建了三个表:Field、DropDownField和RadioButtonField。DropDownField或RadioButtonField中的任何Id在字段表中都有相应的Id。其他表只存储任何唯一的值。老实说,我很长一段时间都在寻找一个“优雅”的解决方案来解决这个问题,我甚至在脑海中把它放在一个队列中。但是,当我面对DBA的观点,特别是完整性约束时,我不再认为这是一个“阻塞”问题。