43

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=System.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...

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Keith Barrows
  • 24,802
  • 26
  • 88
  • 134

14 Answers14

166

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.

jpo
  • 2,550
  • 2
  • 19
  • 16
  • 5
    I experienced the exact same issue after renaming my assembly . You need to manually delete the old files from the bin folder as a Visual Studio is no longer tracking them and performing a "Clean" will not remove them – Chris Oct 31 '12 at 10:37
  • 3
    Greate, thanks a lot. I've got the same assembly of different versions, one in the bin of Web Service (4.0.1.0) and another in the GAC (4.0.1.2). In the config file I define two use the GAC version (Custom Role Provider). I had the same error ultill the old version dll had been removed from the bin folder. – Daniel Kabzon Nov 15 '12 at 10:13
  • 1
    Just to clarify for others, the old assembly is located on the server side in the deploy location. – ChrisO Mar 11 '14 at 11:41
  • I was publishing to a web server and it doesn't delete old assemblies from the bin directory by default. This was exactly it. – trousyt Aug 19 '14 at 17:51
  • This needs bumped to the top of the page. Excellent answer, saved me a headache. I wish I could changetip ya. – TheSoftwareJedi Jul 30 '15 at 16:06
  • 1
    Jip, came back here after a few months and saw my original upvote. Wish I could upvote again – user230910 Nov 24 '15 at 05:45
  • 1
    The 'performing a clean will not remove them' cannot be emphasized enough. – Mike Burger Oct 28 '16 at 17:41
  • For me, this answer together with [MrFlo](https://stackoverflow.com/a/26101713/6305294) answer did the trick. – Alex Apr 23 '20 at 06:20
  • Legend - saved me hours of investigation! Legacy app had the assembly name with spaces in it (urgh) - so after the rename devops doesn't clear the bin folder as you stated - and voila, deleting the old 'space' assembly fixed the problem. Thank you. – PoorbandTony Feb 03 '23 at 11:15
49

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.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    yep - as simple as that. (still trying to understand the overall model of EF at the (usually hidden) class level.) – Keith Barrows Jan 13 '10 at 22:44
  • 1
    You may also need to open the EDMX file in notepad as Visual Studio may not properly pick up the change when you simply change the container name in the property pane. – Ayo I Aug 21 '12 at 19:32
  • In my case, I had one EDMX file that I dropped and re-added. I had to go through Model Browser => Properties to reestablish the container name. – Codes with Hammer Apr 25 '14 at 14:31
21

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.

MrFlo
  • 335
  • 3
  • 8
  • 6
    +1 This is the solution for me. Deleting bin contents on its own didn't solve it; the connection string part of this answer did the trick. – Nick Jan 03 '17 at 16:17
5

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.

Rob Sedgwick
  • 4,342
  • 6
  • 50
  • 87
4

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".

Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
3

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.

TombMedia
  • 1,962
  • 2
  • 22
  • 27
  • I also had to change the `Model.tt` file and change the namespace there. I'm not sure that changing the actual edmx namespace changes anything though... – Benjol Sep 18 '13 at 11:47
  • Weird, if I don't set the namespace on the edmx (left click on edmx, not left click in white space), it stops generating half my POCOs. If I include it, I get an extra (empty) `Model.cs` class generated. If I change the namespace with left click in white space *too*, I get nothing but the `Model.cs` class. Ngngng – Benjol Sep 18 '13 at 12:01
  • Eejit me: in the same folder, so the second one generated obviously squishes the first... – Benjol Sep 18 '13 at 12:30
1

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.

d512
  • 32,267
  • 28
  • 81
  • 107
1

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.

0

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.

Sagar Shirke
  • 648
  • 9
  • 32
0

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.

rootturk
  • 316
  • 4
  • 20
0

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.

lloyd
  • 1,089
  • 1
  • 17
  • 39
0

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.

Jos R.
  • 41
  • 4
-1

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

first, take a quick look at this part of code

enter image description here

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

Adel Mourad
  • 1,351
  • 16
  • 13
-1

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.

François Breton
  • 1,158
  • 14
  • 24