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.
Column mapping for raw SQL queries
Re: Column mapping for raw SQL queries
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
Re: Column mapping for raw SQL queries
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.
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
We are investigating the question.
Re: Column mapping for raw SQL queries
We recommend you to implement the corresponding method yourself. There are two alternative ways:CmdKeen wrote:Even if there was just a helper method that would convert a data table / DbReader to the entity that would be excellent.
- universal method which uses reflection and ADO.NET
- special methods for each particular class