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'
1 | public virtual DbSet<MyTable> Tables { get; set; } |
为了使
谢谢。
您将需要添加对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 } } } } |
刚刚添加了引用