How to work with multiple database connection and repository pattern

asp.net-mvc entity-framework-6 repository-pattern

Question

I have a c# project with EF and repository pattern.

For single database everything work's fine but I have different model which are related to different database like User model data are come from control panel database and other model are also come from different database.

Here I use common repository pattern for the project. Now how can I send the database connection string to the repository when I initialize the model?

Here is my repository pattern :

public  class Repository<C,T> : IRepository<T> where T : class where C : DbContext, new()
{
    private C _context = new C();
    public C context
    {

        get
        {

            //if (_context == null)
            //{
            //    _context = new C();
            //    _context.Database.Connection.ConnectionString = "the new connectionstring";
            //}
            //return dataContext;
            return _context;
        }
        set { _context = value; }
    }
    private IDbSet<T> _entities;
    private IDbSet<T> Entities
    {
        get
        {
            if (_entities == null)
                _entities = context.Set<T>();
            return _entities;
        }
    }

And this is my Service class

public class UserService: Repository<DyeingContext,User> , IUserRepository
{
    public UserService()
    {
        DyeingContext d = new DyeingContext(DataBase.ControlPanal);
    }
    //private readonly IRepository<User> _useRepository = new Repository<User>(new DyeingContext(DataBase.ControlPanal));   
}``

and here is my context class

 public partial class DyeingContext:DbContext
{
  public  DyeingContext(string pDbName):base(GetTheContext(pDbName))
  {

  }

    public DyeingContext():base()
    {
        //throw new NotImplementedException();
    }

    public static string GetTheContext(string pDbName)
  {
      return ConnectionSettings.GetConnectionStringByDbName(pDbName);
  }
}

I cant get the connection string in here

DyeingContext d = new DyeingContext(DataBase.ControlPanal);

it says

database connection through exception of type system.argumentexception

Is there a way to pass the multiple connections strings to the repository?

How and where I should initialize my connection string and how I pass it through rerpository?

1
1
4/23/2017 6:10:00 AM

Popular Answer

I am not sure if you received your answer from the above comments. But here is how i would implement that.

1- Create a dbContext concrete class for each database. Both inheriting from EF DbContext.

2- In config file, create a connection string key for each database. This key is then supplied to each dbContext class as ctr param. As follows:

public DB1Context()
            : base("db1") // connection string key
        {
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }

public DB2Context()
            : base("db2") // connection string key
        {
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }

3- Each database must have its own repositories. For instance, AccountRepository inherits from the base repository. Its db context is supplied on contructor init as follows.

 public class AccountRepository : DataRepositoryBase<Account>, IAccountRepository
    {
        private DB1Context db1Context = new DB1Context();
        public AccountRepository()
        {
            this.DBContext = db1Context;
        }
    }

This way, you can communicate with multiple databases through its repositories. The same engine / service can inject repositories from different databases.

0
4/23/2017 12:59:24 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow