c# - LINQ + EntityFunctions in asp.net mvc -
i have such code using entityframework alpha3 (from nuget):
class member { [key] 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() { database.setinitializer<memberscontext>(null); const int memberkey = 1001; using (var db = new memberscontext()) { var query = 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 use entityfunctions in fresh asp.net mvc project (based on .net 4.5) - fails error:
linq entities not recognize method 'system.nullable`1[system.int32] diffdays(system.nullable`1[system.datetime], system.nullable`1[system.datetime])' method, , method cannot translated store expression.
same code works fine in console app. ideas, what's wrong?
i suspect in case of asp.net app, have reference system.data.entity
, , using entityfunctions
class defined system.data.entity
.
however, entity framework 6 has removed dependency on system.data.entity
, has redefined necessary classes purpose inside of entityframework
assembly.
meaning that, in case of console app, guessing either design (system.data.entity
not referenced) or accident (system.data.entity
referenced entityfunctions
class taken entityframework.dll
), correct version of entityfunctions
(from entityframework.dll
) taken.
if using version of entity framework 6, make sure using entityfunctions
class can found in entityframework.dll
, not 1 in system.data.entity
. source code of entityfunctions.cs
in entity framework 6
actually, if use entity framework 6, recommend removing , references system.data.entity
- in order avoid future confusion , mistake.
Comments
Post a Comment