简体   繁体   中英

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. I'm using EF Core 3.0 against PostgreSQL database.

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.

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. See the following github issue for discussion:

https://github.com/dotnet/efcore/issues/12100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM