开发者

How do I use a stored Procedure to return a single entity in Entity Framework 4?

开发者 https://www.devze.com 2023-01-23 17:43 出处:网络
New to Entity Framework .Using EF4. I have found articles and managed to use stored procedures to return a list of entities.

New to Entity Framework .Using EF4. I have found articles and managed to use stored procedures to return a list of entities.

But cannot see/work ou开发者_Go百科t how you return a single entity.

Given that I have a stored procedure "GetCustomerById" that return a single customer How do I map it?

Using the Model Browser I right click on "Function Import" and I have added my StoredProcedure however whatever I select does not seem to return a "Single Entity"

Am I missing the obvious?

thanks a lot for any link or suggestions


When you do Function Import you need to select the entity your SP returns from the drop down (i.e. Customer). The catch is EF does NOT directly returns Customer object as per your selection but System.Data.Objects.ObjectResult which implements IEnumerable. To be more specific, here is the generated code for your function:

public ObjectResult<Customer> GetCustomerById(Nullable<global::System.Int32> Id)

That's because EF has no idea if your SP returns a single record or a list of them so it wraps the result inside ObjectResult. You can enumerate through this to get your Customer entity like any other IEnumerable object. For example:

Customer myCustomer = context.GetCustomerById(1).First();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号