1
Error while running Sqlquery on my Dbset.
There is no mapping of the object type System.Data.Entity.Core.Objects.Objectparameter for a native type managed provider.
I would like to leave my database generic, so I put the parameters as Objectparameter, how do I perform this mapping of Objectparameter to Sqlparameter?
public static ICollection<T> GetMany(string where, params object[] parameters)
{
object[] objParameters = new object[parameters.Count()];
Type type = typeof(T);
DbSet<T> dbSet = _context.Set<T>();
string sql = " SELECT * FROM " + type.Name.ToUpper() + " WHERE " + where;
int startPosition = 0;
int stopPosition = 0;
int contador = 0;
//Colocar espaço apenas para controle.
sql = sql.Insert(sql.Length, " ");
// Montar Parametros.
for (int i = 0; i < sql.Length; i++)
{
if (sql[i] == '@')
{
startPosition = i + 1;
for (int a = i; a < sql.Length; a++)
{
if (sql[a] == ' ')
{
stopPosition = a + 1;
string nomeParameter = sql.Substring(startPosition, stopPosition - startPosition);
ObjectParameter parameter = new ObjectParameter(nomeParameter.Trim(), parameters[contador]);
objParameters[contador] = parameter;
contador++;
break;
}
}
}
}
var registros = dbSet.SqlQuery(sql, objParameters).ToArray<T>();
if (registros != null)
return registros;
return null;
}
Anderson, I could not understand much your code, I never used Queryable, and also never used Expression<Func<T,bool>, could explain me a little deeper ?
– Nicola Bogar
var t = Entity<Countries>. Getmany(x => x.Description == "GERMANY"); I made the call: but the following error. (Error 2 Cannot Convert lambda Expression to type 'string' because it is not a delegate type C: Users Nicola Bogar Desktop Software Powerade.ERP.Application Powerade.ERP.Application Form1.Cs 38 44 Powerade.ERP.Application )
– Nicola Bogar