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

c# entity-framework-core owned-types postgresql value-objects

Question

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!

1
0
12/10/2019 9:38:17 PM

Popular Answer

Turns out this is an issue.

[Required]/IsRequired() on properties of owned entities is ignored

It will be tackled here so please thumb-up. (:

0
12/11/2019 6:43:26 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow