Page 1 of 1

Entity Framework and SqlQuery

Posted: Wed 04 Feb 2015 08:23
by TopSviluppo
I'm using EF 6.1.1 and trying the SqlQuery method to build dynamic queries.

This code:

using (DBAuraContext context = new DBAuraContext(EFConnectionString))
{
string sql = "SELECT * FROM Assortimenti WHERE CodiceAssortimento > {0}";

System.Data.Entity.Infrastructure.DbRawSqlQuery<Assortimenti> rawRows =
context.Database.SqlQuery<Assortimenti>(sql, "A");

List<Assortimenti> assortim = rawRows.ToList<Assortimenti>();
foreach(Assortimenti ass in assortim)
{
Console.WriteLine(ass.CodiceAssortimento);
}
}

throws a PgSqlException with message {"la colonna \"p0\" non esiste"} (column p0 doesn't exist).

What is wrong in this code ?

Re: Entity Framework and SqlQuery

Posted: Fri 06 Feb 2015 14:53
by Shalex
Please use

Code: Select all

string sql = "SELECT * FROM Assortimenti WHERE CodiceAssortimento > ?";
or

Code: Select all

string sql = "SELECT * FROM Assortimenti WHERE CodiceAssortimento > $1";
instead of

Code: Select all

string sql = "SELECT * FROM Assortimenti WHERE CodiceAssortimento > {0}";
For more information, refer to http://www.devart.com/dotconnect/postgr ... eters.html.

Re: Entity Framework and SqlQuery

Posted: Mon 09 Feb 2015 08:33
by TopSviluppo
Ok, I tried your suggestion and it works.
I was using the syntax {0} because with Sql Server Provider it seems it's the only supported, and i supposed that parameters syntax was the same.

Thank you for your reply