Skip to content

Renamed column causes Invalid Column Name error #863

@FIASJason

Description

@FIASJason

My database has a table Designations, which I've renamed to Templates. When I attempt to get a record from this table, EF Core throws a SQLException with the error message Invalid column name 'TemplateId'.

There is no corresponding column named TemplateId in this table (although there are in other related tables). It's most likely a configuration issue on my end, but I have no idea where to go from here to troubleshoot. Any assistance you can provide is greatly appreciated.

My table definition is:

CREATE TABLE [dbo].[Designations](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](128) NOT NULL,
	[BasedOn_Id] [int] NULL,
	[Description] [nvarchar](1024) NULL,
	[Title] [nvarchar](128) NULL,
	[Department] [nvarchar](64) NULL,
	[LogonScript] [nvarchar](1024) NULL,
	[OrganisationalUnit] [int] NOT NULL,
	[AccountType_Id] [int] NOT NULL,
	[DatabaseField] [nvarchar](10) NULL,
	[NotificationEmail] [nvarchar](max) NULL,
	[ExportLocation] [nvarchar](230) NULL,
	[Assignable] [bit] NOT NULL,
	[SchoolDbGroups] [nvarchar](600) NULL,
	[AllowFinancialDataAccess] [bit] NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

My renaming settings:

        // Rename tables with 'Designation' to 'Template'
        if (name.Contains("Designation"))
            return name.Replace("Designation","Template");

        // Rename columns with 'Designation' to 'Template'
        if (column.NameHumanCase.Contains("Designation"))
            column.NameHumanCase = column.NameHumanCase.Replace("Designation", "Template");

The code and query generated by EF Core:

var template = db.Templates.First();

      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT TOP(1) [d].[Id], [d].[AccountType_Id], [d].[AllowFinancialDataAccess], [d].[Assignable], [d].[BasedOn_Id], [d].[DatabaseField], [d].[Department], [d].[Description], [d].[ExportLocation], [d].[LogonScript], [d].[Name], [d].[NotificationEmail], [d].[OrganisationalUnit], [d].[SchoolDbGroups], [d].[TemplateId], [d].[Title]
      FROM [dbo].[Designations] AS [d]

And the error:

fail: 26/06/2025 08:20:43.438 CoreEventId.QueryIterationFailed[10100] (Microsoft.EntityFrameworkCore.Query)
      An exception occurred while iterating over the results of a query for context type 'My.Model.MyDbContext'.
      Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'TemplateId'.
         at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
         at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
         at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
         at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
         at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
         at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
         at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
         at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
         at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
         at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator)
         at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
      ClientConnectionId:45e09d85-4059-4c55-bc9e-71aca4577cb4
      Error Number:207,State:1,Class:16
  • Microsoft SQL Server 2022
  • Entity Framework Core SQL Server 9.0.5
  • Reverse POCO Generator v3.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions