Perform database mapping for Dbset<T>. Sqlquery()

Asked

Viewed 172 times

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

1 answer

2

Since T is an entity mapped in the EF, you could do so:

public IEnumerable<T> GetMany(Expression<Func<T, bool>> where = null)
{
    IQueryable<T> query = Db.Set<T>();

    if (where != null)
    {
        query = query.Where(where);
    }

    return query.AsNoTracking().ToList();
}

The call of this method would be something like this:

var lista = GetMany(p => p.AlgumaPropriedade == algumValor);

EDIT: Iqueryable is an interface inherited from Ienumerable, as well as Ilist, for example, as can be seen on this link.

Already the Expression<Func<T, bool>> is the same parameter that Where do Linq uses.

You must use an instance of the class you placed the Getmany method in.

  • 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 ?

  • 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 )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.