C# – Entity Framework – Calling A Stored Procedure Without Mapping

c++, entity-framework, entity-framework-5, sql-server, stored-procedures

I currently use a database-first approach using Entity Framework (EF). I'm investigating different ways of calling stored procedures (stored procedures that are NOT tied to entities) using EF. Currently, I'm using the approach found here using the designer (in this approach, I import a stored procedure, create a function import and use a complex type as my mapped object). I don't like this because it relies on a bloated EDMX file and it causes merge conflicts in source control when two or more people perform this procedure and check in their code.

I found this post which claims I can call a stored procedure and map to a plain old C# object (POCO).

My question is if I want to map to a POCO to a stored procedure in EF not using the designer approach, how do I get aliases for column names? For example, let's say I call a stored procedure and get a particular column back named "CustomerID" and I want the property mapped to it to be named "Id" instead of "CustomerID". How can I do this without using the designer approach?

Best Solution

Something like the following should work:

[your db context].Database.SqlQuery<[your POCO class]>("[name of stored proc] [comma separated parameters]", parameter1, parameter2, .....)

Here's example from one of my applications:

_context.Database.SqlQuery<Library>("usp_paged_select_libraries @userId, @offset, @fetch",                    userIdParameter, offsetParameter, fetchParameter);

Within your POCO you would mark up your properties with:

[Column("[your alias here]")]