使用存储过程时出现ASP.Net Core 3.1 MVC错误



客户端希望与数据库的所有通信都通过存储过程完成。要在ASP中做到这一点,我使用FromSqlRaw,如下所示:

_context.Visit.FromSqlRaw("exec VisitApi_RecordVisit @User", userParam)

存储过程完成其工作并记录访问。然而,每次它触发错误时,都会在控制台中抛出错误:

System.InvalidOperationException: The required column 'Id' was not present in the results of a 'FromSql' operation.

在研究这个错误时,我发现这个错误是因为它正在录制的表有一个主键。为了解决这个问题,我必须删除主键列,然后用";"身份规范";设置为";否";。我也尝试过添加.HasNoKey((;并尝试更新数据库。然而,当我这样做时,告诉我需要删除该列并重新添加它。问题是,已经记录了大约200000次访问,我不想丢失已经绑定到每一行的ID。有没有什么方法可以在不删除列和现有数据的情况下消除这个错误?

您也可以使用:

using(var context = new SampleContext())
{
var name = new SqlParameter("@CategoryName", "Test");
context.Database.ExecuteSqlCommand("EXEC AddCategory @CategoryName", name);
}

(取自https://www.learnentityframeworkcore.com/raw-sql#database.executesqlcommand)

文件:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.database?view=efcore-3.1

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.executesqlcommand?view=efcore-3.1

最新更新