Nate
Nate

Reputation: 847

Entity Framework query says column not found for a column I never specified

I have the following database structure for a table.

CREATE TABLE [dbo].[StepVariables]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [VariableId] [int] NOT NULL,
    [Value] [varchar](max) NOT NULL,
    [StepVariableId] [int] NOT NULL,
    CONSTRAINT [PK_StepVariables] 
        PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

My query looks like this.

List<StepVariable> stepVariables;

using (HHEDataContext context = new HHEDataContext("HHEDatabase"))
{
    stepVariables = (from sv in context.StepVariables
                     where sv.StepId == stepId
                     select sv).ToList();
}

return stepVariables;

My entity object looks like this.

public class StepVariable
{
    public int Id { get; set; }
    public int VariableId { get; set; }
    public int StepVariableId { get; set; }
    public string Value { get; set; }
}

When I run this, I get the following error.

An error occurred while executing the command definition. See the inner exception for details.

{System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'StepAction_Id'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext1 c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func3 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) ClientConnectionId:1db1a7c4-51de-49ca-856d-3a0eab5462da Error Number:207,State:1,Class:16}

Does anyone know where it is getting StepAction_ID? Other queries in this database work just fine.

Upvotes: 3

Views: 4599

Answers (1)

Not Important
Not Important

Reputation: 804

are you using a custom convention? because by default Id should be used as key, but maybe depend from the version of EF

take a look here https://msdn.microsoft.com/en-us/data/jj679962.aspx

anyway if you can rename your Id property, try using StepVariableId to see if the error goes away

or since you didn't posted the whole class, did you have maybe a navigable property to a StepAction instance?

Upvotes: 0

Related Questions