0

I am having a project and my project is connecting to two different databases are BookStoreEntities and BlogEntities.

If I remove line code builder.RegisterType<BlogEntities>().As<DbContext>(); in Autofac configuration my project works fine and else I'll get error "The entity type Book is not part of the model for the current context".

My autofac config:

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());

builder.RegisterType<BookStoreEntities>().As<DbContext>();
builder.RegisterType<BlogEntities>().As<DbContext>();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
builder.RegisterType<BookService>().As<IBookService>();

builder.RegisterFilterProvider();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

Repository class:

public class Repository<T> : IRepository<T> where T : class
{
    private DbContext _dbContext;
    private DbSet<T> _dbSet;

    public Repository(DbContext dbContext)
    {
        _dbContext = dbContext;
        _dbSet = dbContext.Set<T>();
    }

    public IEnumerable<T> GetAll()
    {
        return _dbSet;
    }
}

Service layer:

public class BookService : IBookService
{
    private IRepository<Book> _bookRepository;

    public BookService(IRepository<Book> bookRepository)
    {
        _bookRepository = bookRepository;
    }

    public IEnumerable<Book> GetBooks()
    {
        return _bookRepository.GetAll();
    }
}

Controller:

public class BookController : Controller
{
    private IBookService _bookService; 

    public BookController(IBookService bookService)
    {
        _bookService = bookService;
    }

    // GET: Book
    public ActionResult Index()
    {
        var books = _bookService.GetBooks();

        return View(books);
    }
}
0

2 Answers 2

2

My Project is using 2 different databases and Service layer will implement from this Generic Repository. I want to myservice1 works with MyDbContext1 and myservice2 works with MyDbContext2

Then don't new your DbContext inside your repository. That makes testing hard anyway.

Inject it:

public Repository(DbContext dbContext)
{
    _dbContext = dbContext;
}

Now the repository doesn't care which DbContext-derived class is injected. This works because you only call DbContext.Set<T>().

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this way and after I used Dependency Injection. can I register MyDbContext1 and MyDbContext2 as DbContext
Maybe pretty obvious, but in service1 configure your IOC container to inject MyDbContext1, and in service2 configure your IOC container to inject MyDbContext2.
No, you show your current code in your question first. :) I don't know what your "services" are nor how you currently set up your container.
I have resolved issue with this example github.com/raghav-rosberg/UnitOfWorkWithMultipleDBContext
0

Try to something like:

public class MyDbContext1  : DbContext   
{
  public MyDbContext1 ()
    :base("ConnectionString")
  { }

  public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
  {
    return base.Set<TEntity>();
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
   //...
  }
}

And add to repository new property

private IDbSet<T> Entities
{
  get
  {
    if (_dbSet == null)
    {
      _dbSet = _dbContext1.Set<T>();
    }
    return _dbSet;
  }
}

3 Comments

yes I know, but current I want to add more a Context which called is MyDbContext2 and I'll implement in generic repository.
It is a strange idea but what is the problem to add another dbcontexts ?
I have updated my question and I hope so obvious for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.