简体   繁体   English

没有为CLR类型'SqlParameter'找到到关系类型的映射

[英]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: 什么行不通:

  • Passing an array of SqlParameters or objects 传递一组SqlParameters或对象

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.

相关问题 EFCore 2.2 无法为 CLR 类型“Expression[]”找到到关系类型的映射 - EFCore 2.2 No mapping to a relational type can be found for the CLR type 'Expression[]' 对于 CLR 类型“Int32[]”,找不到到关系类型的映射 - No mapping to a relational type can be found for the CLR type 'Int32[]' PostgreSQL EF.Core DB-First Invalid DB Model:找不到与 CLR 类型为“bool”的属性“Db.Order.High”的关系类型的映射 - PostgreSQL EF.Core DB-First Invalid DB Model: No mapping to a relational type can be found for property 'Db.Order.High' with the CLR type 'bool' CLR类型到EDM类型的映射在EF 6和5中不明确? - The mapping of CLR type to EDM type is ambiguous with EF 6 & 5? CLR类型到EDM类型的映射不明确,因为有多个CLR类型 - The mapping of CLR type to EDM type is ambiguous because multiple CLR types SqlParameter和数据类型问题 - Issue with SqlParameter and data type CLR类型到EDM类型的映射与EF 4类ARE在单独的程序集中不明确 - The mapping of CLR type to EDM type is ambiguous with EF 4 classes ARE in a separate assembly CLR类型到EDM类型的映射是模糊的 - 适用于某些机器 - The mapping of CLR type to EDM type is ambiguous - works in some machines Xamarin - 在 xmlns clr-namespace 中找不到类型 - Xamarin - Type not found in xmlns clr-namespace 如何找到OData edm模型类型到clr类型的映射? - How to find mapping of OData edm model type to clr type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM