How to work with multiple database connection and repository pattern

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

Popular Answer

I'm not sure whether the comments up above provided the solution you were looking for. But here is how I would put it into practise.

For each database, create a concrete class called dbContext. They both descended from EF DbContext.

Create a connection string key for each database in the configuration file. Each dbContext class is then given this key as a ctr argument. the following

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. There must be a repository for each database. AccountRepository, for example, derives from the basic repository. On function Object() { [native code] } init, its database context is provided as seen below.

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

Through its repositories, you may interact with many databases in this manner. Several separate databases' repositories may be injected by the same engine or service.

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