将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

MetadataException: Unable to load the specified metadata resource
MetadataException: Unable to load the specified metadata resource

All of a sudden i keep getting a MetadataException on instantiating my generated ObjectContext class.the connection string in App.Config looks correct - hasn't changed since last it worked - and i've tried regenerating a new model (edmx-file) from the underlying database with no change.

Anyone have any ideas?

Further details : i haven't changed any properties, i haven't changed the name of any output assemblies, i haven't tried to embed the EDMX in the assembly.i've merely waited 10 hours from leaving work until i got back.and then it wasn't working anymore.

I've tried recreating the EDMX.i've tried recreating the project.i've even tried recreating the database, from scratch.no luck, whatsoever.

时间:

You can get this exception when the Edmx is in one project and you are using it from another.

The reason is Res://*/ is a uri which points to resources in the CURRENT assembly.if the Edm is defined in a different assembly from the code which is using it, res://*/is not going to work because the resource cannot be found.

Instead of specifying '*', you need to provide the full name of the assembly instead (including public key token).Eg :


res://YourDataAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdefabcedf/YourEdmxFileName.csdl|res://...

A better way to construct connection strings is with EntityConnectionStringBuilder :


public static string GetSqlCeConnectionString(string fileName)
{
 var csBuilder = new EntityConnectionStringBuilder();

 csBuilder.Provider ="System.Data.SqlServerCe.3.5";
 csBuilder.ProviderConnectionString = string.Format("Data Source={0};", fileName);

 csBuilder.Metadata = string.Format("res://{0}/YourEdmxFileName.csdl|res://{0}/YourEdmxFileName.ssdl|res://{0}/YourEdmxFileName.msl", 
 typeof(YourObjectContextType).Assembly.FullName);

 return csBuilder.ToString();
}

public static string GetSqlConnectionString(string serverName, string databaseName)
{
 SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();

 providerCs.DataSource = serverName;
 providerCs.InitialCatalog = databaseName;
 providerCs.IntegratedSecurity = true;

 var csBuilder = new EntityConnectionStringBuilder();

 csBuilder.Provider ="System.Data.SqlClient";
 csBuilder.ProviderConnectionString = providerCs.ToString();

 csBuilder.Metadata = string.Format("res://{0}/YourEdmxFileName.csdl|res://{0}/YourEdmxFileName.ssdl|res://{0}/YourEdmxFileName.msl",
 typeof(YourObjectContextType).Assembly.FullName);

 return csBuilder.ToString();
}

If you still encounter the exception, open the assembly in reflector and check the filenames for your. csdl,. ssdl and. msl files.when the resources have different names to the ones specified in the metadata value, it's not going to work.

I had a similar error.i had recreated the project (long story), and pulled everything over from the old project.i hadn't realized that my model had been in a directory called 'Model 'before, and was now in a directory called 'Models'.once i changed the connection in my Web.Config from this :


<add name="RecipeManagerEntities" connectionString="metadata=res://*/Model.Recipe.csdl

to this :


<add name="RecipeManagerEntities" connectionString="metadata=res://*/Models.Recipe.csdl

Everything worked. (Note that i had to change this three places in this string.

I also had this problem and it was because the connectionstring in my web.config was slightly different than the one in the app.config of the assembly where my EDMX is located.no idea why it changed, but here are the two different versions.

App.config :


<add name="SCMSEntities" connectionString="metadata=res://*/Model.SMCSModel.csdl|res://*/Model.SMCSModel.ssdl|res://*/Model.SMCSModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SANDIEGOsql2008;initial catalog=SCMS;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient"/>

Web.config :


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

What fixed it was simply copying the app.config string (notice the small difference at the end - instead of" App=EntityFramework"it wanted" application name=EntityFramework") into the web.config and problem was solved.: )

...