我已经从 AdventureWorks 数据库生成了实体模型;现在我想删除 app.config 中的连接字符串并在运行时设置它。在 Model1.Context.cs 文件中,我将构造函数更改为
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();
现在它说找不到元数据关键字。如何在运行时为实体框架设置连接字符串?