Page 1 of 1

Column mapping for raw SQL queries

Posted: Wed 06 Jun 2012 09:52
by CmdKeen
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.

Re: Column mapping for raw SQL queries

Posted: Mon 11 Jun 2012 12:19
by Shalex

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?

Re: Column mapping for raw SQL queries

Posted: Tue 12 Jun 2012 09:03
by CmdKeen
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.

Re: Column mapping for raw SQL queries

Posted: Thu 14 Jun 2012 15:27
by Shalex
We are investigating the question.

Re: Column mapping for raw SQL queries

Posted: Tue 19 Jun 2012 15:03
by Shalex
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