在多个数据库上使用实体框架 [英] Using entity framework on multiple databases

查看:74
本文介绍了在多个数据库上使用实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个工资单系统,将与现有的系统集成。原始系统具有处理用户管理和一些全局配置的主数据库,下面有多个数据库,每个数据库的结构相同,基本上每个数据库是一个公司的工资单数据库,所有这些数据库都绑定到主数据库,因为它属于一个父有很多子公司的公司都有自己的人力资源部门。



我想知道的是,如果有任何方法,我可以根据一个cookie或另一种方法存储他们希望连接的公司,使用过滤器根据他们的输入动态地更改实体框架目标数据库。



这里有一个例子:



用户A登录到网站,页面加载用户有权访问的可用公司,用户将选择一个公司,他们在该公司具有管理员权限,他们添加一个员工,之前那个操作是运行的,asp.net会将连接字符串切换到适当的数据库,然后添加记录。

解决方案

EF6从相同的上下文更好地支持多个数据库访问。这是EF5的片段。
以前管理数据库初始化设置很重要。
您可能不想触发任何迁移。
ie,在



Database.SetInitializer(new ContextInitializerNone< MyDbContext>());



但是回答这个问题:是的,你可以

 code> var conn = GetSqlConn4DbName(dataSource,dbName); 
var ctx = new MyDbContext(conn,true);



public DbConnection GetSqlConn4DbName(string dataSource,string dbName){
var sqlConnStringBuilder = new SqlConnectionStringBuilder();
sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource)? DefaultDataSource:dataSource;
sqlConnStringBuilder.IntegratedSecurity = true;
sqlConnStringBuilder.MultipleActiveResultSets = true;

var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
var sqlConn = sqlConnFact.CreateConnection(dbName);
return sqlConn;
}


public class ContextInitializerNone< TContext> :IDatabaseInitializer< TContext>其中TContext:DbContext
{
public void InitializeDatabase(TContext context){}
}

还可以使用迁移,示例代码和动态数据库连接查看StackOverflow答案


I am writing a payroll system that will integrate with a pre-existing system. The original system had a master database that handled user management and some global configuration, below that there are multiple databases each identical in structure, basically each database is one companies payroll database, all these are tied to the main database because it belongs to a parent company who has many subsidiaries each with their own HR department.

What I was wondering is if there is any way that I can, based on either a cookie or another method that stores what company they wish to connect to, dynamically change the entity frameworks target database based on their input using a filter?

Here's an example:

User A logs in to the site, page loads with available companies that the user has permission to access, user will then select a company, they have admin privileges in that company, they add an employee, before that action is run, asp.net will switch the connection string to the appropriate database then add the record.

解决方案

EF6 has better support for multiple DB access from Same context. Here is a snippet from EF5. Managing the database initializer setting prior is important. You may not want to trigger ANY migrations. i.e, use this before

Database.SetInitializer(new ContextInitializerNone<MyDbContext>());

but to answer the question: Yes you can

var conn = GetSqlConn4DbName(dataSource,dbName );
var ctx = new MyDbContext(conn,true);



public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
        var sqlConnStringBuilder = new SqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;

        var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    }


 public class ContextInitializerNone<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext
{
    public void InitializeDatabase(TContext context) {  }
}

Also see StackOverflow answer using migration, sample code, and dynamic db connection

这篇关于在多个数据库上使用实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆