1

I have a solution with two relevant projects. The first builds My.exe, and the second builds a class library MyModel.dll that contains only my EF model.

I'm getting a MetadataException in my Model's VS-generated ObjectContext ctor. I've read through Troubleshooting Entity Framework Connection Strings, but I still haven't been able to narrow down my problem.

The offending constructor code:

public MyEntities() :
    base(@"name=MyEntities", "MyEntities") // MetadataException here
{
    this.ContextOptions.LazyLoadingEnabled = true;
    OnContextCreated();
}

Metadata Artifact Processing is set to EmbedInOutputAssembly. When I open MyModel.dll in Reflector, I see:

  • DataAccessLayer.MyModel.csdl
  • DataAccessLayer.MyModel.msl
  • DataAccessLayer.MyModel.ssdl

I've tried setting Build Action for my app.config to None and Content, and neither makes a difference. The config file contains:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyEntities" 
         connectionString="metadata=
            res://*/DataAccessLayer.MyModel.csdl|
            res://*/DataAccessLayer.MyModel.ssdl|
            res://*/DataAccessLayer.MyModel.msl;
            provider=Devart.Data.PostgreSql;
            provider connection string=&quot;
                User Id=MY_USER;
                Password=MY_PASS;
                Host=127.0.0.1;
                Database=MY_DB;
                Persist Security Info=True&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

I've tried replacing the resource prefix res://*/ with both res://MyModel.dll/ and res://MyModel/, leaving the rest intact (because it matches the resources in MyModel.dll), but neither solved the problem. My class library is strong-named.

Both my executable and class library projects contain references to System.Data.Entity and Devart.Data.PostgreSql, and My.dll is being built to the same location as My.exe.

21
  • 1
    Look at your assembly with reflector and make sure the names match and that is is actually embedded as a resource. Also the connection needs to be in the calling assemblies app.config or web.confg. Commented Jun 13, 2011 at 20:30
  • Are you positive that MyModel.dll is even loaded at this time? A hard-wired reference to the assembly in the res:// URIs may be a better idea than *. Commented Jun 13, 2011 at 20:36
  • * searches the current assembly for the embedded resource I think. However he still needs to have the connection string in the app.config or web.config of the calling project / exe whatever :) I had this same issue, spent several hours on it. Commented Jun 13, 2011 at 20:39
  • The app.config file belongs to the project for My.exe, and I've tried the hard-wired reference res://MyModel.dll. The project for My.exe also contains a reference to the MyModel project; doesn't this mean MyModel.dll should already be loaded? Commented Jun 13, 2011 at 20:50
  • I only use one param in my ctor base("name=MyEntities") where Myentities is the exact name of the EF connection string in the app.config or web.config Commented Jun 13, 2011 at 20:50

1 Answer 1

1

I stepped into .NET source code, and by examining the arguments & local variables of internal EF calls, found the resource assembly name was still set to *.

It turns out a bug in my application code set the model's default connection string before calling the ObjectContext ctor. It was pulled from a Settings.settings file, which hadn't been recently sync'd with the latest, correct app.config.

Some reflections on our discussion in the comments:

  • The correct assembly name in app.config is MyModel rather than MyModel.dll (as shown in Craig's article.) Using MyModel.dll produces an exception in the EF code.

  • Calling the ctor as base(@"name=MyEntities") instead of base(@"name=MyEntities", "MyEntities") actually produced a new exception in ObjectContext.GetEntitySetName()

I updated Settings.settings to be in sync with app.config, and everything is now working.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.