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

342 Views Asked by At

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

There are 1 best solutions below

0
On

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");
    });