gpt4 book ai didi

c# - Entity Framework 查询说找不到我从未指定的列的列

转载 作者:行者123 更新时间:2023-11-30 18:22:22 26 4
gpt4 key购买 nike

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

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;

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

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

当我运行它时,出现以下错误。

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)<br/>
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, DbCommandInterceptionContext
1 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}

有谁知道它从哪里获取 StepAction_ID?此数据库中的其他查询工作正常。

最佳答案

您使用的是自定义约定吗?因为默认情况下 Id 应该用作键,但可能取决于 EF 的版本

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

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

或者由于您没有发布整个类(class),您是否有 StepAction 实例的可导航属性?

关于c# - Entity Framework 查询说找不到我从未指定的列的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34070444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com