简体   繁体   English

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

[英]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.

相关问题 Entity Framework Core 3.0中的拥有类型问题 - Owned types problem in Entity Framework Core 3.0 实体框架核心 - 拥有类型的修改键 - Entity Framework Core - Modified key on owned type 与 IdentityUser 一起使用时,Entity Framework Core 拥有的类型值 Object 抛出 required Primary Key to be defined 错误。 为什么? - Entity Framework Core Owned Type Value Object throws required Primary Key to be defined error when used with IdentityUser. Why? 如何在实体框架核心中使用泛型类型? - How can I use a generic type with entity framework core? 如何使用 Entity Framework Core 中跟踪的 [Owned] 属性获取导航属性 - How to get navigation properties with [Owned] attribute Tracked in Entity Framework Core 如何在 Entity Framework Core 3.0 中播种? - How to seed in Entity Framework Core 3.0? 在实体框架中,如何配置影子拥有的实体? - In Entity Framework how do I configure a shadow owned entity? 如何在 ef core 6 中配置引用另一个实体的拥有类型 - How to configure Owned type referencing another entity in ef core 6 实体框架核心:将拥有的属性与继承相结合 - Entity Framework Core: Combine owned properties with inheritance 指向拥有的实体类型时,为什么不能使用HasOne()? - Why can I not use HasOne() when pointing to an owned Entity Type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM