.net 实体框架错误 - “EntityContainer 名称必须是唯一的”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/979664/
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-09-03 12:51:05  来源:igfitidea点击:

Entity Framework error - "The EntityContainer name must be unique"

.netvisual-studioentity-frameworkexception-handling

提问by ENX

We have a solution which consists of two projects - a console application project and a web application project. Each of these has an identical in schema, but differently named .edmx file used to access the same database.

我们有一个由两个项目组成的解决方案 - 一个控制台应用程序项目和一个 Web 应用程序项目。每个都具有相同的架构,但用于访问同一数据库的 .edmx 文件名称不同。

The console application runs as a scheduled task on the same server as the web application is hosted. The web application contains a reference to the console application as certain actions within the web application lead to methods in the console application being called. This combination has been running in our staging environment successfully for many months.

控制台应用程序在托管 Web 应用程序的同一台服务器上作为计划任务运行。Web 应用程序包含对控制台应用程序的引用,因为 Web 应用程序中的某些操作会导致调用控制台应用程序中的方法。这种组合已经在我们的临时环境中成功运行了好几个月。

When released live we are seeing the following error message at least once a day:

实时发布时,我们每天至少会看到以下错误消息:

Schema specified is not valid. Errors:

指定的架构无效。错误:

ProjectName.csdl(3,4) : error 0019: The EntityContainer name must be unique. An EntityContainer with the name 'ProjectEntities' is already defined. ProjectName.csdl(63,4) : error 0019: Each type name in a schema must be unique. Type name 'ProjectModel.DataSource' was already defined. ProjectName.csdl(75,4) : error 0019: Each type name in a schema must be unique. Type name 'ProjectModel.Error' was already defined.

ProjectName.csdl(3,4):错误 0019:EntityContainer 名称必须是唯一的。已定义名为“ProjectEntities”的 EntityContainer。ProjectName.csdl(63,4):错误 0019:架构中的每个类型名称都必须是唯一的。已定义类型名称“ProjectModel.DataSource”。ProjectName.csdl(75,4):错误 0019:架构中的每个类型名称都必须是唯一的。已定义类型名称“ProjectModel.Error”。

This continues, listing all the type names in our schema.

这将继续,列出我们模式中的所有类型名称。

This error can be fixed by recycling the application pool or restarting IIS.

可以通过回收应用程序池或重新启动 IIS 来修复此错误。

Please note that the error does not occur until the web application has been running for a while - it is not occuring when we compile or the first time we access the application.

请注意,该错误直到 Web 应用程序运行一段时间后才会发生 - 当我们编译或第一次访问该应用程序时不会发生该错误。

My initial theory was that the two Entity Containers are conflicting with each other because the web application references the console application, but they have different names, which makes the error "The EntityContainer name must be unique. An EntityContainer with the name 'ProjectEntities' is already defined" confusing.

我最初的理论是两个实体容器相互冲突,因为 Web 应用程序引用了控制台应用程序,但它们具有不同的名称,这导致错误“实体容器名称必须是唯一的。名称为“ProjectEntities”的实体容器是已经定义”令人困惑。

回答by Guilherme Ferreira

Go to that bin folder and delete dll and pdb file manually.

转到该 bin 文件夹并手动删除 dll 和 pdb 文件。

回答by Zhaph - Ben Duguid

When faced with this problem in the past, I've always created a third, shared library that contains the entity model - that way you know there's not going to be a name conflict, if you decide to add a new table/column/method to the model you only need to do so once, etc, etc.

过去遇到这个问题时,我总是创建一个包含实体模型的第三个共享库 - 这样你就知道如果你决定添加一个新表/列/方法,不会有名称冲突对于模型,您只需要这样做一次,等等。

回答by Thentu

Change the webconfig file entity connection string entry from this

从此更改 webconfig 文件实体连接字符串条目

metadata=res://*/App_Code.AAA.csdl|res://*/App_Code.AAA.ssdl|res://*/App_Code.AAA.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX;initial catalog=XXX;user id=XXX;password=XXX;multipleactiveresultsets=True;App=EntityFramework"" 
providerName="System.Data.EntityClient

to

metadata=res://MyProject/App_Code.AAA.csdl|res://MyProject/App_Code.AAA.ssdl|res://MyProject/App_Code.AAA.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX;initial catalog=XXX;user id=XXX;password=XXX;multipleactiveresultsets=True;App=EntityFramework"" 
providerName="System.Data.EntityClient

回答by fordareh

Just in case anyone else is desperate and not finding any answers - I accidentally ended up with a copy of my .edmx file included in the project in a second location. Both were being included in the build.

以防万一其他人绝望而找不到任何答案 - 我不小心最终将我的 .edmx 文件的副本包含在项目中的第二个位置。两者都包含在构建中。

;-)

;-)

If this happened to you, fear not: You were not the first.

如果这发生在你身上,不要害怕:你不是第一个。

回答by Balaji Birajdar

This error occurs when you have more than one entity model.edmx files in a single DLL or class library project. Although the reason behind the error is not known to me , you can fix this by deleting the other model or moving the other model.edmx file to another class library .

当单个 DLL 或类库项目中有多个实体 model.edmx 文件时,会发生此错误。虽然我不知道错误背后的原因,但您可以通过删除另一个模型或将另一个 model.edmx 文件移动到另一个类库来解决此问题。

Also dont forget to remove the references of the previous dll

也不要忘记删除以前的dll的引用

回答by Julio Avila

Well, in my case I'm working diferent software architecture, because I have diferent projects (dlls), they access to the same Database, but I have diferent entities inside. So, when I have references in a project that needs all or at least two of them, i get this error

好吧,就我而言,我正在使用不同的软件架构,因为我有不同的项目(dll),它们访问相同的数据库,但我内部有不同的实体。因此,当我在需要全部或至少两个引用的项目中引用时,我收到此错误

If I change the Entity Container Name on each one, I solve the problem, but now my connection string are diferent and I have to include all the connection strings in the config file for each dll I refer.

如果我更改每个 Entity Container Name 上的实体容器名称,我就解决了问题,但现在我的连接字符串不同,我必须在我引用的每个 dll 的配置文件中包含所有连接字符串。

回答by jayson.centeno

I fixed this issue in EntityFramework 6.0 by.

我在 EntityFramework 6.0 中修复了这个问题。

  1. During creation of the models, I name the .edmx to the name of the entities. i.e. (MYDbEntities)

  2. After creation i open the MYDbEntities.Context.cs and modified the "Entities" Class to "MYDbEntities".

  3. Open the App.Config and look for the connection string
  4. Open the MYDbEntities.Context.cs and change the connection string to Connection string from the config. example "MyDbConnection"
  5. Make sure that the connection string is like this

    metadata=res:///MYDbEntities.csdl|res:///MYDbEntities.ssdl|res://*/MYDbEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=YourDb;persist security info=True;user id=sa;password=Sw0rdf!sh;multipleactiveresultsets=True;application name=EntityFramework"

  1. 在创建模型期间,我将 .edmx 命名为实体的名称。即(MYDbEntities)

  2. 创建后,我打开 MYDbEntities.Context.cs 并将“实体”类修改为“MYDbEntities”。

  3. 打开 App.Config 并查找连接字符串
  4. 打开 MYDbEntities.Context.cs 并将连接字符串从配置更改为连接字符串。示例“MyDbConnection”
  5. 确保连接字符串是这样的

    metadata=res:// /MYDbEntities.csdl|res:///MYDbEntities.ssdl|res://*/MYDbEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=.;初始目录=YourDb;persist security info=True;user id=sa;password=Sw0rdf!sh;multipleactiveresultsets=True;application name=EntityFramework”

Note: my entities resides in a separate DLL.

注意:我的实体驻留在单独的 DLL 中。

Hope this helps.

希望这可以帮助。

回答by FLICKER

In my case none of the solutions worked. I finally recycled the IIS on my hosting to make it work. Hope this helps someone :)

在我的情况下,没有一个解决方案有效。我终于在我的主机上回收了 IIS 以使其工作。希望这对某人有所帮助:)

回答by Himalaya Garg

My Entities were in another class library (MyLibrary.Data). Changing below worked :

我的实体在另一个类库 (MyLibrary.Data) 中。以下更改有效:

<add name="Entities" connectionString="metadata=res://*/;&#xA;         provider=MySql.Data.MySqlClient; .....

<add name="Entities" connectionString="metadata=res://MyLibrary.Data;&#xA;         provider=MySql.Data.MySqlClient;

回答by John Zumbrum

Note when trying to rename a entity container to that of a previously deleted entity container, you will need to go into your web.config or app.config and delete the connection string out of the configuration section.

请注意,当尝试将实体容器重命名为之前删除的实体容器时,您需要进入 web.config 或 app.config 并从配置部分中删除连接字符串。