2009-08-13 25 views
81

Estoy usando entidades, C# y SQL Server para crear una aplicación de n niveles. Estoy creando algunas clases base comunes a todos mis componentes DAL. En esta clase base, quiero manejar el estado de conexión de la clase base ObjectContext heredada por objeto de entidades.El tipo o el nombre del espacio de nombres 'Objetos' no existe en el espacio de nombres 'System.Data'

Compilación lanza el siguiente error:

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

También, los que utilizan System.Data.Objects declaración no resuelve por la misma razón.

Intenté agregar el ensamblado como referencia, pero no pude encontrarlo en la pestaña .NET de las referencias de ensamblaje.

¿Alguna idea? ¡Gracias!

Respuesta

176

Necesita agregar una referencia al ensamblaje .NET System.Data.Entity.dll.

+0

¡Funcionó! Curioso, si el espacio de nombres System.Data.objects está realmente presente dentro de System.Data.Entity? – pencilslate

+0

gracias. Eso funcionó. –

0

Agregué una referencia al archivo .dll, para System.Data.Linq, lo anterior no era suficiente. Puede encontrar .dll en los diversos directorios para las siguientes versiones.

System.Data.Linq C: \ Archivos de programa (x86) \ Referencia Asambleas \ Microsoft \ Framework \ v3.5 \ System.Data.Linq.dll 3.5.0.0

System.Data.Linq C: \ archivos de programa (x86) \ Referencia Asambleas \ Microsoft \ Framework.NETFramework \ v4.0 \ Perfil \ Client \ System.Data.Linq.dll 4.0.0.0

+2

Corrección que responde una pregunta donde: El tipo o el nombre del espacio de nombres 'Linq' no existe en el espacio de nombres 'System.Data' –

44

Si está utilizando Entity Framework 6, el espacio de nombre ha cambiado. Desea utilizar

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

Tengo Entity Framework 6.1.3 instalado a través del administrador de paquetes nuget. Hasta ahora no me he referido al ensamblado de Microsoft System.Data.Entity. Me está dando errores. Entonces mi pregunta es: ¿necesito hacer una referencia a System.Data.Entity FIRST antes de agregar esa declaración using? – vibs2006

27

Actualizado desde EF5 a EF6 nuget hace un tiempo atrás y siguió encontrando este problema. Me gustaría solucionarlo actualizando el código generado para hacer referencia a System.Data.Entity.Core.Objects, pero después de la generación volvería a cambiar (como se esperaba desde que se generó).

Esto resolvió el problema para siempre:

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

si desea utilizar "System.Data.Objects.EntityFunctions"

utiliza las "System.Data.Entity.DbFunctions "en EF 6.1+

2

En mi caso para EF 6+, cuando se utiliza esta:

System.Data.Entity.Core.Objects.ObjectQuery 

Como parte de este comando:

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

Tengo este error:

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' 

Así que terminé teniendo que usar esto:

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

Por supuesto, su firma tipográfica anónima puede ser diferente.

HTH.

0

Necesita agregar una referencia al ensamblado .NET System.Data.Linq

+0

Hola nulo29, ¿puedes explicarnos cómo tu respuesta es mejor que las que ya proporcionamos? –

Cuestiones relacionadas