我有一個多個應用程序,它具有多個數據庫,用於具有相同db結構的不同客戶。EF在同一類型的不同上下文之間切換
在實際應用中,用戶登錄時根據自己的客戶ID,我會知道他的客戶數據庫,並在他的登錄按鈕單擊事件我需要動態改變我的分貝範圍內的飛行連接字符串之後。
我正在使用EF 5.0和Autofac IOC容器。 我在這裏粘貼了我的代碼,這對我沒有幫助。
什麼是我可以管理的最佳方式?
string EntityFrameworkConnectionString = null;
var builder = new ContainerBuilder();
builder.Register(c =>
{
if (string.IsNullOrEmpty(EntityFrameworkConnectionString))
{
var profileProvider = c.Resolve<IConfigurationProfileProvider<CustomerProfile>>();
var profile = profileProvider.GetProfile();
EntityFrameworkConnectionString = profile.CustomerDatabaseConnectionString;
}
return new CustomerDataContext(EntityFrameworkConnectionString);
})
.As<ICustomerDataContext>()
.As<IDbContext>()
.InstancePerDependency();
builder.RegisterType<CustomerDataContextFactory>().As<ICustomerDataContextFactory>();
var assembly = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerDependency();
爲什麼在註冊委託之外留下EntityFrameworkConnectionString變量?由於關閉,這很糟糕。 –