C# 如何在实体框架6中使用简单的选择

C# 如何在实体框架6中使用简单的选择,c#,asp.net,linq,entity-framework,C#,Asp.net,Linq,Entity Framework,如何通过主键查询Entity Framework 6对象 我有以下代码: using (var db = new bookEntities()){ var books = db.books.where // this doesn't work as work command is not available var books = from b in db.books where b.book_id == 1 select b; //

如何通过主键查询Entity Framework 6对象

我有以下代码:

using (var db = new bookEntities()){
    var books = db.books.where // this doesn't work as work command is not available

     var books = from b in db.books
          where b.book_id == 1
          select b; // results in compilation error: "Could not find an implementation of the query pattern of source type System.Data.Entity.DbSet...
}
db.books return
System.Data.Entity.DbSet

我的目标是实现即时延迟加载,但我需要知道如何首先访问数据。我跟着,但对我不起作用

请注意,我正在使用EntityFramework6(最新版本)在ASP.NET4.5Web项目的类中运行代码。我遵循了几个教程,但总是得到相同的结果

在班级的第一名:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
运行项目时,我得到:

Parser Error Message: Could not load type 'System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider'.

谢谢你的随机猜测,因为我没有可以测试这个的设置,但我想你需要:

using System.Data.Linq;

你的使用声明很好。它应该使用System.Linq。未使用System.Data.Linq

检查web.config文件中是否有正确的引用:

<assemblies>

    <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

</assemblies>


db.books.FirstOrDefault(b=>b.book\u id==1)
应该适合您。您的
DbContext
拥有
books
DbSet
产权?
bookEntities()
返回什么?什么是bookEntities?急切的延迟加载是矛盾的,因为急切的加载与延迟加载相反。当鼠标悬停在bookEntities()上时,VS显示:bookEntities.bookEntities()。它是EF6基于MySQL数据库生成对象时创建的类。您缺少实例化对象上下文的
new
关键字<代码>使用(var db=new bookEntities())…嗨,我明白了“名称空间系统中不存在namspace name Linq的类型。DataI已添加引用,现在可以编译。我来看看它是否有效。Reference
System.Data.Linq.dll
在引用管理器中引用System.Data.Linq无法解决此问题。我仍然得到名称空间名称“Linq”的类型不存在错误如果您引用了
System.Data.Linq
并且仍然找不到
System.Data.Linq
名称空间,那么您还有其他问题。