My table has
DeviceLocationId
and
ChildDeviceLocationId
that point to the DeviceLocation table
I cannot generate the entity source code
System.InvalidOperationException: The foreign key {'DeviceLocationId'} cannot be added to the entity type 'Device' because a foreign key on the same properties already exists on entity type 'Device' and also targets the key {'DeviceLocationId'} on 'DeviceLocation'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.Validate(IReadOnlyList1 properties, Key principalKey, EntityType declaringEntityType, EntityType principalEntityType)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey..ctor(IReadOnlyList
1 dependentProperties, Key principalKey, EntityType dependentEntityType, EntityType principalEntityType, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddForeignKey(IReadOnlyList1 properties, Key principalKey, EntityType principalEntityType, Nullable
1 componentConfigurationSource, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Microsoft.EntityFrameworkCore.Metadata.IMutableEntityType.AddForeignKey(IReadOnlyList1 properties, IMutableKey principalKey, IMutableEntityType principalEntityType)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitForeignKey(ModelBuilder modelBuilder, DatabaseForeignKey foreignKey)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitForeignKeys(ModelBuilder modelBuilder, IList
1 foreignKeys)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitDatabaseModel(ModelBuilder modelBuilder, DatabaseModel databaseModel)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(DatabaseModel databaseModel, Boolean useDatabaseNames)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable1 schemas, IEnumerable
1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable
1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Scaffold-DbContext "Server=.;Database=Jail;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10 1903
IDE: Visual Studio 2019 16.3.7
Please share the schema of the two involved tables
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
For the people who land here through their favorite search engine, this is usually caused by two identical foreign keys. If you are getting this error, double-check all the foreign keys on your table to make sure they are not accidentally identical.
And to make the obvious conclusion to @govert-co-co comment. Just delete the FK that is duplicated and the issue should be solved.
For the people who land here through their favorite search engine, this is usually caused by two identical foreign keys. If you are getting this error, double-check all the foreign keys on your table to make sure they are not accidentally identical.
@govert-co-co Man your comment is just awesome. You will never know how many lives you saved. :D
If anyone else stumbles here and can't identify which relationships are duplicates this SO answer helped me.
Most helpful comment
For the people who land here through their favorite search engine, this is usually caused by two identical foreign keys. If you are getting this error, double-check all the foreign keys on your table to make sure they are not accidentally identical.