2015-07-06 9 views
11

Sto utilizzando Entity Framework per la prima volta, ma sembra non funzionare come previsto.Impossibile trovare un'implementazione del modello di query per il tipo di origine 'System.Data.Entity.DbSet'

ho questo codice:

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; 

     } 
    } 
} 

Sulla linea di query (esattamente il "set" variabile è sottolineata in rosso) ottengo l'errore:

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 viene generato automaticamente da Entity Framework in un approccio Database-First, context.Tables è un DbSet, quindi dovrebbe essere in grado di utilizzare Linq, che è stato aggiunto tramite la direttiva using. Al fine di evitare misurderstantings, all'interno di questa classe ho trovato la seguente:

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

Che cosa mi manca in modo da rendere il lavoro select?

Grazie.

+0

che misura il progetto ha un riferimento System.Core ? – Krishna

+0

@Krishna sì fa – Fylax

risposta

14

dovrai aggiungere riferimento alla System.Data.Linq

System.Data.Linq è LINQ-SQL specifici (DataContext, ecc)

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 
      } 
     } 
    } 
} 
+0

Ho fatto riferimento a _System.Data.Linq_ e l'ho aggiunto a tutta la direttiva _using_, ma continua a darmi il problema – Fylax

+0

che non funziona. L'uso diretto da [...] mi dà l'errore_Could non trovare Tipo o Namespace 'da__ mentre prepending una var x = da [...] mi dà l'errore originale di questo post. – Fylax

+0

Scusate il mio male, stavo facendo questo fuori VS. Per favore riprova. Controlla anche se il tuo contesto sta avendo MyTables o Tabelle come tabella nome db set – Krishna

Problemi correlati