问题描述
我在使用的EntityFramework ALPHA3(从的NuGet)这样的code:
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();
}
}
(基于.NET 4.5)
我曾尝试使用新鲜的ASP.NET MVC项目EntityFunctions - 它总是失败,错误:
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.
同一code工作在控制台应用程序完全没问题。任何想法,有什么不对?
Same code works completely fine in Console app. Any ideas, what's wrong?
推荐答案
我怀疑你的ASP.NET应用程序的情况下,还必须参照 System.Data.Entity的
,而你正在使用 EntityFunctions
类中定义的 System.Data.Entity的
。
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
.
不过,实体框架6去掉了依赖于 System.Data.Entity的
,并重新定义了的EntityFramework内用于此目的的所有必要的类
组装。
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.
意思是说,在你的控制台应用程序的情况下,我猜测可能是设计的( System.Data.Entity的
不被引用)或意外( System.Data.Entity的
引用但 EntityFunctions
类是从 EntityFramework.dll $取C $ C>),
EntityFunctions
(从 EntityFramework.dll
)取。
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.
如果您使用的是实体框架6的任何版本,请确保您使用的是可以在 EntityFramework.dll中发现的
,而不是一个在 EntityFunctions
类 System.Data.Entity的
。 <一href=\"https://entityframework.$c$cplex.com/SourceControl/latest#src/EntityFramework/Core/Objects/EntityFunctions.cs\">Source在实体 EntityFunctions.cs
的code框架6
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
其实,如果你使用实体框架6,我会建议删除任何引用 System.Data.Entity的
- 为了避免今后的任何混乱和错误。
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.
这篇关于在asp.net mvc的LINQ + EntityFunctions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!