开发者

The EntityContainer name must be unique. An EntityContainer with the name 'Entities' is already defined

开发者 https://www.devze.com 2022-12-16 12:58 出处:网络
For a little background: I have a DLL project with the following structure: Rivworks.Model (project) \\Negotiation (folder)

For a little background:

I have a DLL project with the following structure:

Rivworks.Model (project)  
  \Negotiation (folder)  
      Model.edmx (model from DB #1)  
  \NegotiationAutos (folder)  
      Model.edmx (model from DB #2)  

I have moved the connection strings from this project's app.config to the web.config file. They are not in the ConnectionString section. Rather, I have a static class that consumes part of the web.config and exposes them to my app as AppSettings.[settingName].

<FeedAutosEntities_connString>metadata=res://*/;provider=System.Data.SqlClient;provider connection string='Data Source=db4;Initial Catalog=RivFeeds;Persist Security Info=True;User ID=****;Password=&quot;****&quot;;MultipleActiveResultSets=True'</FeedAutosEntities_connString>
<RivWorkEntities_connString>metadata=res://*/NegotiationAutos.NegotiationAutos.csdl|res://*/NegotiationAutos.NegotiationAutos.ssdl|res://*/NegotiationAutos.NegotiationAutos.msl;provider=Sys开发者_StackOverflow社区tem.Data.SqlClient;provider connection string='Data Source=db2;Initial Catalog=RivFramework_Dev;Persist Security Info=True;User ID=****;Password=&quot;****&quot;;MultipleActiveResultSets=True'</RivWorkEntities_connString>

I have 2 classes, one for each Context and they look like this:

namespace RivWorks.Model
{
    public class RivWorksStore
    {
        private RivWorks.Model.Negotiation.Entities _dbNegotiation;

        public RivWorksStore(string connectionString, string metadata, string provider)
        {
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.ConnectionString = connectionString;
            entityBuilder.Metadata = "res://*/";    // metadata;
            //entityBuilder.Provider = provider;
            _dbNegotiation = new RivWorks.Model.Negotiation.Entities(entityBuilder.ConnectionString);
        }

        public RivWorks.Model.Negotiation.Entities NegotiationEntities()
        {
            return _dbNegotiation;
        }
    }
}

namespace RivWorks.Model
{
    public class FeedStoreReadOnly
    {
        private RivWorks.Model.NegotiationAutos.Entities _dbFeed;

        public FeedStoreReadOnly(string connectionString, string metadata, string provider)
        {
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.ConnectionString = connectionString;
            entityBuilder.Metadata = "res://*/";    // metadata;
            //entityBuilder.Provider = provider;
            _dbFeed = new RivWorks.Model.NegotiationAutos.Entities(entityBuilder.ConnectionString);
        }

        public RivWorks.Model.NegotiationAutos.Entities ReadOnlyEntities()
        {
            return _dbFeed;
        }
    }
}

You will note that the MetaData is being rewritten to a short version.

When I comment out that line in each class I get this error:

Unable to load the specified metadata resource.

When I leave that line in in each class I get this error:

Schema specified is not valid. Errors:

Negotiation.Model.csdl(3,4) : error 0019: The EntityContainer name must be unique. An EntityContainer with the name 'Entities' is already defined.

I know it is something simple, something obvious. Any suggestions welcome...


Hijacking this, since it is the top Google result for the error message.

In case anyone else encounters this while only using a single model/context: I once ran into this problem because the assembly containing the model/context was renamed and a copy with the previous name remained in the bin directory of the application. The solution was to delete the old assembly file.


Your two EDMX files probably have the same entity container name. You need to change (at least) one of them.

In the GUI designer, open Model Browser. Look for a node that says "EntityContainer: Entities". Click it. In Properties, change Name to something else. Save and rebuild.


I had the problem too but my solution was to clean the bin directory and then remove the connection string with the entity container name. Then I could rename my entities and put back the connection string.


I renamed my project but the old file was still in the bin folder. I just had to delete the old DLL from the bin folder.


I have found a way to save multiple containers with the same name (namespaced of course).

In EF5 and VS2012 you can set three different namespaces. First you can click on the edmx file in the solution browser and in the properties windows you can set the "Custom Tool Namespace", you can click on the *.Context.tt file right below the edmx and set another namespace there, and finally thanks to a lead from Mr Stuntz awesome answer I realized that by opening the edmx file and clicking in the white space you get another namespace field under Schema in the properties window.

Think we're done, not quite, I know you can see the Entity Container Name field and will try to change the name there but that doesn't seem to work, you get a little error that pops up. Ensure that all your edmx files have an individual namespace there. (I ensured I had a unique namespace in all three places)

Then go to your Model Browser and right click on EntityContainer: Entity to go to properties and change the name of your container(s). From that window with the namespace set everywhere I was able to get multiple contexts with the same name. I was dealing with things like blahcontext and blahcontextcontainer all of a sudden even though they were in different folders.

When you see that its a namespace problem. Or lack thereof.


In my case, the problem was caused by my connection string in Web.config being named the same thing as my entities container class.

Change

<add name="ConflictingNameEntities" connectionString="metadata=res://*/blahblah

to

<add name="ConflictingNameEntitiesConnection" connectionString="metadata=res://*/blahblah

and regenerate the container class by right-clicking ConflictingNameModel.Context.tt in Solution Explorer and clicking "Run Custom Tool".


I just ran into this. It looks like somehow Entity Framework got into a bad state in terms of what tables it thought it needed to add.

Normally EF will not recognize that a new table has to be created until you put the line

public virtual DbSet<NewTable> NewTable { get; set; }

inside your context class.

However, EF somehow got into a bad state where the mere presence of the NewTable class in my solution was triggering it to think that it needed to generate that table. So by the time the above line in the context triggered it to create a NewTable table, it already thought it had done it, hence the error about duplicate entities.

I just removed the NewTable class from my solution, commented things out so it would compile again (thankfully there wasn't too much of this), then added a new migration to make sure it was blank as it should have been. Then once things were back into a more predictable state, I re-added the NewTable class back into the solution and adding the migration worked fine.


If you are using web deployment from Visual Studio sometimes doesn't delete the old dlls. I had the same problem, change to Web Deployment Package and didn't give me problems.


I had a same problem in my asp.net website, to resolve the problem I purposely added compile time error in one of the cs file in the app code by removing a semicolon, then I rectified the compilation problem by adding semicolon again.

This process caused application to compile again.After this error got vanished.


The other cause of this problem, your add model in any solution project and change your model project.

--PROJECT A --> MODEL.EDMX

--- WEB CONFIG -->Entity Connection

--PROJECT B

--- WEB CONFIG -->Entity Connection

Later, I think this structure is bad and Change project.

--PROJECT A
using PROJECT.C;
WEB.CONFIG - USE PROJECT C APP.CONFIG CONNECTIONSTRING 

--PROJECT B
using PROJECT.C;
WEB.CONFIG - USE PROJECT C APP.CONFIG CONNECTIONSTRING 

--PROJECT C  (CLASS LIBRARY) --> MODEL.EDMX 
--- APP.CONFIG -->Entity Connection

Everything was fine but I get an error. Error Detail : The EntityContainer name must be unique. An EntityContainer with the name 'Entities' is already defined

Because I forgot change Web.Config files.

OLD WEB.CONFIG

    <add name="donatelloEntities" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=donatello;persist security info=True;user id=1;password=1;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

NEW WEB.CONFIG

 <add name="donatelloEntities" connectionString="metadata=res://*/EntityModel.Model.csdl|res://*/EntityModel.Model.ssdl|res://*/EntityModel.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=donatello;user id=sa;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

This Problem is Simple but may cause loss of time. I wanted to give an example. I hope is useful.

Thanks.


To solve the issue Entity 6.2.0, VS 2017, single edmx

I changed the name of my model.edmx to another name, built the project, then changed it back to the original name.


In my case, probably after merging a version back, my startup project file (csproj) got corrupted.

Al the Entity classes where added:

<Compile Include="Class.cs">
  <DependentUpon>MyModel.tt</DependentUpon>
</Compile>

After removing all relevant entries by hand the problem was solved.


Strange problems have strange answers, so please do not blame.

first, take a quick look at this part of code

The EntityContainer name must be unique. An EntityContainer with the name 'Entities' is already defined

I got a problem the problem

The EntityContainer name must be unique. An EntityContainer with the name 'Entities' is already defined

with this class name 'ShippingSpeed_Month_Speed'

The solution for me was just renaming 'speeds' to 'speedsx'.

So, you MAY need to rename something like the ending name of the relation or navigational property which is the same ending of the same table name or the ending name of the DbSet<>

I'm answering to document what worked for me and hoping to benefit others.

Thanks


No bin, just aspx/edmx ? Just move the shema files (mode.edmx + model.designer.vb) refresh and put them back ! IIS/aspnet probably corrupt for x reason.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号