How do I add a shadow property to an owned type?

entity-framework-core owned-types

Question

Say I have a Customer entity with a list of owned types of Address. I also have an Office entity also with a list of owned types of Address.

In my OnModelCreating() method I have:

modelBuilder.Entity<Customer>().OwnsMany(s => s.Addresses, a =>
{
    a.Property<DateTime>("CreatedDate");
    a.Property<DateTime>("UpdatedDate");
}).ToTable("CustomerAddresses");

modelBuilder.Entity<Office>().OwnsMany(s => s.Addresses, a =>
{
    a.Property<DateTime>("CreatedDate");
    a.Property<DateTime>("UpdatedDate");
}).ToTable("OfficeAddresses");

I get an error of:

"Cannot use table 'Addresses' for entity type 'Customer.Addresses#Address' since it is being used for entity type 'Office.Addresses#Address' and there is no relationship between their primary keys.".

However, if I just have:

modelBuilder.Entity<Customer>().OwnsMany(s => s.Addresses).ToTable("CustomerAddresses");
modelBuilder.Entity<Office>().OwnsMany(s => s.Addresses).ToTable("OfficeAddresses");

it works.

So how do I add the shadow properties to this?

1
0
4/2/2020 12:32:41 AM

Popular Answer

The solution if found is:

    modelBuilder.Entity<Customer>().OwnsMany(s => s.Addresses, a =>
    {
        a.Property<DateTime>("CreatedDate");
        a.Property<DateTime>("UpdatedDate");
        a.ToTable("CustomerAddresses");
    });

    modelBuilder.Entity<Office>().OwnsMany(s => s.Addresses, a =>
    {
        a.Property<DateTime>("CreatedDate");
        a.Property<DateTime>("UpdatedDate");
        a.ToTable("OfficeAddresses");
    });
0
4/2/2020 12:50:22 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