LINQ + EntityFunctions in mvc c# entity-framework


I have such code using EntityFramework Alpha3 (from nuget):

class Member
    public int Key { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Sitename { get; set; }
    public DateTime? RegDate { get; set; }

class MembersContext : DbContext
    public MembersContext()
        : base("Name=ConnectionString")

    public DbSet<Member> Members { get; set; }

public class HomeController : Controller
    // GET: /Home/

    public ActionResult Index()

        const int memberKey = 1001;
        using (var db = new MembersContext())
            var query = from m in db.Members
                            m.Key == memberKey
                            && EntityFunctions.DiffDays(m.RegDate, DateTime.Now) > 0
                        select m;

            var member = query.FirstOrDefault();

        return View();

I have tried to use EntityFunctions in fresh ASP.NET MVC project (based on .net 4.5) - it always fails with error:

LINQ to Entities does not recognize the method 'System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])' method, and this method cannot be translated into a store expression.

Same code works completely fine in Console app. Any ideas, what's wrong?

5/14/2013 10:58:46 AM

Accepted Answer

I suspect that in the case of your ASP.NET app, you also have a reference to System.Data.Entity, and that you are using the EntityFunctions class defined System.Data.Entity.

However, Entity Framework 6 has removed the dependency on System.Data.Entity and has redefined all necessary classes for that purpose inside of the EntityFramework assembly.

Meaning that, in the case of your console app, I am guessing that either by design (System.Data.Entity is not referenced) or by accident (System.Data.Entity is referenced but the EntityFunctions class is taken from EntityFramework.dll), the correct version of EntityFunctions (from EntityFramework.dll) is taken.

If you are using any version of Entity Framework 6, make sure that you are using the EntityFunctions class that can be found in EntityFramework.dll, not the one in System.Data.Entity. Source code of EntityFunctions.cs in Entity Framework 6

Actually, if you use Entity Framework 6, I would recommend removing all and any references to System.Data.Entity - in order to avoid any future confusion and mistake.

5/14/2013 10:16:30 AM

Popular Answer

The reason why you're getting this error is that by design Linq to Entities converts all expression to server query. So it does not know how to translate DiffDays to SQL. Try to omit this method and rewrite expression.


EntityFunctions are Canonical functions so you can use them in Linq to Entities. So the only reasonable explanation is in EF Alhpa version.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow