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

Scaffolder throws error if junction table foreign key columns have identical names #26496

Closed
aboondeviant opened this issue Nov 1, 2021 · 2 comments
Assignees
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@aboondeviant
Copy link

In the new EF Core 6 Scaffolder junction tables are generated as many-to-many relationships and not as separate entities.

The scaffolder seems to be using the column names in the junction table to generate names of the navigation properties.
This will cause issues if you have multiple junction tables using the same column name (this can be quite common if a table has multiple junction tables to it self)

Expected behaviour:

It would be preferable if the scaffolder didn't fail but had a fallback for duplicate names (eg. Products and Products1).
This behavious is already used if scaffolded tables have identical names.

Stack trace:

System.InvalidOperationException: The property or navigation 'Products' cannot be added to the entity type 'Product (Dictionary<string, object>)' because a property or navigation with the same name already exists on entity type 'Pro
duct (Dictionary<string, object>)'.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddSkipNavigation(String name, MemberInfo memberInfo, EntityType targetEntityType, Boolean collection, Boolean onDependent, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Microsoft.EntityFrameworkCore.Metadata.IMutableEntityType.AddSkipNavigation(String name, MemberInfo memberInfo, IMutableEntityType targetEntityType, Boolean collection
, Boolean onDependent)
   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, ModelReverseEngineerOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOpti
ons codeOptions)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerabl
e`1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumera
ble`1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluarlize)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

Reproduction:

This works with any table that has 2 junction tables connected to it where the junction tables are using the same column names.

Repo with SQL script to create a database that generates an error.
https://github.com/aboondeviant/ef-core-junction-table-issue

Include provider and version information

EF Core version: 6.0.0-rtm.21519.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 6.0.0-rc2

@smitpatel
Copy link
Contributor

existingIdentifiers.AddRange(entityType.GetNavigations().Select(p => p.Name));

Need to add SkipNav names in identifiers list too.

smitpatel added a commit that referenced this issue Nov 3, 2021
… members of class

Resolves #26496

We had mechanism to generate unique identifiers to assign to skip navigations but the code to find existing identifiers lacked skip navigations. So skip navigation names were not unique-fied throwing error when trying to add skip navigation with same name
@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 Nov 3, 2021
@smitpatel smitpatel modified the milestones: 6.0.x, 6.0.1 Nov 9, 2021
@ghost ghost closed this as completed in 62dc53f Nov 9, 2021
@ajcvickers
Copy link
Member

FYI for those impacted by this issue: EF Core 6.0.1 is now available from NuGet.

This issue was closed.
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. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants