关于c#:找不到源类型’System.Data.Entity.DbSet’的查询模式的实现

Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'

我第一次使用Entity Framework,但似乎无法正常工作。

我有以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.TablesDbSet,因此它应该能够使用通过using指令添加的Linq。 为了避免造成误解,我在本课程中找到以下内容:

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

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

谢谢。


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
            }
        }
     }
}


刚刚添加了引用using System.Linq;,并且如上所述已经可以正常工作了。