Navigation Property Already Exists
See original GitHub issueWhile the migration run with simple one to many relation, there are error occur with message The navigation property ‘Blog’ cannot be added to the entity type ‘Post’ because a property with the same name already exists on entity type ‘Post’.
Exception message:
The property or navigation 'Posts' cannot be added to the entity type 'Blog' because a property or navigation with the same name already exists on entity type 'Blog'.
Stack trace:
System.InvalidOperationException: The property or navigation 'Posts' cannot be added to the entity type 'Blog' because a property or navigation with the same name already exists on entity type 'Blog'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddNavigation(PropertyIdentity propertyIdentity, ForeignKey foreignKey, Boolean pointsToPrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.Navigation(Nullable`1 propertyIdentity, ConfigurationSource configurationSource, Boolean pointsToPrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.HasPrincipalToDependent(MemberInfo property, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.Navigations(Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, EntityType principalEntityType, EntityType dependentEntityType, Nullable`1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.Navigations(Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, Nullable`1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.PrincipalToDependent(MemberInfo property, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasMany[TRelatedEntity](Expression`1 navigationExpression)
at SQ.Data.Mappings.Mapping.QuestionMapping.MapEntity(ModelBuilder modelBuilder) in J:\..\..\..\src\..\Mapping\QuestionMapping.cs:line 22
at XPTO.Data.Mappings.SQDataModel.UseSQModel(DbContextOptionsBuilder dbContextBuilder) in J:\..\..\..\src\..\XXXDataModel.cs:line 91
at SQ.DataMigrator.App.SQDBContextFactory.CreateDbContext(String connectionString) in J:\..\..\..\src\..\XPTODBContextFactory.cs:line 17
at SQ.DataMigrator.App.SQDBContextFactory.CreateDbContext(String[] args) in J:\..\..\..\src\..\XPTODBContextFactory.cs:line 28
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Steps to reproduce
Run the migration commad
Models:
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Blog Blog { get; set; }
}
Mapping Classes: BlogMapping:
modelBuilder.Entity<Blog>().ToTable("Blog");
modelBuilder.Entity<Blog>().Property(g => g.BlogId).ValueGeneratedOnAdd().UseSqlServerIdentityColumn();
modelBuilder.Entity<Blog>().HasMany(s => s.Posts).WithOne(a => a.Blog);
PostMapping:
modelBuilder.Entity<Post>().ToTable("Post");
modelBuilder.Entity<Post>().Property(g => g.PostId).ValueGeneratedOnAdd().UseSqlServerIdentityColumn();
//modelBuilder.Entity<Post>().HasOne(a => a.Blog).WithMany(s => s.Posts);
Further technical details
EF Core version: 2.1.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer" Version=“2.1.0” Operating system: Microsoft Windows [Version 10.0.17134.112] IDE: Visual Studio Enterprise 2017, Version: 15.7.3
Maybe the issue already issued but I couldn’t find.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Property or navigation with the same name already exists ...
Property or navigation with the same name already exists on entity type - how to add foreign keys in migration scenario of Entity...
Read more >CoreStrings.ConflictingPropertyOrNavigation(Object, ...
The property or navigation '{member}' cannot be added to the entity type '{entityType}' because a property or navigation with the same name already...
Read more >C# – Property or navigation with the same name already ...
C# – Property or navigation with the same name already exists on entity type – how to add foreign keys in migration scenario...
Read more >Property or navigation with the same name already exists on ...
Property or navigation with the same name already exists on entity type - how to add foreign keys in migration scenario of Entity...
Read more >Entity Framework Core One-To-Many Relationships ...
The navigation property in this example is the Books property in the Author entity. It cannot be mapped to an equivalent data type...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For me it occur when I set blog to IsRequired in configuration and also use that in OnModelCreating to create y relation
check this link https://entityframeworkcore.com/knowledge-base/51919192/entity-framework-core-navigation-properties-error
@gsayem - The core of the issue is incomplete ConventionSet. You created ConventionSet based on core but you are missing Relational/SqlServer conventions, hence you are using incorrect type mapper too. These are EF internal services. You can implement one of them and replace in service provider but create a service correctly without using service provider is not easy task. Look at following code to get the convention set builder.
https://github.com/aspnet/EntityFrameworkCore/blob/7de3ebb8f8fd18167fa9c701cea168d6029ed2b6/src/EFCore.SqlServer/Metadata/Conventions/SqlServerConventionSetBuilder.cs#L88-L102
Note: above code is internal API and can be broken in future releases of EF Core. You either call the method directly or copy over the code.