Base solution for your next web application
Open Closed

Separate DB Contexts #1793


User avatar
0
rafalhc created

Hi,

I want to have two separate DB Contexts. One for write side and second (read only) for read side.

Additionally I need to have ability to set different isolation levels for both and not to use distributed transactions.

What is the best way to do that?


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can have 2 dbcontextes. But there may be some problems. For example;

    • If you inject IRepository<YourEntity> and if YourEntity is included in both dbcontextes, how ABP will know which one to use? In that case, you may not use automatic repositories but you can create custom repositories. In custom repos, dbcontext is known in development time (document: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/EntityFramework-Integration#DocCustomRepositoryMethods">http://www.aspnetboilerplate.com/Pages/ ... oryMethods</a>)

    Transactions are not per dbcontext, but per unit of work (UOW - <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work">http://www.aspnetboilerplate.com/Pages/ ... it-Of-Work</a>). Because in a UOW, you can use both dbcontextes and all operations will be transactional. There is not a system that restricts you to only use single dbcontext in a request or separate transaction per dbcontext. Only option can be inheriting from EfUnitOfWork and implement it yourself (check for dbcontext types and begin transaction based on it's type).

    Another option; You can add [UnitOfWork(IsolationLevel...)] attribute to your app service or/and controller methods to control it.