6

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;
        OnContextCreated();
    }

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?

MadBoy
  • 10,824
  • 24
  • 95
  • 156
  • see here. this is a way good answer :) http://stackoverflow.com/questions/689355/metadataexception-unable-to-load-the-specified-metadata-resource – Phil Dec 20 '12 at 21:06

2 Answers2

28

After reading this answers article and this blog I changed:

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

To:

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

And it works :-)

Community
  • 1
  • 1
MadBoy
  • 10,824
  • 24
  • 95
  • 156
0

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

Community
  • 1
  • 1
Thiru
  • 407
  • 5
  • 7