Entity Framework error - “The EntityContainer name

2020-06-01 13:34发布

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.

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.

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.

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

This error can be fixed by recycling the application pool or restarting 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.

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.

10条回答
小情绪 Triste *
2楼-- · 2020-06-01 14:06

I fixed this issue in EntityFramework 6.0 by.

  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"

Note: my entities resides in a separate DLL.

Hope this helps.

查看更多
小情绪 Triste *
3楼-- · 2020-06-01 14:06

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

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

<add name="Entities" connectionString="metadata=res://MyLibrary.Data;&#xA;         provider=MySql.Data.MySqlClient;
查看更多
该账号已被封号
4楼-- · 2020-06-01 14:07

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.

;-)

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

查看更多
对你真心纯属浪费
5楼-- · 2020-06-01 14:09

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.

查看更多
爷的心禁止访问
6楼-- · 2020-06-01 14:11

Change the webconfig file entity connection string entry from this

metadata=res://*/App_Code.AAA.csdl|res://*/App_Code.AAA.ssdl|res://*/App_Code.AAA.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=XXX;initial catalog=XXX;user id=XXX;password=XXX;multipleactiveresultsets=True;App=EntityFramework&quot;" 
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=&quot;data source=XXX;initial catalog=XXX;user id=XXX;password=XXX;multipleactiveresultsets=True;App=EntityFramework&quot;" 
providerName="System.Data.EntityClient
查看更多
姐就是有狂的资本
7楼-- · 2020-06-01 14:14

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

查看更多
登录 后发表回答