Return value mapping on Stored Procedures in Entity Framework
I am calling a stored procedure with EntityFramework. But custom property that i set in partial entity class is null. I have Entities in my edmx (I called edmx i dont know what to call for this). For example I have a "User" table in my database and so i have a "User" class on my Entity. I have a stored procedure called GetUserById(@userId) and in this stored procedure i am writing a basic sql statement like below
"SELECT * FROM Users WHERE Id=@userId"
in my edmx i make a function import to call this stored procedure and set its return value to Entities (also select User from dropdownlist). It works perfectly when i call my stored procedure like below
User user = Context.SP_GetUserById(123456);
But i add a custom new column to stored procedure to开发者_C百科 return one more column like below
SELECT *, dbo.ConcatRoles(U.Id) AS RolesAsString
FROM membership.[User] U
WHERE Id = @id
Now when i execute it from SSMS new column called RolesAsString appear in result. To work this on entity framework i added a new property called RolesAsString to my User class like below.
public partial class User
{
public string RolesAsString{ get; set; }
}
But this field isnt filled by stored procedure when i call it. I look to the Mapping Detail windows of my SP_GetUserById there isnt a mapping on this window. I want to add but window is read only i cant map it. I looked to the source of edmx cant find anything about mapping of SP.
How can i map this custom field?
You have to create a complex type for the SP instead of using the partial class.
Try adding the property to the User entity in the model browser. It might work if defined in the model, not as a partial class... or, in the end, the simplest may be have it return to an entity, and convert the SP result into the User result, as a last resort.
HTH.
Very old thread but I encountered this exact issue today.
One difference is that I haven't imported the SP into the dbContext but chose to use SQLQuery to return a list of Contact entities.
var allContacts = _dbContext.Database.SqlQuery<Contacts>
("CRM.GetAllContactsForCustomer @customerID, param1).ToList();
Like TS I had created a partial Class to hold an extra property that the SP would return.
public partial class Contacts
{
public EnumContactOrigin ContactOrigin { get; set; }
}
This property does NOT get filled when executing the SP, same as when you do so with an imported SP in the dbContext.
For some reason I came up with the idea to create a wrapper class for Contacts to hold that extra property.
public class ExtendedContacts : Contacts
{
public new EnumContactOrigin ContactOrigin { get; set; }
}
And guess what, it works! ContactOrigin does now get filled correctly.
var allContacts = _dbContext.Database.SqlQuery<ExtendedContacts>
("CRM.GetAllContactsForCustomer @customerID, param1).ToList();
In my case I'm ok with it being a wrapper class as I only use the list of contacts to show in a grid.
I would love to know though why this works? And if there is another way of retrieving extra properties into Contacts without the need of this ExtendedContacts class.
精彩评论