Monday, August 27, 2018

Entity Framework (EF) 6 Database.SqlQuery Mapping

I bumped into a problem with mapping results from manually crafted sql query to object using EF. Somehow, it doesn't seem to recognize the column attribute. And I found out that it really doesn't and they don't plan to enhance EF 6, although there seems to have the option in EF Core to do the mapping.

To illustrate my problem better, suppose my database column name is pk and would like to map it to ID property. Usually, using [Column("pk")] will solve the mapping, however, it doesn't work if the query was executed via dbContext.Database.SqlQuery("SELECT pk FROM TableName").

So I have to either change the ID property to pk or the one that I preferred is to change the query to dbContext.Database.SqlQuery("SELECT pk AS ID FROM TableName").

No comments:

Post a Comment