[英]How can I create a Required Owned Type with Entity Framework Core 3.0
I'm struggling creating a non-nullable/required Owned Type with Entity Framework Core.我正在努力使用 Entity Framework Core 创建一个不可为空/必需的拥有类型。 I'm using EF Core 3.0 against PostgreSQL database.
我正在对 PostgreSQL 数据库使用 EF Core 3.0。
My value object:我的价值对象:
public class PersonName
{
public PersonName(string name)
{
this.Name = name;
}
public string Name { get; set; }
}
My entity:我的实体:
public class Person
{
public int Id { get; set; }
public virtual PersonName FullName { get; set; }
}
My entity configuration:我的实体配置:
public void Configure(EntityTypeBuilder<Person> builder)
{
builder.ToTable(nameof(Person));
builder.HasKey(person => person.Id);
builder.OwnsOne(person => person.FullName, personName =>
{
personName.Property(pn => pn.Name).IsRequired().HasColumnName("FullName");
});
}
The value type property is successfully persisted into the 'Person' table in the database but the column apears to be nullable despite that I'm using 'IsRequired()' method.值类型属性已成功保存到数据库中的“Person”表中,但尽管我使用的是“IsRequired()”方法,但该列仍然可以为空。
Thanks a lot!非常感谢!
So after some digging into the same problem the fix appears to be upgrade to ef core 5 (when its released) or manually edit the migrations.因此,在深入研究同一问题之后,修复似乎是升级到 ef core 5(发布时)或手动编辑迁移。 See the following github issue for discussion:
讨论见以下github问题:
https://github.com/dotnet/efcore/issues/12100 https://github.com/dotnet/efcore/issues/12100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.