Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No mapping exists from ObjectParameter to a known managed provider native type

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

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

On this query:

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; 
}

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

like image 903
TheIdiot Avatar asked Apr 08 '14 17:04

TheIdiot


1 Answers

try using sql parameter instaed of object parameter

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

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+"'");
like image 152
COLD TOLD Avatar answered Oct 14 '22 13:10

COLD TOLD