public partial class MainDataModel : DbContext
{
public MainDataModel()
: base("name=MainDataModel")
{
}
...
public MainDataModel(string databaseName)
{
Database.Connection.ConnectionString = @"Data Source=MySQLServer;Initial Catalog=" + databaseName + ";Integrated Security=True";
}
using (var context = new MainDataModel("MyFirstDatabase"))
{
...
}
using (var context = new MainDataModel("MySecondDatabase"))
{
...
}
private static void InitConnectSetting() {
Configuration config = ConfigurationManager.
OpenExeConfiguration(ConfigurationUserLevel.None);
var path = DebugMode ? DebugModeDbPath() : Application.StartupPath;
Dictionary<string, string> db = new Dictionary<string, string>() {
{"ec", string.Format("{0}\\db\\{1}", path, /*DebugMode ? "data.db" : */"data.db")},
};
var conSets = config.ConnectionStrings;
foreach (var key in db.Keys) {
var sName = key + "Entities";
var conSet = conSets.ConnectionStrings[sName];
if (conSet != null)
conSets.ConnectionStrings.Remove(conSet);
conSet = new ConnectionStringSettings();
conSet.ProviderName = "System.Data.EntityClient";
conSet.Name = sName;
conSet.ConnectionString =
string.Format("metadata=res://*/{0}Model.csdl|res://*/{0}Model.ssdl|res://*/{0}Model.msl;" +
"provider=System.Data.SQLite;" +
"provider connection string=\"data source={1}\"", key, db[key]);
conSets.ConnectionStrings.Add(conSet);
var file = new FileInfo(db[key]);
if (false && !file.Exists) {
using (BinaryWriter binWriter =
new BinaryWriter(File.Open(db[key], FileMode.Create))) {
switch (key) {
case "ec":
binWriter.Write(Properties.Resources.kladr, 0, Properties.Resources.kladr.Length);
break;
}
}
}
}
conSets.SectionInformation.ForceSave = true;
conSets.SectionInformation.RequirePermission = false;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(conSets.SectionInformation.Name);
}