Using Root & Nested owned type in multi entities migration error

160 Views Asked by At

I getting an error, when I try to using a owned type in multi entities. I think I got the error because User has used the same type. Using same type in multi entities isn't supports by EF core?

System.InvalidOperationException: The entity type 'Hitasp.Tradegram.Types.Location' cannot be added to the model because a weak entity type with the same name already exists.

public class User : IEntity<Guid>
{
    public Guid Id { get; set; }
    public ContactInfo UserContactInfo { get; set; }
}

public class Listing : IEntity<Guid>
{
    public Guid Id { get; set; }
    public ContactInfo ListingContactInfo { get; set; }
}

public class ContactInfo : IOwnedType
{
    public string WebsiteUrl { get; set; }
    public Location GeoLocation { get; set; }
}

public class Location : IOwnedType
{
    public NetTopologySuite.Geometries.Point Coords { get; set; }
    public Address PhysicalAddress { get; set; }
}
public class Address : IOwnedType
{
    public string AddressLine1 { get; set; }
    public string ZipPostalCode { get; set; }
}
//User modelBuilder
userBuilder.OwnsOne(c => c.UserContactInfo, conInfo =>
{
    conInfo.ToTable("UsersContactInfo");
    conInfo.OwnsOne(ci => ci.GeoLocation, location =>
    {
        location.OwnsOne(x => x.PhysicalAddress, address =>
        {
            address.ToTable("UsersPhysicalAddresses");
        });
        location.Property(x => x.Coords).HasColumnType("geography");
        location.ToTable("UsersLocations");
    });
});
//Listing modelBuilder
listingBuilder.OwnsOne(c => c.ListingContactInfo, conInfo =>
{
    conInfo.ToTable("ListingsContactInfo");
    conInfo.OwnsOne(ci => ci.GeoLocation, location =>
    {
        location.OwnsOne(x => x.PhysicalAddress, address =>
        {
            address.ToTable("ListingsPhysicalAddresses");
        });
        location.Property(x => x.Coords).HasColumnType("geography");
        location.ToTable("ListingsLocations");
    });
});

file: 13980720151831_Initial_I.Designer.cs#line2808

modelBuilder.Entity("Hitasp.Tradegram.Authorization.Users.User", b =>
{
    b.OwnsOne("Hitasp.Tradegram.Types.ContactInfo", "UserContactInfo", b1 =>
    {
        b1.Property<Guid>("UserId");
        b1.Property<string>("WebsiteUrl");
        b1.HasKey("UserId");
        b1.ToTable("UserContactInfo");
        b1.HasOne("Hitasp.Tradegram.Authorization.Users.User")
            .WithOne("UserContactInfo")
            .HasForeignKey("Hitasp.Tradegram.Types.ContactInfo", "UserId")
            .OnDelete(DeleteBehavior.Cascade);

        b1.OwnsOne("Hitasp.Tradegram.Types.Location", "GeoLocation", b2 =>
             {
                b2.Property<Guid>("ContactInfoUserId");
                b2.Property<Point>("Coords")
                    .HasColumnType("geography");

                b2.HasKey("ContactInfoUserId");
                b2.ToTable("UserLocations");

                b2.HasOne("Hitasp.Tradegram.Types.ContactInfo")
                    .WithOne("GeoLocation")
                    .HasForeignKey("Hitasp.Tradegram.Types.Location", "ContactInfoUserId")
                    .OnDelete(DeleteBehavior.Cascade);

                b2.OwnsOne("Hitasp.Tradegram.Types.Address", "PhysicalAddress", b3 =>
                    {
                        b3.Property<Guid>("LocationContactInfoUserId");
                        b3.Property<string>("AddressLine1");
                        b3.Property<string>("ZipPostalCode");
                        b3.HasKey("LocationContactInfoUserId");
                        b3.ToTable("UserPhysicalAddresses");

                        b3.HasOne("Hitasp.Tradegram.Types.Location")
                            .WithOne("PhysicalAddress")
                            .HasForeignKey("Hitasp.Tradegram.Types.Address", "LocationContactInfoUserId")
                            .OnDelete(DeleteBehavior.Cascade);
                    });
            });
        });
});

file: 13980720151831_Initial_I.Designer.cs#line3019

modelBuilder.Entity("Hitasp.Tradegram.Listings.Listing", b =>
{
    b.OwnsOne("Hitasp.Tradegram.Types.ContactInfo", "ListingContactInfo", b1 =>
    {
        b1.Property<Guid>("ListingId");
        b1.Property<string>("WebsiteUrl");
        b1.HasKey("ListingId");
        b1.ToTable("ListingContactInfo");
        b1.HasOne("Hitasp.Tradegram.Listings.Listing")
            .WithOne("ListingContactInfo")
            .HasForeignKey("Hitasp.Tradegram.Types.ContactInfo", "ListingId")
            .OnDelete(DeleteBehavior.Cascade);

        b1.OwnsOne("Hitasp.Tradegram.Types.Location", "GeoLocation", b2 =>
             {
                b2.Property<Guid>("ContactInfoListingId");
                b2.Property<Point>("Coords")
                    .HasColumnType("geography");

                b2.HasKey("ContactInfoListingId");
                b2.ToTable("ListingLocations");

                b2.HasOne("Hitasp.Tradegram.Types.ContactInfo")
                    .WithOne("GeoLocation")
                    .HasForeignKey("Hitasp.Tradegram.Types.Location", "ContactInfoListingId")
                    .OnDelete(DeleteBehavior.Cascade);

                b2.OwnsOne("Hitasp.Tradegram.Types.Address", "PhysicalAddress", b3 =>
                    {
                        b3.Property<Guid>("LocationContactInfoListingId");
                        b3.Property<string>("AddressLine1");
                        b3.Property<string>("ZipPostalCode");
                        b3.HasKey("LocationContactInfoListingId");
                        b3.ToTable("ListingPhysicalAddresses");

                        b3.HasOne("Hitasp.Tradegram.Types.Location")
                            .WithOne("PhysicalAddress")
                            .HasForeignKey("Hitasp.Tradegram.Types.Address", "LocationContactInfoListingId")
                            .OnDelete(DeleteBehavior.Cascade);
                    });
            });
        });
});

Further technical details

EF Core version: 2.2.6 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 2.2 Operating system: Windows 10 IDE: Rider 2019.2.2

1

There are 1 best solutions below

0
On

With Andriy (from EF's team) comment in github my problem was solved

You would need to remove the b3.HasOne("Hitasp.Tradegram.Types.Location") line and the following calls. Or upgrade to 3.0.0 and use the WithOwner method instead.