没有从ObjectParameter到已知的托管提供者本机类型的映射 [英] No mapping exists from ObjectParameter to a known managed provider native type

查看:196
本文介绍了没有从ObjectParameter到已知的托管提供者本机类型的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF向Microsoft SQL Server构建WCF服务,但仍然收到以下错误:

I am in the process of building a WCF service using EF to a Microsoft SQL Server but keep getting the following error:


附加信息:从对象类型System.Data.Objects.ObjectParameter到已知的托管提供者本机类型不存在映射。

Additional information: No mapping exists from object type System.Data.Objects.ObjectParameter to a known managed provider native type.

在此查询中:

string ID = "XXID";
string Sql = @"SELECT * FROM @Table WHERE " + ID + " LIKE '@SearchTerm'";
ObjectParameter[] Parameters = new ObjectParameter[2];
Parameters[0] = new ObjectParameter("Table", Table);
Parameters[1] = new ObjectParameter("SearchTerm", SearchTerm);

using (var Context = new XXEntities())
{
    Context.Database.Connection.Open();
    IEnumerable<string> Query = Context.Database.SqlQuery<string>(Sql, Parameters);

    string[] Results = Query.ToArray();
    Context.Database.Connection.Close();
    return Results; 
}

我已经在SQL Server上测试了查询,它的工作原理如下 - 匹配ID的记录。

I have tested the query on SQL Server and it works as expected- returning the record of a matched ID.

推荐答案

尝试使用sql参数instaed的对象参数

try using sql parameter instaed of object parameter

IEnumerable<string> Query = Context.Database.SqlQuery<string>(Sql, 
new SqlParameter("Table", Table), 
new SqlParameter("SearchTerm", SearchTerm));

理论上你可以操纵sql,而不需要像这样的参数发送任何$ b $
$ b

Theoretically you can manipulate sql without the need of any send in parameters like this

IEnumerable<string> Query = Context.Database.SqlQuery<string>("SELECT * FROM "+Table+" WHERE " + ID + " LIKE '"+SearchTerm+"'");

这篇关于没有从ObjectParameter到已知的托管提供者本机类型的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆