我已經從AdventureWorks數據庫中生成實體模型;現在我想刪除app.config中的連接字符串,並在運行時設置它。在Model1.Context.cs文件我已經chnaged構造函數在運行時設置實體框架連接字符串
public AdventureWorksEntities(string str)
: base("name=AdventureWorksEntities")
{
this.Database.Connection.ConnectionString = str;
}
和Program.cs文件
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();
現在它說,沒有發現元數據關鍵字。如何在運行時爲實體框架設置連接字符串?