这是我的情景:
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;(官方文档示例显示子/卫星表具有自己的主键。这似乎没必要;数据完全由外键识别,不是吗?