2009-08-13 184 views
81

我正在使用实体,C#和SQL Server创建一个n层应用程序。我正在创建一些通用于所有DAL组件的基类。在这个基类中,我想处理由实体对象继承的ObjectContext基类的连接状态。命名空间'System.Data'中不存在类型或名称空间名称'Objects'

编译引发以下错误:

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

此外,using语句System.Data.Objects不能解决出于同样的原因。

我尝试添加程序集作为参考,但无法在程序集引用的.NET选项卡中找到它。

有什么想法?谢谢!

回答

176

您需要添加对.NET程序集System.Data.Entity.dll的引用。

+0

它工作正常!好奇的是,如果System.Data.Entity中确实存在System.Data.objects名称空间? – pencilslate 2009-08-13 22:51:21

+0

谢谢,这很有效。 – 2017-03-01 08:25:46

0

我添加了对.dll文件的引用,对于System.Data.Linq, 以上是不够的。您可以在以下版本的各种目录 中找到.dll。

将System.Data.Linq C:\ Program Files文件(x86)的\参考大会\微软\框架\ v3.5版本\ System.Data.Linq.dll 3.5.0.0

将System.Data.Linq C:\ Program Files文件(x86)的\参考大会\微软\ Framework.NETFramework \ V4.0 \资料\客户端\ System.Data.Linq.dll 4.0.0.0

+2

更正这回答了一个问题,其中:类型或命名空间名称'Linq'不存在于命名空间'System.Data' – 2011-08-07 19:11:53

44

如果您正在使用实体框架6,名称空间已更改。你想用

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

我有Entity Framework 6.1.3通过nuget包管理器安装。我没有引用微软的程序集System.Data.Entity。它给我错误。所以我的问题是,我需要在添加using语句之前引用System.Data.Entity FIRST吗? – vibs2006 2017-04-24 05:50:22

27

从EF5升级到EF6的NuGet而回,并保持遇到此问题。我会通过更新生成的代码来修复它,以便引用System.Data.Entity.Core.Objects,但是在生成之后它会再次改回(如自生成以来的预期那样)。

这解决好这个问题:

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
如果你想使用 “System.Data.Objects.EntityFunctions”

使用“System.Data.Entity.DbFunctions

“在EF 6中。1+

2

在我来说,我EF 6+,在使用这样的:

System.Data.Entity.Core.Objects.ObjectQuery 

此命令的一部分:

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

我得到这个错误:

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' 

所以我最终不得不使用这个:

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

当然,您的匿名类型签名可能会有所不同。

HTH。

0

您需要添加对.NET程序集的引用System.Data.Linq

+0

嗨Null29,你能解释你的答案比已经提供的答案好吗? – 2017-06-01 13:48:36

相关问题