c# - 找不到源类型 'System.Data.Entity.DbSet' 的查询模式的实现

标签 c# linq entity-framework

我是第一次使用 Entity Framework,但它似乎没有按预期工作。

我有这个代码:

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”变量用红色下划线标出)我得到错误:

Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'.'Select' not found. Missing a reference or an using directive for 'System.Linq'

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

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

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

谢谢。

最佳答案

您需要添加对 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
            }
        }
     }
}

关于c# - 找不到源类型 'System.Data.Entity.DbSet' 的查询模式的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241541/

相关文章:

c# - 使用 SqlConnection 打开 DataReader 错误

c# - 为什么在实现接口(interface)方法时允许虚拟?

c# - 在 C# 中使用 IDisposable 与析构函数有什么区别?

c# - 单个 LINQ 查询中的多个求和会触发多个 SQL 查询

entity-framework - IValidatableObject 的 Validate 方法期间出现延迟加载问题

c# - 检查上传文件的类型

c# - 使用 LINQ 和匿名类型进行透视?

c# - 如何在linq where子句中比较int和string

c# - 来自 wcf 数据服务的 Entity Framework 6 数据上下文

c# - 具体化查询结果不支持此方法