如何使用每个请求范围的Ninject注册多个DbContexts [英] How to register multiple DbContexts using Ninject with per request scope

查看:92
本文介绍了如何使用每个请求范围的Ninject注册多个DbContexts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Generic Repository与数据库进行交互(我正在使用EF6.0)



我正在使用两个数据库。



Ninject一直用来实施DI。



下面的示例代码片段。

1。通用存储库

I'm using Generic Repository to interact with Database(I'm using EF6.0)

I'm using two Databases.

Ninject has been using to implement DI.

Sample code snippet below.
1. Generic Repository

/// Genertic Repository absract class with class and context
  /// </summary>
  /// <typeparam name="C"></typeparam>
  /// <typeparam name="T"></typeparam>
  public class   GenericRepository<T> :
  IGenericRepository<T>
      where T : class
  {

      public GenericRepository(DbContext DbContext)
      {
          _entities = DbContext;
      }

      private DbContext _entities;







2.数据库上下文(我想为另一个数据库再引入一个DBContext)




2. DB Context (I want to introduce one more DBContext for another Database)

/// <summary>
   /// Administrative context
   /// </summary>
   public class DatabaseContext: DbContext
   {
       /// <summary>
       /// Reference Code Value DataSet
       /// </summary>
       public DbSet<ReferenceCodeValue> ReferenceValues { get; set; }
       /// <summary>
       /// Reference Code Type DataSet
       /// </summary>
       public DbSet<ReferenceDataType> ReferenceTypes { get; set; }
       ///<summary>
       /// Static Constructor
       /// </summary>
       static DatabaseContext()
       {
           Database.SetInitializer<DatabaseContext>(null);
       }





3.使用Ninject注册数据库上下文



3. Register the DB Context using Ninject

internal static class DependencyRegistry
    {
        [DependencyRegistrar]
        public static void Register(Dependency Container)
        {
            Container.
            RegisterPerRequestScopeType<DbContext, DatabaseContext>();
        }
    };





4.域名服务 - 我通过构造函数注入调用通用存储库





4. Domain Service-where I'm invoking the generic repository through constructor injection

/// Concrete Class AdministrativeManager which implements IAdministrativeManager
    /// </summary>
    internal class AdministrationDomain : DomainElement, IAdministrationDomain
    {
        private IGenericRepository<DTO.ReferenceDataType> _referenceTypeRepo;
        private IGenericRepository<DTO.ReferenceCodeValue> _referenceValueRepo;

        public AdministrationDomain(
            IGenericRepository<DTO.ReferenceDataType> referenceTypeRepo,
            IGenericRepository<DTO.ReferenceCodeValue> referenceValueRepo)
        {
            _referenceTypeRepo = referenceTypeRepo;
            _referenceValueRepo = referenceValueRepo;
        }







我想使用相同的IGenericRepository和两个实现,一个用于Database1和另一个数据库2.



我尝试过命名绑定注册




I want to use the same IGenericRepository with two implementations one for Database1 and another one Database2.

I tried Named binding to register

Container.
            RegisterPerRequestScopeType&lt;DbContext, DatabaseContext&gt;().Named("DB1";




Container.
            RegisterPerRequestScopeType&lt;DbContext, AppContext&gt;().Named("DB2");







DatabaseContext已被AppContext覆盖。我怎么解决这个问题?




the DatabaseContext has been overriden by AppContext. how I can resolve the issue?

推荐答案

感谢您的支持。解决了这个问题Ninject中的上下文绑定
thanks for your support. Resolved the issue using Contextual Binding in Ninject


这篇关于如何使用每个请求范围的Ninject注册多个DbContexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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