Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"?>
<configuration>
  <configSections>
    <!-- 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="Leegz_Entities_Organisations" connectionString="data source=NEIL-INSPIRON\NEILDEV;initial catalog=TheLeegz;integrated security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="Leegz.Entities.Organisations.DbSecuritySchema" value="Leegz.Entities.Organisations"/>
  </appSettings>
</configuration>

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?

like image 425
Neil W Avatar asked Nov 06 '13 17:11

Neil W


People also ask

How do I change the connection string in EDMX?

Open the edmx (go to properties, the connection string should be blank), close the edmx file again. Open the app. config and uncomment the connection string (save file) Open the edmx, go to properties, you should see the connection string uptated!!


2 Answers

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" 
    providerName="System.Data.SqlClient"/>
like image 127
Maxime Avatar answered Sep 28 '22 13:09

Maxime


  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.
like image 44
alexmac Avatar answered Sep 28 '22 13:09

alexmac