C# – The EntityContainer name must be unique in different assemblies

centity-frameworkentity-framework-4

I have two projects:

  • News.Data
  • Tags.Data

Both define Entities. When I try to execute

using (var db = new News.Data.Entities("name=Entities"))
{
    results1 = db.News.ToList();
}

using (var db = new Tag.Data.Entities("name=Entities"))
{
    results2 = db.Tag.ToList();
}

on a console application I get this error:

Schema specified is not valid. Errors:
Model1.csdl(3,4) : error 0019: The
EntityContainer name must be unique.
An EntityContainer with the name
'Entities' is already defined.

Is it possible to use

News.Data.Entities
Tags.Data.Entities

instead of

News.Data.NewsEntities
Tags.Data.TagsEntities

?

Best Answer

Connectionstrings are configured at application level, and the containername serves as a unique identifier. Either change the containerNames to a unique value, either reimplement the the default constructor of the ObjectContext to perhaps lookup a Setting, which can be configured at assembly level.

Edit: When creating an instance of an ObjectContext, News.Data.Entities in this example, the base class of your context is typically constructed with a ContainerName. This parameter gives the ObjectContext all the necessary information to make its store connection, and do all its mapping business.

Basically, an association is made between the ObjectContext type and the ContainerName. When instantiating your second context Tags.Data.Entities, which is a different type, it will try to associate the type with the container and this is what throws the exception, since you can't associate the same ContainerName twice.

To address the issue, if you can, best thing would be to recreate the EDMX, with different container names. If deleting is not an option, you can modify the ContainerName parameter in the designer, then crack open the edmx in notepad, and look for 'EntitiesModelStoreContainer', and change the 'Entities' part to whatever your new ContainerName is.

Hope it helps...