gpt4 book ai didi

MySql 更新查询不适用于 ASP.NET 中的 EF6

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

public void PostPlayerAttendance(int id, string attendance)
{
using (var ctx = new ApplicationDbContext())
{
var upd = ctx.Database.ExecuteSqlCommand(
@"UPDATE dbName.Player SET Attendance = @Attendance WHERE Id = @Id",
new SqlParameter("Attendance", attendance),
new SqlParameter("Id", id));
}
}

也尝试过这个:

@"UPDATE `dbName.Player` SET `Attendance` = @Attendance WHERE `Id` = @Id"

还有这个:

@"UPDATE 'dbName.Player' SET 'Attendance' = @Attendance WHERE 'Id' = @Id"

出现此错误:

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Only MySqlParameter objects may be stored

最佳答案

您将 SQL 语句和 LINQ 混合在一起。我建议您使用 LINQ,因为您正在使用 Entity Framework 。

你的代码应该是这样的:

public void PostPlayerAttendance(int id, string attendance)
{
using (var ctx = new ApplicationDbContext())
{
var query = (from player in ctx.Players
where player.ID == id
select player).Single();
query.Attendance = attendance;
ctx.SubmitChanges();
}
}

了解 LINQ here

关于MySql 更新查询不适用于 ASP.NET 中的 EF6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29854640/

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