RMD
RMD

Reputation: 3253

Entity Framework Metadata Problem

Using EF 4.0. I have two entity models, both named CoreEntities, but in different assemblies and in difference namespaces.

If I have a single project that references both assmeblies, EF seems to get confused as to what the proper metadata artifacts to load are.

Specifically, I get errors like:

The EntitySet name 'CoreEntities.MyEntities' could not be found.

As soon as a remove a reference to one of the assemblies, the errors stop happening.

I'm unsure as to whether this makes a difference, but I am using the following code to genereate my entitiy connection string:

public static string BuildEntityConnectiongString(string entityContextName)
{
    //Format: metadata=res://*/{EntityContext}.csdl|res://*/{EntityContext}.ssdl|res://*/{EntityContext}.msl;provider=...
    string rawString = System.Configuration.ConfigurationManager.ConnectionStrings["GenericEntityConnectionString"].ConnectionString;
    return rawString.Replace("{EntityContext}", entityContextName);
}

How does EF determine which assembly to look for the embedded meta data in? One would think it would always look in the assembly which contains the model first, but apparently that's not the way it works.

Ideas?

Upvotes: 1

Views: 1879

Answers (1)

RMD
RMD

Reputation: 3253

As usual, a quick look at documentation provides the answer:

http://msdn.microsoft.com/en-us/library/cc716756.aspx

Basically, the wildcard in res://*/ tells the EF to search all loaded assemblies for the meta data. The first one that it finds that matches, it uses.

In my case, it finds the wrong one first.

The resolution to this is to modify my BuildEntityConnectiongString function (and the generic connection string I'm using) to allow me to specify the assembly name explicitly.

Upvotes: 1

Related Questions