Entity Framework and SqlQuery

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
TopSviluppo
Posts: 7
Joined: Fri 19 Dec 2014 07:42

Entity Framework and SqlQuery

Post by 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 ?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity Framework and SqlQuery

Post by 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/postgr ... eters.html.

TopSviluppo
Posts: 7
Joined: Fri 19 Dec 2014 07:42

Re: Entity Framework and SqlQuery

Post by 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

Post Reply