EF 6.1 Scalar-Valued Functions Database First

hncl picture hncl · Oct 15, 2014 · Viewed 11.4k times · Source

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

hncl picture hncl · Oct 16, 2014

Apparently I could not use Scalar-Valued Function directly into my model; I found a solution in this blog http://programmaticponderings.wordpress.com/2012/11/22/first-impressions-of-database-first-development-with-entity-framework-5-in-visual-studio-2012/.

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.