Efcore: DefiningEntityType not set on Owned entity

Created on 11 Feb 2020  ·  5Comments  ·  Source: dotnet/efcore


I'm using modelBuilder.Model.GetEntityTypes() to find types that are not owned and set their table name to the entity name to prevent pluralization, except for owned types to prevent table splitting. When checking modelBuilder.Model.GetEntityTypes(), Commication has its DefiningEntityType correctly set to Company while Address hasn't while both are defined as owned types of Company.

Steps to reproduce

I have an entity Address

    [Owned]
    public class Address
    {
        public string DepartmentName { get; set; }
        public virtual int? CountryId { get; set; }
        [StringLength(20)] 
        public string PostalCode { get; set; }
        [StringLength(120)] 
        public virtual string StreetName { get; set; }
        [StringLength(10)] 
        public virtual string StreetNumber { get; set; }
        public virtual Country Country { get; set; }
        public virtual string StateName { get; set; }
        public virtual string CityName { get; set; }
    }

and an entity Communication

    [Owned]
    public class Communication
    {
        public string WebsiteUrl { get; set; }
        public string EmailAddress { get; set; }
        public string FaxNumber { get; set; }
        public string MobileNumber { get; set; }
        public string Landline1 { get; set; }
        public string LandLine2 { get; set; }
        public string EmergencyNumber { get; set; }
        public string SkypeAddress { get; set; }
        public string LinkedInUrl { get; set; }
    }

both owned by an entity Company

    public class Company : DomainObjectWithId
    {
        public List<CompanyRoleType> CompanyRoles { get; set; }
        public string Code { get; set; }
        public virtual string Name { get; set; }
        public string Vat { get; set; }
        public Address Address { get; set; }
        public string ExternalReference { get; set; }
        [DefaultValue("1")] 
        public bool Enabled { get; set; }
        public Communication Communication { get; set; }
        public ICollection<Person> Persons { get; set; }
        public bool IsDeleted { get; set; }
    }

Company configuration

    public class CompanyConfiguration : IEntityTypeConfiguration<Company>
    {
        public void Configure(EntityTypeBuilder<Company> builder)
        {
            builder
                .Property(company => company.CompanyRoles)
                .HasJsonConversion();
            builder
                .OwnsOne(c => c.Communication);
            builder
                .OwnsOne(c => c.Address);
        }
    }