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 = from m in db.Members
where
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?
&& EntityFunctions.DiffDays(m.RegDate.Value, DateTime.Now)asm.RegDateis a nullable property in the class definition.DiffDaysinto executable SQL. Could you post the implementation ofDiffDays?Entity Frameworkutility functions to construct custom LINQ queries than could be translated into store expressions (SQL)if (query.Any()) { var member = query.First(); }use FirstOrDefaul