EF核心:一对一卫星'表需要自己的密钥吗?

时间:2016-09-03 06:56:13

标签: entity-framework-core

这是我的情景:

public class Main
{
    public Guid Id { get; set; }
    public string Something { get; set; }
    public MoreDetail OptionalDetail { get; set; }
}

public class MoreDetail
{
    public Guid MainId { get; set; }
    public Main Main { get; set; }
    public string MoreInfo { get; set; }
}

// model building code:

mainBuilder.HasOne(m => m.OptionalDetail).WithOne(d => d.Main).IsRequired(false);

拟:

  • MoreDetail是一个可选的孩子'表格Main
  • 主键是MainId;它不需要自己的ID

实际值:

The entity type 'Main' requires a key to be defined.

尝试:

  • 添加moreDetailBuilder.HasKey(d => d.MainId)。产生相同的错误。
  • .HasForeignKey<MoreDetail>(d => d.MainId)添加到Main模型构建器代码。给出The foreign key {'MainId'} on entity type 'MoreDetail' cannot be marked as optional because it does not contain any property of a nullable type. Any foreign key can be marked as required, but only foreign keys with at least one property of a nullable type and which is not part of primary key can be marked as optional.
  • 改为使用.IsRequired(true)。密钥设置和插入记录工作正常..但在查询时,EF Core生成INNER JOIN,这意味着如果没有记录则不返回任何内容。 :&#39;(

官方文档示例显示子/卫星表具有自己的主键。这似乎没必要;数据完全由外键识别,不是吗?

0 个答案:

没有答案