![](/img/trans.png)
[英]EFCore 2.2 No mapping to a relational type can be found for the CLR type 'Expression[]'
[英]No mapping to a relational type can be found for the CLR type 'SqlParameter'
當嘗試在EntityFrameworkCore(2.2)上執行帶有命名參數的SQL請求時,我得到以下異常:
InvalidOperationException:無法為CLR類型“SqlParameter”找到到關系類型的映射。
我嘗試過但工作但不盡如人意的:
原始SQL請求:工作但不安全
未命名的參數:也可以,但不太明確的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);
什么行不通:
這是我的代碼:
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));
我不明白為什么它不起作用,因為根據文檔( 鏈接 ):
這允許您在SQL查詢字符串中使用命名參數。 context.Database.ExecuteSqlCommandAsync(“UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author”,new SqlParameter(“@ author”,userSuppliedAuthor));
那么如何在我的請求中使用命名參數?
編輯:
我不認為問題來自請求本身。 以下結果導致完全相同的異常:
await context.Database.ExecuteSqlCommandAsync(
"UPDATE dbo.Table SET Name = @name WHERE SerialNumber = 'XXXX'",
new SqlParameter("@name", "testing"));
找到了解決方案......
導入的SqlParameter來自命名空間:
Microsoft.Azure.Documents.SqlParameter
正確的是:
System.Data.SqlClient.SqlParameter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.