【发布时间】:2019-10-16 15:55:02
【问题描述】:
我有一个主实体 Profile,它的属性 Name 是一个值对象。 Name 对象有两个属性 First 和 Last。当我使用 Fluent API 将 Name objects 属性映射到 Profile 表中的列时,我指定它们是必需的。当我创建迁移时,它说 nullable 是真的。我认为这与在 EF Core 3.0 中拥有的实体现在是可选的这一事实有关,但我如何告诉 EF 它们实际上是必需的?
public class Profile
{
public Name Name { get; private set; }
...
}
public class Name
{
public string First { get; }
public string Last { get; }
...
}
public override void Configure(EntityTypeBuilder<Profile> builder)
{
base.Configure(builder);
builder.OwnsOne(
navigationExpression: p => p.Name,
buildAction: n =>
{
n.Property(n => n.First)
.HasColumnName("NameFirst")
.HasMaxLength(25)
.IsRequired();
n.Property(n => n.Last)
.HasColumnName("NameLast")
.HasMaxLength(25)
.IsRequired();
});
}
您能提供的任何帮助都会很棒。
【问题讨论】:
标签: c# entity-framework-core ef-core-3.0