我有这样的代码使用EntityFramework Alpha3(来自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();
}
}
我曾尝试在新的 ASP.NET MVC 项目(基于 .net 4.5)中使用 EntityFunctions - 它总是失败并出现错误:
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.
相同的代码在控制台应用程序中完全正常。任何想法,有什么问题?