简体   繁体   English

不允许从数据类型 sql_variant 隐式转换为 varchar

[英]Implicit conversion from data type sql_variant to varchar is not allowed

I'm getting this error when calling a stored procedure from .NET Entity Framework using Dapper.使用 Dapper 从 .NET Entity Framework 调用存储过程时出现此错误。

Implicit conversion from data type sql_variant to varchar is not allowed.不允许从数据类型 sql_variant 隐式转换为 varchar。

The stored procedure parameter is @Name nvarchar(50) = NULL .存储过程参数是@Name nvarchar(50) = NULL

So ideally, it should accept a null value.所以理想情况下,它应该接受一个空值。 When I call the stored procedure and pass @Name = null from .NET, I get gives this error.当我调用存储过程并从 .NET 传递@Name = null时,我得到了这个错误。

This is the stored procedure:这是存储过程:

CREATE PROCEDURE [dbo].[Member_Add]
    @Id NVARCHAR(50)  = NULL,
    @Name NVARCHAR(50)  = NULL
AS
BEGIN
    INSERT INTO [dbo].Member (Id, Name)
    VALUES (@Id, @Name)
END

Dapper was internally mapping object to sql_variant . Dapper 在内部将object映射到sql_variant When I pass null it was considering it as object type.当我传递null它会将其视为object类型。 As my stored procedure was expecting varchar , sql_variant was not convertible to it.由于我的存储过程需要varchar ,所以sql_variant不能转换为它。

I started passing string.Empty instead of null object and it was working fine.我开始传递string.Empty而不是null对象,它工作正常。

@Name = Request.Name??string.Empty; @Name = Request.Name??string.Empty;

对我来说,问题是我使用 Visual Studio 的“选择性粘贴”->“将 JSON 粘贴为类”自动生成了我的 DTO 类,并且它将某些成员的数据类型设置为对象而不是字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.SqlClient.SqlException:不允许从数据类型sql_variant到uniqueidentifier的隐式转换。 - System.Data.SqlClient.SqlException: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. 不允许从数据类型sql_variant隐式转换为varbinary(max)。 使用CONVERT函数运行此查询。 ASP.NET SQL数据源 - Implicit conversion from data type sql_variant to varbinary(max) is not allowed. Use the CONVERT function to run this query. ASP.NET SQL DataSource 不允许从数据类型varchar隐式转换为varbinary(max)C# - Implicit conversion from data type varchar to varbinary(max) is not allowed c# 错误:不允许从数据类型varchar到varbinary(max)的隐式转换。 使用CONVERT函数运行此查询 - Error : Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query 在数据库中存储字节时出错,不允许从数据类型varchar到varbinary(max)的隐式转换 - Error while storing bytes in database ,Implicit conversion from data type varchar to varbinary(max) is not allowed 如何处理异常不允许从数据类型varchar隐式转换为varbinary(max)。 在将数据添加到数据库时 - how to handle the exception Implicit conversion from data type varchar to varbinary(max) is not allowed. while adding the data to database 在SQL Server中使用sql_variant而不是varchar有什么好处吗? - Are there any benefits to using sql_variant over varchar in SQL Server? 不允许从数据类型nvarchar隐式转换为varbinary(max) - Implicit conversion from data type nvarchar to varbinary(max) is not allowed 如何在 C# 中读取 sql_variant 数据库类型 - How to read sql_variant database type in C# 使用EF6存储数据时出现“不允许从数据类型nvarchar(max)到varbinary的隐式转换”错误 - “Implicit conversion from data type nvarchar(max) to varbinary is not allowed” error when storing data using EF6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM