EF6, Code-First, enable-migrations, "Unable to load the specified metadata resource"

code-first ef-migrations entity-framework entity-framework-6


I have created a C# class library with 3 entity classes and a DbContext for code-first generation of database. All has gone well with version 1. I have created a separate test library and the class library with the DbContext class has been behaving as expected.

Now, I wanted to make one of the fields mandatory and following the code-first conventions, I have added a [Required] attribute to the property in the entity class. The next step was to enable migrations.

I went to the Package Manager Console, entered "enable-migrations" and ... bang ... "Unable to load the specified metadata resource".

For reference, my DbContext class includes:

public OrganisationsContext()
    : base("Leegz_Entities_Organisations")
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.ProxyCreationEnabled = false;

public DbSet<Organisation> Organisations { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<LeegzUser> LeegzUsers { get; set; }

and my app.config contains:

<?xml version="1.0" encoding="utf-8"?>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameter value="v11.0" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    <add name="Leegz_Entities_Organisations" connectionString="data source=NEIL-INSPIRON\NEILDEV;initial catalog=TheLeegz;integrated security=True" providerName="System.Data.SqlClient" />
    <add key="Leegz.Entities.Organisations.DbSecuritySchema" value="Leegz.Entities.Organisations"/>

I've seen a number of threads on this subject, but they all seem to be talking about errors in reference elements of the EDMX model file. However, as I've used code-first, I don't have a model (maybe I'm missing a step here), so the advice that I've seen in relation to the EDMX information in the connection string does not seem to apply to me.

Any ideas, please?

11/6/2013 5:25:57 PM

Accepted Answer

  1. IF you have more than one project in the solution, be sure that in the Package manager console window you have selected the project with dbContext.
  2. Migrations use connectionString with name equals DbContext class. In your case it's OrganisationsContext, but isn't Leegz_Entities_Organisations.
4/25/2014 5:26:33 AM

Popular Answer

I had a similar problem but with a different outcome. As it took too many hours to debug, here are some hints.

  1. The "Unable to load the specified metadata resource." error is related to your connection strings.
  2. Start by checking the connection string in your project where you have all your entities.
  3. Make sure the connection string is for a Code First approach and not the Model First or Database First approach (see below for example).
  4. Repeat these steps in all the projects within your solution where you have a connection string related to your DbContext.
  5. Then, retry the Enable-Migrations command and it should work properly.

Model first connection string example:

<add name="MyContext" 
    connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MY_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
    providerName="System.Data.EntityClient" />

Code first connection string example:

<add name="MyContext" 
    connectionString="Data Source=.;Initial Catalog=MY_DB;Integrated Security=True;MultipleActiveResultSets=True" 

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow