question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested owned types migration error

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Oct 12, 2019

@hitaspdotnet Yes, you should do that for both types. If everything is correct adding another migration should produce one without any database changes (though the snapshot would need to be fixed for every migration)

0reactions
hitaspdotnetcommented, Oct 12, 2019

@AndriySvyryd Thanks for response.

You would need to remove the b3.HasOne(“Hitasp.Tradegram.Types.Location”) line and the following calls.

for both entities?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Root & Nested owned type in multi entities migration ...
I getting an error, when I try to using a owned type in multi entities. I think I got the error because User...
Read more >
Owned Entity Types - EF Core
Configuring types as owned ... In most providers, entity types are never configured as owned by convention - you must explicitly use the...
Read more >
Nested field type | Elasticsearch Guide [8.9]
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >
Entity Framework Core 5 - Pitfalls To Avoid and Ideas to Try
In this post, we'll look at some pitfalls and ideas EF Core users like yourself may want to consider when developing an application....
Read more >
Migrating from EF6 to EF Core
Here is our story from migrating Bokio to EF Core. ... Complex Types are now Owned Entities. You need to change from x.ComplexType<Period>() ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found