0

Here some parts of my code class NinjectWebCommon place where I need bind dada contexts. It is just piece of the code, not complete classes.

private static void RegisterServices(IKernel kernel)
        {
            kernel.BindSharpRepository();
            RepositoryDependencyResolver.SetDependencyResolver(new NinjectDependencyResolver(kernel));

            kernel.Bind<DbContext>().To<EntitiesDbOne>().InRequestScope();

        //kernel.Bind<DbContext>().To<EntitiesDbTwo>().InRequestScope();
    }  

Category Repository class where i need to have two databases in same time

public class CategoryRepository : ConfigurationBasedRepository<CategoryData, int>, ICategoryRepository
    {
    private readonly EntitiesDbOne _ctxOne;
        private readonly EntitiesDbTwo _ctxTwo;

    public CategoryRepository(EntitiesDbOne ctxOne, EntitiesDbTwo ctxTwo)
        {
            _ctxOne= ctxOne;
            _ctxTwo= ctxTwo;
        }

    public CategoryData GetById(int Id)
        {
             //dummy data, just for usage two different dcContexts
             var category = _ctxOne.Categories.Include((string) (x => x.MetaTags)).FirstOrDefault(x => x.Id == Id);
             var categoryName = _ctxTwo.category.FirstOrDefault(x => x.Id == category.Id);

            return category;
        }

In my project I use SharpRepository(ConfigurationBasedRepository) and I use UnitOfWork. I think I can skip UnitOfWork because EntityFramework is doing all this (UnitOfWork patterns) job. Booth of databases is EntityFramework one is CodeFirst approach other (DbTwo) modelFirst

public class EntitiesDbOne : DbContext
    {
        public DbSet<CategoryData> Categories { get; set; }
}

public partial class EntitiesDbTwo: DbContext
    {
        public EntitiesDbTwo()
            : base("name=EntitiesDbTwo")
        {
        }

        public DbSet<attributenames> attributenames { get; set; }
        public DbSet<category> category { get; set; }
}

Please give me some links to examples I can use, I do not think I can manage this with simple explanation. Before I wrote this question, I had search for the answer here on the Multiple DbContexts in N-Tier Application

EF and repository pattern - ending up with multiple DbContexts in one controller - any issues (performance, data integrity)?

This question by the name is complete in my situation but the code for me, is far different. Multiple dbcontexts with Repository, UnitOfWork and Ninject

This one is multiple databases but use one per request, and I need two databases in the same request. http://blog.staticvoid.co.nz/2012/1/9/multiple_repository_data_contexts_with_my_repository_pattern site and other place.

I read about 20 suggestions, there are two close to my situation but probably not enough.

Community
  • 1
  • 1
infoexpert.it
  • 345
  • 1
  • 5
  • 17
  • It feels to me like this need falls outside of the generic repository concept. Inheriting from ConfigurationBasedRepository gives you 2 benefits really. First, you can change the underlying datastore (change from EfRepository to InMemoryRepository or XmlRepository for testing, etc.) and secondly you can configure and change caching options from the config file. I don't see you getting any of those benefits when it comes to the method that uses 2 DbContexts. Personally, I would move this to a separate class. Use SharpRepository when it makes sense but don't try to force it when it doesn't. – Jeff Treuting Aug 04 '14 at 04:37

1 Answers1

0

just as a basis for discussion:

What if you use:

kernel.Bind<EntitiesDbOne>().ToSelf().InRequestScope();
kernel.Bind<EntitiesDbTwo>().ToSelf().InRequestScope();

and use it like

public class CategoryRepository : ConfigurationBasedRepository<CategoryData, int>,    ICategoryRepository
{

    public CategoryRepository(EntitiesDbOne ctxOne, EntitiesDbTwo ctxTwo)
    {
    }
}

what's the issue then?

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • It is no work. You can see. Second binding is commented out. //kernel.Bind().To().InRequestScope(); – infoexpert.it Aug 04 '14 at 09:20
  • yes i can see that it is commented out. But why does it not work? What is the issue? I mean it's certainly not a ninject issue because the binding works perfectly fine, so what is it exactly that doesn't work? – BatteryBackupUnit Aug 04 '14 at 09:32
  • It is good question :) (why does it not work?), but I do not know. I Wrote before, I had read about 20 suggestions, and I've tried most of them and got 20 different issues. I this particular one "ActivationException from Ninject, More than one matching bindings are available." This question is for those who have experience with same situation or can simulate the alike one and try to solve it. Thanks. – infoexpert.it Aug 06 '14 at 06:35
  • I actually do have quite a lot of experience with similar scenarios, albeit not exactly the same. If you'd put in some more concentration you'd remarked that what i suggested in the "answer" differs from what you did. With what you did, it's 100% clear that you'll get said `ActivationException` (more than one bindings available), since you _did_ create two bindings for `DbContext`! My suggestions however does not feature a binding for `DbContext` at all, but rather (exactly) one for `EntitiesDbOne` and one for `EntitiesDbTwo`. To sum it up, I suggest that you adjust your attitude. – BatteryBackupUnit Aug 06 '14 at 06:58
  • Than, you exactly person I need. Can you post some code, where you can bind with Ninject two database, I my case one is EF Code First other Model First and have access to them in one request. Thanks. – infoexpert.it Aug 06 '14 at 07:05
  • I'm not very well acquainted with EF itself. So again: see the above answer! If that does not work, tell me exactly why, so then i can adjust the answer/solution to suit your needs. You could also upload a complete minimal solution (VS solution, project, ...) which replicates the issue. – BatteryBackupUnit Aug 06 '14 at 07:22