我正在尝试使用实体框架7和Fluid-API创建一个可选的外键。在EF v6.x中,我们可以选择使用.WithOptional
或.HasOptional
添加此功能,但我在EF 7中找不到任何等效功能。有什么想法吗?
Br,Inx
找到了答案。。可以将"false"作为参数传递给.IsRequired()。。例如:
EntityShortcut<ContentEntity>()
.HasMany(e => e.Children)
.WithOne(e => e.Parent)
.IsRequired();
这将是所需的关系
EntityShortcut<ContentEntity>()
.HasMany(e => e.Children)
.WithOne(e => e.Parent)
.IsRequired(false)
虽然这不是一个必要的关系。
FYI:
private static EntityTypeBuilder<T> EntityShortcut<T>() where T : class
{
return _modelBuilder.Entity<T>();
}