2009-08-13 37 views
81

Sto utilizzando entità, C# e SQL Server per creare un'app di livello. Sto creando alcune classi base comuni a tutti i miei componenti DAL. In questa classe base, voglio gestire lo stato della connessione della classe base ObjectContext ereditata dall'oggetto entità.Il nome dello spazio dei nomi 'Oggetti' non esiste nello spazio dei nomi 'System.Data'

Compilazione getta il seguente errore:

The type or namespace name 'Objects' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

Inoltre, le utilizzano System.Data.Objects istruzione non risolve per lo stesso motivo.

Ho provato ad aggiungere l'assembly come riferimento, ma non è riuscito a trovarlo nella scheda .NET dei riferimenti di assembly.

Qualche idea? Grazie!

risposta

176

È necessario aggiungere un riferimento all'assembly .NET System.Data.Entity.dll.

+0

Ha funzionato! Curioso, se il namespace System.Data.objects è effettivamente presente all'interno di System.Data.Entity? – pencilslate

+0

grazie. Questo ha funzionato. –

0

Ho aggiunto un riferimento al file .dll, per System.Data.Linq, quanto sopra non era sufficiente. Puoi trovare .dll nelle varie directory per le seguenti versioni.

System.Data.Linq C: \ Program Files (x86) \ Riferimento Assemblies \ Microsoft \ Framework \ v3.5 \ System.Data.Linq.dll 3.5.0.0

System.Data.Linq C: \ Program Files (x86) \ Riferimento Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ Profilo \ client \ System.Data.Linq.dll 4.0.0.0

+2

Correzione ciò risponde a una domanda in cui: Il tipo o il nome dello spazio dei nomi 'Linq' non esiste nello spazio dei nomi 'System.Data' –

44

Se si utilizza Entity Framework 6, lo spazio nome è cambiato. Si desidera utilizzare

System.Data.Entity.Core.Objects.ObjectQuery 
+0

Ho Entity Framework 6.1.3 installato tramite il gestore pacchetti nuget. Non ho ancora fatto riferimento all'assembly di Microsoft System.Data.Entity. Mi sta dando errori. Quindi la mia domanda è che devo fare riferimento a System.Data.Entity FIRST prima di aggiungere quell'istruzione using? – vibs2006

27

Aggiornato da EF5 a EF6 nuget un po 'indietro e continuava a riscontrare questo problema. Mi piacerebbe risolvere il problema aggiornando il codice generato per fare riferimento a System.Data.Entity.Core.Objects, ma dopo la generazione sarebbe stato modificato nuovamente (come previsto dal suo generato).

Questo risolto il problema per bene:

http://msdn.microsoft.com/en-us/data/upgradeef6

If you have any models created with the EF Designer, you will need to update the code generation templates to generate EF6 compatible code. Note: There are currently only EF 6.x DbContext Generator templates available for Visual Studio 2012 and 2013.

  1. Delete existing code-generation templates. These files will typically be named <edmx_file_name>.tt and <edmx_file_name>.Context.tt and be nested under your edmx file in Solution Explorer. You can select the templates in Solution Explorer and press the Del key to delete them.
    Note: In Web Site projects the templates will not be nested under your edmx file, but listed alongside it in Solution Explorer.
    Note: In VB.NET projects you will need to enable 'Show All Files' to be able to see the nested template files.
  2. Add the appropriate EF 6.x code generation template. Open your model in the EF Designer, right-click on the design surface and select Add Code Generation Item...
    • If you are using the DbContext API (recommended) then EF 6.x DbContext Generator will be available under the Data tab.
      Note: If you are using Visual Studio 2012, you will need to install the EF 6 Tools to have this template. See Get Entity Framework for details.
    • If you are using the ObjectContext API then you will need to select the Online tab and search for EF 6.x EntityObject Generator.
  3. If you applied any customizations to the code generation templates you will need to re-apply them to the updated templates.
2

se si desidera utilizzare "System.Data.Objects.EntityFunctions"

usa "System.Data.Entity.DbFunctions "in EF 6.1+

2

Nel mio caso per EF 6+, quando si utilizza questo:

System.Data.Entity.Core.Objects.ObjectQuery 

Come parte di questo comando:

var sql = ((System.Data.Entity.Core.Objects.ObjectQuery)query).ToTraceString(); 

ho ottenuto questo errore:

Cannot cast 'query' (which has an actual type of 'System.Data.Entity.Infrastructure.DbQuery<<>f__AnonymousType3<string,string,string,short,string>>') to 'System.Data.Entity.Core.Objects.ObjectQuery' 

Così ho finito per dover usare questo:

var sql = ((System.Data.Entity.Infrastructure.DbQuery<<>f__AnonymousType3<string,string,string,short,string>>)query).ToString();  

Naturalmente la tua firma di tipo anonima potrebbe essere diversa.

HTH.

0

È necessario aggiungere un riferimento all'assembly .NET System.Data.Linq

+0

Ciao Null29, puoi spiegare come la tua risposta è migliore di quella già fornita? –

Problemi correlati