[英]No mapping to a relational type can be found for the CLR type 'SqlParameter'
When trying to execute a SQL request with named parameters on EntityFrameworkCore (2.2) I get the following exception : 当尝试在EntityFrameworkCore(2.2)上执行带有命名参数的SQL请求时,我得到以下异常:
InvalidOperationException: No mapping to a relational type can be found for the CLR type 'SqlParameter'.
InvalidOperationException:无法为CLR类型“SqlParameter”找到到关系类型的映射。
What I've tried and works but unsatisfactory: 我尝试过但工作但不尽如人意的:
Raw SQL request: Works but unsafe
原始SQL请求:工作但不安全
Unamed parameters: Also works but less explicit imo
未命名的参数:也可以,但不太明确的imo
await context.Database.ExecuteSqlCommandAsync(
"UPDATE dbo.Table SET ActivationDate = @p0, SubscriptionEndTime = @p1 WHERE SerialNumber IN (@p2) AND UserId = @p3;",
DateTime.UtcNow,
DateTime.UtcNow.AddYears(1),
string.Join("','", eqs.Select(e => e.SerialNumber)),
user.Id);
What does not work: 什么行不通:
This is my code: 这是我的代码:
await context.Database.ExecuteSqlCommandAsync(
"UPDATE dbo.Table SET ActivationDate = @actDate, SubscriptionEndTime = @endDate WHERE SerialNumber IN (@serials) AND UserId = @userId;",
new SqlParameter("@actDate", DateTime.UtcNow),
new SqlParameter("@endDate", DateTime.UtcNow.AddYears(1)),
new SqlParameter("@serials", string.Join("','", eqs.Select(e => e.SerialNumber))),
new SqlParameter("@userId", user.Id));
I don't understand why it isn't working because according to the documentation ( link ): 我不明白为什么它不起作用,因为根据文档( 链接 ):
This allows you to use named parameters in the SQL query string.
这允许您在SQL查询字符串中使用命名参数。 context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));
context.Database.ExecuteSqlCommandAsync(“UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author”,new SqlParameter(“@ author”,userSuppliedAuthor));
So how can I use named parameters in my request ? 那么如何在我的请求中使用命名参数?
EDIT : 编辑:
I don't think the problem comes from the request itself. 我不认为问题来自请求本身。 The following one results in the same exact Exception:
以下结果导致完全相同的异常:
await context.Database.ExecuteSqlCommandAsync(
"UPDATE dbo.Table SET Name = @name WHERE SerialNumber = 'XXXX'",
new SqlParameter("@name", "testing"));
Found my solution... 找到了解决方案......
The imported SqlParameter was from namespace: 导入的SqlParameter来自命名空间:
Microsoft.Azure.Documents.SqlParameter
Microsoft.Azure.Documents.SqlParameter
The correct one is: 正确的是:
System.Data.SqlClient.SqlParameter
System.Data.SqlClient.SqlParameter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.