建立 xxxEntities 的擴充class,加入方法 : CreateDbContext
public partial class xxxEntities : DbContext
{
private xxxEntities(string connectionString)
: base(connectionString)
{
}
public static xxxEntities CreateDbContext()
{
var efbuilder = new EntityConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["xxxEntities"].ConnectionString);
var sqlbuilder = new SqlConnectionStringBuilder(efbuilder.ProviderConnectionString);
sqlbuilder.Password = "xxx";
efbuilder.ProviderConnectionString = sqlbuilder.ConnectionString;
return new xxxEntities(efbuilder.ConnectionString);
}
}
使用方式
using (var db=xxxEntities.CreateDbContext())