Neil W
Neil W

Reputation: 9257

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?

Upvotes: 13

Views: 11042

Answers (3)

Robert J. Good
Robert J. Good

Reputation: 1357

When switching from EF 4/5/6 model-first/database-first to code-first, I neglected to delete the \bin and \obj folders and got the same error on migrations and reading data. After that, this error went away.

To change model-first/database-first to code-first:

  1. Change the connection string from EF (with metadata information) to traditional ADO.
  2. Delete the EDMX file and all child files (.tt, .cs models, etc.)
  3. Delete \bin and \obj of your project.

Your project should be in code-first mode at this point.

Upvotes: 0

Maxime
Maxime

Reputation: 8989

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"/>

Upvotes: 24

alexmac
alexmac

Reputation: 19617

  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.

Upvotes: 5

Related Questions