Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using Entity Framework for the first time, but it seems not working as expected.

I have this code:

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;           }     } } 

On the query line (exactly the "set" variable is underlined in red) I get the error:

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 is auto-generated by Entity Framework in a Database-First approach, context.Tables is a DbSet, so it should be able to use Linq, which has been added through the using directive. In order to avoid misurderstantings, within this class I find the following:

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

What am I missing in order to make the select work?

Thank you.

like image 284
Fylax Avatar asked Jul 06 '15 08:07

Fylax


1 Answers

you will need to add reference to System.Data.Linq

System.Data.Linq is LINQ-SQL specific (DataContext, etc)

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             }         }      } } 
like image 197
Krishna Avatar answered Sep 21 '22 12:09

Krishna