0
votes

I use this code to change Connection string

var connstring = ConfigurationManager
                 .ConnectionStrings["BAEntities"].ConnectionString;
var estringnew = new EntityConnectionStringBuilder(connstring);
estringnew.Metadata = Properties.Settings.Default.MetadataFilePath; 
var context = new BAEntities(estringnew.ToString());
var query =
    from con in context.Contacts
    where con.Addresses.Any((a) => a.City == "Seattle")
    select con;

source

I have error in line:

estringnew.Metadata = Properties.Settings.Default.MetadataFilePath; 

Severity Code Description Project File Line Suppression State Error CS1061 'Settings' does not contain a definition for 'MetadataPath' and no extension method 'MetadataPath' accepting a first argument of type 'Settings' could be found (are you missing a using directive or an assembly reference?)

how can i find the current MetadataFilePath in the project?

I use Entity framework 6 and .net 4.5

1
is this code first or database first? - CodingYoshi
@CodingYoshi Database first - Amir Abdollahi

1 Answers

1
votes

Imagine your model is named Abc then set the Metadata like this:

estringnew.Metadata = "res://*/Abc.csdl|res://*/Abc.ssdl|res://*/Abc.msl"

In the above you will see the wildcard * used. Here is the meaning of that:

If you specify a wildcard (*) for assemblyFullName, the Entity Framework runtime will search for resources in the following locations, in this order:

  1. The calling assembly.
  2. The referenced assemblies.
  3. The assemblies in the bin directory of an application.

A Better Option

string con = ConfigurationManager.ConnectionStrings["BAEntities"].ConnectionString;
Type contextType = typeof(BAEntities);
string efConnection = 
    string.Format(
        "metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;provider=System.Data.SqlClient;provider connection string=\"{1}\"",
        contextType.Name,
        con);

object objContext = Activator.CreateInstance(contextType, efConnection);
return objContext as Entities; 

More info here