gpt4 book ai didi

c# - "Invalid column name"使用 EntityFramework.Extended 执行更新时出错

转载 作者:行者123 更新时间:2023-11-30 17:47:28 25 4
gpt4 key购买 nike

我正在使用 EntityFramework.Extended库做一组批量更新。我正在更新的表格的重要部分如下所示:

CREATE TABLE [dbo].[BoosterTargetLog]
(
[Id] BIGINT NOT NULL identity PRIMARY KEY,
[CustomerUserId] INT NULL,
CookieId uniqueidentifier not null,
CONSTRAINT [FK_BoosterTargetLog_ToOrganizationCookie] FOREIGN KEY (CookieId) REFERENCES [OrganizationCookie]([CookieId])
)

C# 更新代码如下所示:

var query = db.BoosterTargetLogs
.Where(x =>
x.OrganizationCookie.OrganizationId == organizationId &&
x.CookieId == cookieId &&
x.CustomerUserId == null);

var updated = await query.UpdateAsync(x => new BoosterTargetLog
{
CustomerUserId = customerUserId
});

生成的 SQL 更新代码如下所示:

exec sp_executesql N'UPDATE [dbo].[BoosterTargetLog] SET 
[CustomerUserId] = @p__update__0
FROM [dbo].[BoosterTargetLog] AS j0 INNER JOIN (
SELECT
[Extent2].[OrganizationId] AS [OrganizationId],
CAST( [Extent1].[Id] AS int) AS [C1]
FROM [dbo].[BoosterTargetLog] AS [Extent1]
INNER JOIN [dbo].[OrganizationCookie] AS [Extent2] ON [Extent1].[CookieId] = [Extent2].[CookieId]
WHERE ([Extent2].[OrganizationId] = @p__linq__0) AND ([Extent1].[CookieId] = @p__linq__1) AND ([Extent1].[CustomerUserId] IS NULL)
) AS j1 ON (j0.[Id] = j1.[Id])',N'@p__linq__0 int,@p__linq__1 uniqueidentifier,@p__update__0 int',@p__linq__0=1075,@p__linq__1='44A191F0-9086-4867-9777-ACEB9BB3B944',@p__update__0=95941

当我通过 C# 代码或手动执行生成的 SQL 运行更新时,出现此错误:

Invalid column name 'Id'.

问题是生成的 SQL 中有错误。请注意如下内容:

 AS j1 ON (j0.[Id] = j1.[Id])

它实际上应该是:

 AS j1 ON (j0.[Id] = j1.[C1])

那么,我的假设是 EntityFramework.Extended 库中存在某种错误。

解决方法的建议?

最佳答案

我收到了同样的错误,但当我的 Where 子句不匹配任何项目时。

请检查以确保您有匹配的数据:

var query = db.BoosterTargetLogs
.Where(x =>
x.OrganizationCookie.OrganizationId == organizationId &&
x.CookieId == cookieId &&
x.CustomerUserId == null);

我检查以确保我有一些记录要更新,也许试试这个:

if (query.Any())
{
var updated = await query.UpdateAsync(x => new BoosterTargetLog
{
CustomerUserId = customerUserId
});
}

虽然我没有使用 UpdateAsync,只是普通的 Update。希望这同样适用于您的情况。

关于c# - "Invalid column name"使用 EntityFramework.Extended 执行更新时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24730228/

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