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.
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+"'");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With