实体框架查询表示找不到我从未指定的列的列

时间:2015-12-03 15:55:09

标签: c# .net entity-framework

我有一个表的以下数据库结构。

public class A {
    private C c;
    public void myMethod(B b) {
        b.callMethod();
    }
}

我的查询看起来像这样。

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

我的实体对象看起来像这样。

List<StepVariable> stepVariables;

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

return stepVariables;

当我运行时,我收到以下错误。

  

执行命令定义时发生错误。有关详细信息,请参阅内部异常。

     

{System.Data.SqlClient.SqlException(0x80131904):列名称'StepAction_Id'无效。
  在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action public class StepVariable { public int Id { get; set; } public int VariableId { get; set; } public int StepVariableId { get; set; } public string Value { get; set; } } 1 wrapCloseInAction)
  在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)
  在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&amp; dataReady)      在System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()      在System.Data.SqlClient.SqlDataReader.get_MetaData()      在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)      在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task&amp; task,Boolean asyncWrite,SqlDataReader ds)      在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,TaskCompletionSource 1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 c)      在System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1 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, DbCommandInterceptionContext 3操作,TInterceptionContext interceptionContext,Action 1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func 3执行)      在System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)      在System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)      在System.Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)      在System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)   ClientConnectionId:1db1a7c4-51de-49ca-856d-3a0eab5462da   错误号码:207,状态:1,等级:16}

有谁知道它获得StepAction_ID的位置?此数据库中的其他查询工作正常。

2 个答案:

答案 0 :(得分:0)

我还没有能力只将它添加为注释,但最可能的情况是您的数据库表中包含“StepAction_Id”列,这会使您的实体框架混乱,因为您的模型没有解决它

编辑:该表有什么样的关系?

答案 1 :(得分:0)

你正在使用自定义约定吗?因为默认情况下,Id应该用作密钥,但可能取决于EF的版本

看看https://msdn.microsoft.com/en-us/data/jj679962.aspx

无论如何,如果您可以重命名您的Id属性,请尝试使用StepVariableId查看错误是否消失

或者由于您没有发布整个班级,您是否有可能导航到StepAction实例的属性?