Entity Framework and SqlQuery

Entity Framework and SqlQuery

Postby TopSviluppo » Wed 04 Feb 2015 08:23

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 ?
TopSviluppo
 
Posts: 7
Joined: Fri 19 Dec 2014 07:42

Re: Entity Framework and SqlQuery

Postby Shalex » Fri 06 Feb 2015 14:53

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/postgresql/docs/?parameters.html.
Shalex
Devart Team
 
Posts: 7659
Joined: Thu 14 Aug 2008 12:44

Re: Entity Framework and SqlQuery

Postby TopSviluppo » Mon 09 Feb 2015 08:33

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
TopSviluppo
 
Posts: 7
Joined: Fri 19 Dec 2014 07:42


Return to dotConnect for PostgreSQL