efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Case Sensitive Database with Case Insensitive Duplicate Table throws Scaffold-DbContext error

Open ewmccarty opened this issue 8 years ago • 1 comments

For instances where the Database collation is Case Sensitive, two tables with Case Sensitive names [wkr].[em_ATT] and [wkr].[em_att] but using identical identity column generates and error during Scaffold-DbContext.

Exception message:
PM> Scaffold-DbContext -Connection "data source=MySqlServer;initial catalog=CSDb;integrated security=True;multipleactiveresultsets=True;encrypt=True;trustservercertificate=True;Connection Timeout=30;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context CSDBContext -DataAnnotations:$true -Force:$true -Project CSDBDM -StartupProject CSDBDM.Test 
System.ArgumentException: An item with the same key has already been added. Key: [wkr].[em_att].[Foundation Account Number]
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.GetColumns()
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(DbConnection connection, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ModelScaffolder.Generate(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String projectPath, String outputPath, String rootNamespace, String contextName, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.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)
An item with the same key has already been added. Key: [wkr].[em_att].[Foundation Account Number]

Steps to reproduce

SELECT collation_name FROM sys.databases WHERE name = 'CSDb' -- Latin1_General_BIN

EXECUTE sp_helpsort -- Server default collation Latin1-General, binary sort

USE [CSDb] GO drop table [wkr].[em_ATT]

/****** Object: Table [wkr].[em_ATT] Script Date: 2/21/2018 1:40:38 PM ******/ SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER ON GO

CREATE TABLE [wkr].[em_ATT]( [Foundation Account Number] [int] NOT NULL ) ON [PRIMARY]

GO

ALTER AUTHORIZATION ON [wkr].[em_ATT] TO SCHEMA OWNER GO

USE [CSDb] GO drop table [wkr].[em_att]

/****** Object: Table [wkr].[em_ATT] Script Date: 2/21/2018 1:40:38 PM ******/ SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER ON GO

CREATE TABLE [wkr].[em_att]( [Foundation Account Number] [int] NOT NULL ) ON [PRIMARY]

GO

ALTER AUTHORIZATION ON [wkr].[em_att] TO SCHEMA OWNER GO

PM> Scaffold-DbContext -Connection "data source=MySqlServer;initial catalog=CSDb;integrated security=True;multipleactiveresultsets=True;encrypt=True;trustservercertificate=True;Connection Timeout=30;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context CSDBContext -DataAnnotations:$true -Force:$true -Project CSDBDM -StartupProject CSDBDM.Test System.ArgumentException: An item with the same key has already been added. Key: [wkr].[em_att].[Foundation Account Number] at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key) at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.GetColumns() at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(DbConnection connection, IEnumerable1 tables, IEnumerable1 schemas) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, IEnumerable1 tables, IEnumerable1 schemas) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(String connectionString, IEnumerable1 tables, IEnumerable1 schemas, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ModelScaffolder.Generate(String connectionString, IEnumerable1 tables, IEnumerable1 schemas, String projectPath, String outputPath, String rootNamespace, String contextName, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable1 schemas, IEnumerable1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) An item with the same key has already been added. Key: [wkr].[em_att].[Foundation Account Number]

Further technical details

EF Core version: 2.0.1 Database Provider: Microsoft.EntityFrameworkCore.SqlServer v2.0.1 Operating system: Windows 10 IDE: Visual Studio 2017 15.5.6

ewmccarty avatar Feb 21 '18 19:02 ewmccarty

Verified this is fixed in 2.1.0-preview1 release as part of #9568 I ran above schema in SSMS and ran Scaffold-DbContext, it did not create entities due to missing PK but i believe its just stripped out database schema. It no longer throws above exception.

smitpatel avatar Feb 21 '18 21:02 smitpatel