Mic*_*mel 1 c# entity-framework asp.net-mvc-4
我有这样的代码使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在新的ASP.NET MVC项目中使用EntityFunctions(基于.net 4.5) - 它总是失败并出现错误:
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.
Run Code Online (Sandbox Code Playgroud)
相同的代码在Console应用程序中完全正常.有什么想法,有什么不对?
我怀疑在你的ASP.NET应用程序的情况下,你也有一个引用System.Data.Entity,并且你正在使用EntityFunctions定义的类System.Data.Entity.
但是,实体框架6已删除了依赖关系,System.Data.Entity并已在EntityFramework程序集内部为此目的重新定义了所有必需的类.
这意味着,在您的控制台应用程序的情况下,我猜测无论是设计(System.Data.Entity未引用)还是偶然(System.Data.Entity被引用但是EntityFunctions从中获取类EntityFramework.dll),都会采用正确的EntityFunctions(from EntityFramework.dll)版本.
如果您使用的是任何版本的Entity Framework 6,请确保您使用的EntityFunctions是可以找到的类EntityFramework.dll,而不是使用的类System.Data.Entity.实体框架6中的源代码EntityFunctions.cs
实际上,如果您使用实体框架6,我建议删除所有和任何引用System.Data.Entity- 以避免任何未来的混淆和错误.
| 归档时间: |
|
| 查看次数: |
6546 次 |
| 最近记录: |