gpt4 book ai didi

c# - 如何使用 Entity Framework Core 3.0 创建所需的拥有类型

转载 作者:行者123 更新时间:2023-12-03 13:41:44 35 4
gpt4 key购买 nike

我正在努力使用 Entity Framework Core 创建一个不可为空/必需的拥有类型。
我正在对 PostgreSQL 数据库使用 EF Core 3.0。

我的值(value)对象:

    public class PersonName
{
public PersonName(string name)
{
this.Name = name;
}

public string Name { get; set; }
}

我的实体:
    public class Person
{
public int Id { get; set; }

public virtual PersonName FullName { get; set; }
}

我的实体配置:
    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");
});
}

值类型属性已成功保存到数据库中的“Person”表中,但尽管我使用的是“IsRequired()”方法,但该列仍然可以为空。

非常感谢!

最佳答案

因此,在深入研究同一问题之后,修复似乎是升级到 ef core 5(发布时)或手动编辑迁移。讨论见以下github问题:
https://github.com/dotnet/efcore/issues/12100

关于c# - 如何使用 Entity Framework Core 3.0 创建所需的拥有类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59275745/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com