本文介绍了如何使用Autofac连接管理多个数据库。
Model项目修改
首先根据前文提到的方法再次添加一个数据源,别忘记web.congfig的修改。
Repository项目修改
打开Repository/BaseRepository文件,进行代码改写:
using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
namespace MVC5Demo.Repository.Repository
{
public class BaseRepository<T> : IRepository.IBaseRepository<T> where T : class
{
protected DbContext _dbContext;
protected DbSet<T> _dbSet;
public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
{
if (exp == null) return _dbSet;
return _dbSet.Where(exp);
}
}
}
在IRepository新建接口文件IDemoDBRepository和IDemoDB2Repository,并继承IBaseRepository接口,代码如下:
namespace MVC5Demo.Repository.IRepository
{
public interface IDemoDBRepository<T> : IBaseRepository<T> where T : class
{
}
}
namespace MVC5Demo.Repository.IRepository
{
public interface IDemoDB2Repository<T>