Entity Framework: The given metadata resource could not be loaded.

c# entity-framework


I decided to move Entity Connection String from app.config to code. However after setting it up like this:

    public static string GetConnectionString() {
        string connection = "";

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = dbServer;
        sqlBuilder.InitialCatalog = dbInitialCatalog;

        sqlBuilder.IntegratedSecurity = false;
        sqlBuilder.UserID = dbUserName;
        sqlBuilder.Password = dbPasswWord;
        sqlBuilder.MultipleActiveResultSets = true;

        EntityConnectionStringBuilder entity = new EntityConnectionStringBuilder();
       // entity.Name = "EntityBazaCRM";
        entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl";

        entity.Provider = "System.Data.SqlClient";
        entity.ProviderConnectionString = sqlBuilder.ToString();

        connection = entity.ToString();

        return connection;

I have an exception thrown Unable to load the specified metadata resource. in .Designer.cs.

    /// <summary>
    /// Initialize a new EntityBazaCRM object.
    /// </summary>
    public EntityBazaCRM(string connectionString) : base(connectionString, "EntityBazaCRM")
        this.ContextOptions.LazyLoadingEnabled = true;

If I define .Name inside my Entity creator it throws another exception

"Other keywords are not allowed when the 'Name' keyword is specified." (System.ArgumentException) Exception Message = "Other keywords are not allowed when the 'Name' keyword is specified.", Exception Type = "System.ArgumentException"

I know I'm missing something that I have to change so that self generated code uses new connection string but where to look for it?

1/23/2012 12:35:06 PM

Accepted Answer

After reading this answers article and this blog I changed:

  entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl";


  entity.Metadata = "res://*/";

And it works :-)

5/23/2017 11:53:22 AM

Popular Answer

I upgraded to the new csproj format(Visual studio 2017 with simple format) after that I started getting this error. The csproj has a feature where you don't need to include each file instead it includes all the relevant files under the folder by default so the entity framework files are treated same way so those are not embedded into the assembly by default.

I need to go and manually change the build action of my edml file(edmx in case of Microsoft entity framework) to 'DevartEntityDeploy' (I hope it is EntityDeploy for Microsoft Entity framework)and build it which solved my problem

enter image description here

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