How can I create a Required Owned Type with Entity Framework Core 3.0

陌路散爱 提交于 2020-08-07 08:03:50

问题


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!

来源:https://stackoverflow.com/questions/59275745/how-can-i-create-a-required-owned-type-with-entity-framework-core-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!