I have generated entity model from AdventureWorks database; now I want to delete the connection string in app.config and set it at runtime. In the Model1.Context.cs file I have chnaged the constructor to
public AdventureWorksEntities(string str)
: base("name=AdventureWorksEntities")
{
this.Database.Connection.ConnectionString = str;
}
and in the program.cs file
EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
ecsb.Provider = @"System.Data.SqlClient";
ecsb.ProviderConnectionString =
@"data source=.\sqlexpress;initial catalog=AdventureWorks;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
using (var ent = new AdventureWorksEntities(ecsb.ConnectionString))
{
Console.WriteLine(ent.Database.Connection.ConnectionString);
var add = ent.Addresses;
foreach (var ad in add)
{
Console.WriteLine(ad.City);
}
}
Console.ReadKey();
Now it says metadata keyword not found. How to set connectionstring for entityframework at runtime?