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?
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!!
I had a similar problem but with a different outcome. As it took too many hours to debug, here are some hints.
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="data source=.;initial catalog=MY_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With