Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unblocking duplicate foreign keys when reverse engineering an existing database (SQL Server) #25197

Closed
Tracked by #22948
ErikEJ opened this issue Jul 4, 2021 · 2 comments · Fixed by #25378
Closed
Tracked by #22948
Assignees
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-enhancement
Milestone

Comments

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 4, 2021

I frequently see this error when users of SQL Server databases reverse engineer an existing database.

One possible solution could be to let the SQL Server scaffolding factory ignore the "second" foreign key. (and maybe just log a warning)

Thoughts?

System.InvalidOperationException: The foreign key {'Activity'} cannot be added to the entity type 'MyChild' because a foreign key on the same properties already exists on entity type 'MyChild' and also targets the key {'Oid'} on 'MyParent'.
   at void Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.Validate(IReadOnlyList<Property> properties, Key principalKey, EntityType declaringEntityType, EntityType principalEntityType)
   at new Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey(IReadOnlyList<Property> dependentProperties, Key principalKey, EntityType dependentEntityType, EntityType principalEntityType, ConfigurationSource configurationSource)
   at ForeignKey Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddForeignKey(IReadOnlyList<Property> properties, Key principalKey, EntityType principalEntityType, ConfigurationSource? componentConfigurationSource, ConfigurationSource configurationSource)
   at IMutableForeignKey Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Microsoft.EntityFrameworkCore.Metadata.IMutableEntityType.AddForeignKey(IReadOnlyList<IMutableProperty> properties, IMutableKey principalKey, IMutableEntityType principalEntityType)
   at IMutableForeignKey Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitForeignKey(ModelBuilder modelBuilder, DatabaseForeignKey foreignKey)
   at ModelBuilder Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitForeignKeys(ModelBuilder modelBuilder, IList<DatabaseForeignKey> foreignKeys)
   at ModelBuilder Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitDatabaseModel(ModelBuilder modelBuilder, DatabaseModel databaseModel)
   at IModel Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(DatabaseModel databaseModel, ModelReverseEngineerOptions options)

EF Core version: 5, 6
Database provider: often Microsoft.EntityFrameworkCore.SqlServer

@ErikEJ
Copy link
Contributor Author

ErikEJ commented Jul 12, 2021

@AndriySvyryd @ajcvickers What solution are you proposing - maybe I could do a PR?

@AndriySvyryd
Copy link
Member

Ignoring the second FK and logging a warning would be acceptable.

ErikEJ pushed a commit to ErikEJ/EntityFramework that referenced this issue Jul 31, 2021
ErikEJ pushed a commit to ErikEJ/EntityFramework that referenced this issue Aug 1, 2021
smitpatel pushed a commit that referenced this issue Aug 10, 2021
@smitpatel smitpatel self-assigned this Aug 10, 2021
@smitpatel smitpatel modified the milestones: Backlog, 6.0.0 Aug 10, 2021
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 10, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc1 Aug 12, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc1, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants