hncl hncl - 11 months ago 63
C# Question

EF 6.1 Scalar-Valued Functions Database First

My application is c# MVC5, using EF 6.1. Imported tables and functions using Database First. I can see the function in model (emdx) browser listed under DALModel.Store / Stored Procedures / Functions (grayed out).

I am trying to use the function using the following:

using (var ctx = new DALEntities())
int? result = ctx.fn_TotalClient(MemberRepository.AllowedCId, fromDate, toDate);
return (result != null ? result.Value : 0);

I get can't resolve fn_TotalClient

Would appreciate your suggestions.

Answer Source

Apparently I could not use Scalar-Valued Function directly into my model; I found a solution in this blog

However, I used a different approach by redeveloping the function as a Table-Valued Function, then used FirstOrDefault() to get the result value.

Hope this could help someone facing the same issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download