Column mapping for raw SQL queries

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
CmdKeen
Posts: 2
Joined: Wed 06 Jun 2012 09:43

Column mapping for raw SQL queries

Post by CmdKeen » Wed 06 Jun 2012 09:52

The entity framework doesn't pass the results of the various raw SQL query methods (ExecuteStoreQuery and SqlQuery) through the mapping process which means if column mapping has been used raw sql is a real pain. There is vague suggestion that is it coming at some point, but isn't in the EF5 RC. The suggested workaround of aliasing each column is also a massive pain and hardly good practice as the column mappings should be the single source of such information.

So is there a way to either access the column mapping information, fluent or otherwise, or even better a way to translate from a sql reader to a set of entities? If it comes to it I'll probably use the DbContext template as a starting point for autogenerating more accessible mapping information but I'd rather not have to.

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

Re: Column mapping for raw SQL queries

Post by Shalex » Mon 11 Jun 2012 12:19

Code: Select all

public class Dept {   
    int id { get; set; }
}

modelBuilder.Entity<Dept>().Property(d => d.Dept).ColumnName("DEPTNO");

var depts  = ctx.ExecuteStoreQuery<Dept>("select deptno from dept"); // failed
var depts  = ctx.ExecuteStoreQuery<Dept>("select deptno as "id" from dept"); // succeded
As we understood, you are looking for the way to execute the "failed" line from the sample successfully, aren't you?

CmdKeen
Posts: 2
Joined: Wed 06 Jun 2012 09:43

Re: Column mapping for raw SQL queries

Post by CmdKeen » Tue 12 Jun 2012 09:03

Yes - exactly. I should probably have posted an example...

Even if there was just a helper method that would convert a data table / DbReader to the entity that would be excellent.

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

Re: Column mapping for raw SQL queries

Post by Shalex » Thu 14 Jun 2012 15:27

We are investigating the question.

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

Re: Column mapping for raw SQL queries

Post by Shalex » Tue 19 Jun 2012 15:03

CmdKeen wrote:Even if there was just a helper method that would convert a data table / DbReader to the entity that would be excellent.
We recommend you to implement the corresponding method yourself. There are two alternative ways:
  • universal method which uses reflection and ADO.NET
  • special methods for each particular class

Post Reply