Efcore: System.ArgumentException: The string argument 'originalIdentifier' cannot be empty.

Created on 25 Sep 2020  路  22Comments  路  Source: dotnet/efcore

I am trying to scaffold an existing database using entity framework core, visual studio 2019, .net core 3.1

But I get the error "System.ArgumentException: The string argument 'originalIdentifier' cannot be empty."

I can't seem to point out what exactly is causing the failure

- Nuget command = Scaffold-DbContext "Server=10.3.**.**;database=G*ps;user id=***;password=********;Trusted_Connection=True;Integrated Security = false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entity

After build suceeded
i get this

System.ArgumentException: The string argument 'originalIdentifier' cannot be empty.
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CandidateNamingService.GenerateCandidateIdentifier(String originalIdentifier)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CandidateNamingService.GenerateCandidateIdentifier(DatabaseColumn originalColumn)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.<GetPropertyName>b__18_1(DatabaseColumn c)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpNamer`1.GetName(T item)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpUniqueNamer`1.GetName(T item)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.GetPropertyName(DatabaseColumn column)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitColumn(EntityTypeBuilder builder, DatabaseColumn column)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitColumns(EntityTypeBuilder builder, ICollection`1 columns)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTable(ModelBuilder modelBuilder, DatabaseTable table)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTables(ModelBuilder modelBuilder, ICollection`1 tables)
   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, 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 outputDbContextDir, String dbContextClassName, IEnumerable`1 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.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The string argument 'originalIdentifier' cannot be empty.

I would appreciate any help i can get,

Thank You

area-scaffolding closed-fixed community-contribution customer-reported type-bug

All 22 comments

I think you need to share your schema. Which database server version and edition?

-Database server version and edition = Microsoft SQL Server 2012 (SP3-GDR) (KB3194721) - 11.0.6248.0 (X64) Sep 23 2016 15:49:43 Copyright (c) Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor)

-Database schema - This is quite an extensive database with tables, views, and procedures

Can you provide the exact information you are looking out for on the database, or what exactly could cause the problem

You can script the schema using SSMS and share it with me privately.

Hello @ErikEJ , Sorry for the late response . The database is owned by an organization and requires a lot of approvals to share.
Is that the only way i can resolve this issue?

@aces32 - The error indicates that while scaffolding there was a column found which had empty string or white spaces as name. It may not be theoretically possible but what has happened. Unless we get schema for the table which caused it, there is no way for us to investigate the issue.

@aces32 Thanks for sharing the schema privately. @smitpatel is going to take a look.

As suspected earlier

CREATE TABLE [dbo].[LNG_BATCH](
    [   ] [float] NULL,
    [TRA_DATE] [datetime] NULL,
    [TRA_SEQ1] [float] NULL,
    [TRA_SEQ2] [float] NULL,
    [BRA_CODE] [float] NULL,
    [CUS_NUM] [float] NULL,
    [CUR_CODE] [float] NULL,
    [LED_CODE] [float] NULL,
    [SUB_ACCT_CODE] [float] NULL,
    [TELL_ID] [float] NULL,
    [EXT_INT_FLAG] [float] NULL,
    [DEP_CODE] [float] NULL,
    [DIS_CODE] [float] NULL,
    [TRA_AMT] [float] NULL,
    [DEB_CRE_IND] [float] NULL,
    [MAN_APP] [float] NULL,
    [MAN_REP] [float] NULL,
    [EXPL_CODE] [float] NULL,
    [VAL_DATE] [datetime] NULL,
    [INT_DATE] [datetime] NULL,
    [CAN_REA_CODE] [float] NULL,
    [DOC_ALP] [float] NULL,
    [DOC_NUM] [float] NULL,
    [CUR_PRI] [float] NULL,
    [EQU_TRA_AMT] [float] NULL,
    [ORIGT_BRA_CODE] [float] NULL,
    [ORIGT_TRA_DATE] [datetime] NULL,
    [ORIGT_TRA_SEQ1] [float] NULL,
    [ORIGT_TRA_SEQ2] [float] NULL,
    [DRA_ON_BRA_CODE] [float] NULL,
    [BANK_CODE] [float] NULL,
    [CITY_LOC_CODE] [float] NULL,
    [BLO_SEQ] [float] NULL,
    [DEB_INT] [nvarchar](255) NULL,
    [PEN_INT] [nvarchar](255) NULL,
    [CRE_INT] [nvarchar](255) NULL,
    [REMARKS] [nvarchar](255) NULL,
    [PRO_CODE] [float] NULL,
    [HO_TELL_ID] [nvarchar](255) NULL,
    [OFFICER_CODE] [nvarchar](255) NULL,
    [UPD_TIME] [float] NULL
) ON [PRIMARY]
GO

SqlServer allows white spaces as column name, we don't expect or handle it in scaffolding code. Hence we throw error that original identifier which is white spaces is empty.
@bricelam , @roji, @lauxjpn - Are there other databases which also support white spaces as identifiers?

@smitpatel SQLite does for sure.

Options here would be

  • Log a warning and ignore the column. Though if it is PK then it would also cascade to missing PK/FK constraints.
  • Log a warning and use "_" as identifier.

Are there other databases which also support white spaces as identifiers?

MySQL 8.0 and MariaDB 10.5 do not:

CREATE TABLE `WhiteSpaceTest` (
    ` ` int null, /* invalid */
    `a ` int null, /* invalid */
    ` a` int null, /* valid */
    ` a ` int null, /* invalid */
    `a b` int null /* valid */
) ENGINE=InnoDB;

We can do this in provider specific way by enforcing a non-empty identifier in DatabaseColumn directly. So provider can throw or ignore or put "_" as identifier in ProviderScaffoldingModelFactory.

Don't ignore the column. Wouldn't CSharpNamer already replace it with underscores? I don't even think we need to warn; getting a property with a bunch of underscores makes it pretty obvious what happened. 馃槈

In that case it is one line fix to stop checking for empty. Check.NotNull is more appropriate. Checked that CSharpUtilities will assign it "_" as name.

_Not a regression_ it has been like this since candidate naming service was introduced.

I will add a fix to EF Core Power Tools to create compensating column names in the meantime to unblock, as I assume the fix will be in 6.0 only. Happy to submit a PR.

Thanks everyone, i really appreciate the help.
I was able to scaffold one of the databases after renaming the empty named column on LNGBatch table.

I would also like to confirm if a locked table on SQL server could cause this issue, As i have another backup database without this LNGBatch table with the same issue.

I am currently checking all the tables for whitespace and would give feedback on the outcome

@ErikEJ We are patiently awaiting v6.0

Thanks Everyone , i found the empty column on the backup database, everything seems to be fine.

Once again, thanks

Wow Happy to see a fix for this.

@uzakari do you rely on empty column names?

I have implemented a fix for this in the latest daily build of EF Core Power Tools: https://github.com/ErikEJ/EFCorePowerTools/wiki/Release-notes

@aces32 @uzakari I'm also really curious why you use empty column names.

@ajcvickers the tables were created a long time ago; I am also curious as you are and surprised.

@ErikEJ A PR for 6.0 (i.e. main branch) would be much appreciated.

Was this page helpful?
0 / 5 - 0 ratings