C# 命名空间“System.Data”中不存在类型或命名空间名称“Objects”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1275043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:32:41  来源:igfitidea点击:

The type or namespace name 'Objects' does not exist in the namespace 'System.Data'

c#.netvisual-studio-2008linq-to-entitiesentities

提问by pencilslate

I am using entities, C# and SQL Server to create an n-tier app. I am creating some base classes common to all my DAL components. In this base class, i want to handle the connection state of the ObjectContext base class inherited by entities object.

我正在使用实体、C# 和 SQL Server 来创建 n 层应用程序。我正在创建一些对所有 DAL 组件通用的基类。在这个基类中,我想处理由实体对象继承的ObjectContext基类的连接状态。

Compiling throws the following error:

编译会抛出以下错误:

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

命名空间“System.Data”中不存在类型或命名空间名称“Objects”(您是否缺少程序集引用?)

Also, the using statement System.Data.Objects doesn't resolve for the same reason.

此外,由于同样的原因,使用语句 System.Data.Objects 也无法解析。

I tried adding the assembly as reference, but couldn't find it in the .NET tab of assembly references.

我尝试将程序集添加为引用,但在程序集引用的 .NET 选项卡中找不到它。

Any thoughts? Thanks!

有什么想法吗?谢谢!

采纳答案by Joshua Belden

You need to add a reference to the .NET assembly System.Data.Entity.dll.

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

回答by Tom Cubbins

I added a reference to the .dll file, for System.Data.Linq, the above was not sufficient. You can find .dll in the various directories for the following versions.

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

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

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

System.Data.Linq C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\System.Data.Linq.dll 4.0.0.0

System.Data.Linq C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\System.Data.Linq.dll 4.0.0.0

回答by Caesar

Same problem in VS 2013

VS 2013 中的同样问题

I added in Web.config :

我在 Web.config 中添加:

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

It worked like a charm.

它就像一个魅力。

I found it on page: http://www.programmer.bz/Home/tabid/115/asp_net_sql/281/The-type-or-namespace-name-Objects-does-not-exist-in-the-namespace-SystemData.aspx

我在页面上找到了它:http: //www.programmer.bz/Home/tabid/115/asp_net_sql/281/The-type-or-namespace-name-Objects-does-not-exist-in-the-namespace-系统数据文件

回答by James

If you are using Entity Framework 6, the name space has changed. You want to use

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

System.Data.Entity.Core.Objects.ObjectQuery

回答by Tim Valentine

Upgraded from EF5 to EF6 nuget a while back and kept encountering this issue. I'd temp fix it by updating the generated code to reference System.Data.Entity.Core.Objects, but after generation it would be changed back again (as expected since its generated).

不久前从 EF5 升级到 EF6 nuget 并不断遇到此问题。我会通过将生成的代码更新为 reference 来临时修复它System.Data.Entity.Core.Objects,但是在生成之后它会再次更改回来(正如预期的那样)。

This solved the problem for good:

这很好地解决了问题:

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

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>.ttand <edmx_file_name>.Context.ttand be nested under your edmx file in Solution Explorer. You can select the templates in Solution Explorer and press the Delkey 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 Generatorwill be available under the Datatab.
      Note:If you are using Visual Studio 2012, you will need to install the EF 6 Tools to have this template. See Get Entity Frameworkfor details.
    • If you are using the ObjectContext API then you will need to select the Onlinetab 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.

如果您有使用 EF 设计器创建的任何模型,则需要更新代码生成模板以生成 EF6 兼容代码。 注意:目前只有 EF 6.x DbContext Generator 模板可用于 Visual Studio 2012 和 2013。

  1. 删除现有的代码生成模板。这些文件通常被命名为<edmx_file_name> .TT<edmx_file_name> .Context.tt下在解决方案资源管理您的EDMX文件嵌套。您可以在解决方案资源管理器中选择模板并按Del键删除它们。
    注意:在网站项目中,模板不会嵌套在您的 edmx 文件下,而是在解决方案资源管理器中与它一起列出。
    注意:在 VB.NET 项目中,您需要启用“显示所有文件”才能看到嵌套的模板文件。
  2. 添加适当的 EF 6.x 代码生成模板。在 EF 设计器中打开您的模型,右键单击设计图面并选择添加代码生成项...
    • 如果您使用 DbContext API(推荐),则EF 6.x DbContext Generator将在“数据”选项卡下可用。
      注意:如果您使用的是 Visual Studio 2012,则需要安装 EF 6 工具才能拥有此模板。有关详细信息,请参阅获取实体框架
    • 如果您使用的是 ObjectContext API,则需要选择Online选项卡并搜索EF 6.x EntityObject Generator
  3. 如果您对代码生成模板应用了任何自定义,则需要将它们重新应用到更新的模板。

回答by sumanth

if you want to use "System.Data.Objects.EntityFunctions"

如果你想使用“System.Data.Objects.EntityFunctions”

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

在 EF 6.1+ 中使用“System.Data.Entity.DbFunctions”

回答by user8128167

In my case for EF 6+, when using this:

在我的 EF 6+ 的情况下,当使用这个时:

System.Data.Entity.Core.Objects.ObjectQuery

As part of this command:

作为此命令的一部分:

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

I got this 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'

So I ended up having to use this:

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

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

Of course your anonymous type signature might be different.

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

HTH.

哈。

回答by Null29

You need to add a reference to the .NET assembly System.Data.Linq

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