For SQL Server connection, override the entity container class: (works in EF6, SQL server 2012 express, VS2013)
public partial class PxxxxEntities
{
private PxxxxEntities(string connectionString)
: base(connectionString)
{
}
public static PxxxxEntities ConnectToSqlServer(string host, string catalog, string user, string pass, bool winAuth)
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
{
DataSource = host,
InitialCatalog = catalog,
PersistSecurityInfo = true,
IntegratedSecurity = winAuth,
MultipleActiveResultSets = true,
UserID = user,
Password = pass,
};
// assumes a connectionString name in .config of MyDbEntities
var entityConnectionStringBuilder = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
ProviderConnectionString = sqlBuilder.ConnectionString,
Metadata = "res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl",
};
return new PxxxxEntities(entityConnectionStringBuilder.ConnectionString);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…