【问题标题】:Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'找不到源类型“System.Data.Entity.DbSet”的查询模式的实现
【发布时间】:2015-09-23 08:17:47
【问题描述】:

我是第一次使用实体框架,但它似乎没有按预期工作。

我有这个代码:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

public static class QueryClass
{
    public static void Query()
    {
        using (var context = new MyDbEntities())
        {
            DbSet<MyTable> set = context.Tables;
            var query = from val in set select value; 

        }
    }
}

在查询行(确切地说“set”变量用红色下划线表示)我收到错误:

找不到源类型的查询模式的实现 'System.Data.Entity.DbSet'。找不到'选择'。缺少参考资料或 'System.Linq' 的 using 指令

MyDbEntities 是由实体框架以数据库优先的方式自动生成的,context.Tables 是一个DbSet,因此它应该能够使用通过using 指令添加的Linq。为了避免误解,在这个类中我发现了以下内容:

public virtual DbSet<MyTable> Tables { get; set; }

为了使select 工作,我缺少什么?

谢谢。

【问题讨论】:

  • 你的项目有参考 System.Core 吗?
  • @Krishna 是的

标签: c# linq entity-framework


【解决方案1】:

您需要添加对 System.Data.Linq 的引用

System.Data.Linq 是特定于 LINQ-SQL 的(DataContext 等)

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Linq;
using System.Linq;

public static class QueryClass
{
    public static void Query()
    {
        using (var context = new MyDbEntities())
        {

            IQueryable<MyTable> qTable= from t in context.Tables
                                        select t; // can you confirm if your context has Tables or MyTables?
            Console.WriteLine("Table Names:");
            foreach (var t in qTable)
            {
                Console.WriteLine(t.Name);//put the relevant property instead of Name
            }
        }
     }
}

【讨论】:

  • 我引用了 System.Data.Linq 并通过 using 指令添加了它,但它仍然给我这个问题
  • 它不起作用。直接使用 from [...] 给了我错误_Could not find Type or Namespace 'from'_ 而在前面加上 var x = from [...] 给了我这篇文章的原始错误。
  • 对不起,我在 VS 外面做这个。请再试一次。还要检查您的上下文是否将 MyTables 或 Tables 作为表数据库集名称
  • 就我而言,我需要参考 System.Linq
  • 在使用 Linq2SQL 时应完成对 System.Data.Linq 的引用,在所有其他情况下设置对 System.Linq 的引用。
【解决方案2】:

刚刚添加了一个参考using System.Linq; 并且工作正常,如上所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多