Skip to content

Nested owned types migration error #18350

@hitaspdotnet

Description

@hitaspdotnet

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.

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");
    });
});
D:\MATA360\host\src\Hitasp.Tradegram.EntityFrameworkCore>dotnet ef database update
Applying migration '13980720151831_Initial_I'.
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.

   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(EntityType entityType)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity& type, ConfigurationSource configurationSource, Boolean allowOwned, Boolean thr
owOnQuery)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(String name, ConfigurationSource configurationSource, Boolean allowOwned, Boolean throwOnQue
ry)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceOwnershipBuilder.FindRelatedEntityType(String relatedTypeName, String navigationName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceOwnershipBuilder.HasOne(String relatedTypeName, String navigationName)
   at Hitasp.Tradegram.Migrations.Initial_I.<>c.<BuildTargetModel>b__2_133(ReferenceOwnershipBuilder b3) in D:\MATA360\host\src\Hitasp.Tradegram.EntityFrameworkCore\Migration
s\13980720151831_Initial_I.Designer.cs:line 3093
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceOwnershipBuilder.OwnsOne(String ownedTypeName, String navigationName, Action`1 buildAction)
   at Hitasp.Tradegram.Migrations.Initial_I.<>c.<BuildTargetModel>b__2_132(ReferenceOwnershipBuilder b2) in D:\MATA360\host\src\Hitasp.Tradegram.EntityFrameworkCore\Migration
s\13980720151831_Initial_I.Designer.cs:line 3063
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceOwnershipBuilder.OwnsOne(String ownedTypeName, String navigationName, Action`1 buildAction)
   at Hitasp.Tradegram.Migrations.Initial_I.<>c.<BuildTargetModel>b__2_131(ReferenceOwnershipBuilder b1) in D:\MATA360\host\src\Hitasp.Tradegram.EntityFrameworkCore\Migration
s\13980720151831_Initial_I.Designer.cs:line 3047
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.OwnsOne(String ownedTypeName, String navigationName, Action`1 buildAction)
   at Hitasp.Tradegram.Migrations.Initial_I.<>c.<BuildTargetModel>b__2_106(EntityTypeBuilder b) in D:\MATA360\host\src\Hitasp.Tradegram.EntityFrameworkCore\Migrations\1398072
0151831_Initial_I.Designer.cs:line 3026
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
   at Hitasp.Tradegram.Migrations.Initial_I.BuildTargetModel(ModelBuilder modelBuilder) in D:\MATA360\host\src\Hitasp.Tradegram.EntityFrameworkCore\Migrations\13980720151831_
Initial_I.Designer.cs:line 3019
   at Microsoft.EntityFrameworkCore.Migrations.Migration.<.ctor>b__4_0()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'Hitasp.Tradegram.Types.Location' cannot be added to the model because a weak entity type with the same name already exists.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions