Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Multiple Database #4350


User avatar
0
verrrrrrrrrrrrrdi created

Hi,

How to use multiple database on multiple db context ? DbContextA - DatabaseA DbContextB - DatabaseB

I'm using ASP.Net MVC 5 + Angularjs 1.x

Please help


14 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can check this sample <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextDemo">https://github.com/aspnetboilerplate/as ... ontextDemo</a>.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi,

    It require MSDTC. Azure doesn't support MSDTC. can you provide other solution to use multiple database and multiple dbcontet using this: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1706">https://github.com/aspnetboilerplate/as ... ssues/1706</a>

    Thanks

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Any help ??

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you configure IEfTransactionStrategy as explained here <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/EntityFramework-Integration#transaction-management">https://aspnetboilerplate.com/Pages/Doc ... management</a>.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi,

    Is that for multiple dbcontext with single database or multiple dbcontext with multiple database ?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Verrrrrrrrrrrrrdi,

    It is for single DbContext as far as I know. Did you have a chance to try ?

  • User Avatar
    0
    JeffMH created

    Are you using Azure SQL? We are using multiple DB Contexts in Azure SQL. I think they recently released support for this. You may be hitting some other type of DB that isn't supported but just in case:

    <a class="postlink" href="https://azure.microsoft.com/en-us/blog/elastic-database-transactions-with-azure-sql-database/">https://azure.microsoft.com/en-us/blog/ ... -database/</a>

    In our app, we have separate contexts for the Host database and tenant databases. Transactions for sure work between the two. Anyway, this may not help but thought I would post it.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    hi @ismcagdas,

    Yes, i have try it. I has implemented new EF Transaction Strategy using this link: <a class="postlink" href="https://github.com/aspnetboilerplate/as">https://github.com/aspnetboilerplate/as</a> ... ssues/1706

    But as you know my case is different. I have multiple databases and multiple db context. not a single database. can you provide other solution to solve this case ?

    can you give me a sample solution?

    please help me..

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Verrrrrrrrrrrrrdi,

    Would you like to use both dbContexes in the same transaction ? As far as I know, it is not possible because they are pointing to different databases.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @ismcagdas,

    Yes i want to use both dbContext in same transaction (UOW). So you mean the way to solve this case is using MSDTC ?

    Do you have sample implementation with multiple database using ASP.NET Core + Angular template? this sample <a class="postlink" href="https://github.com/aspnetboilerplate/as">https://github.com/aspnetboilerplate/as</a> ... ontextDemo is for ASP.NET MVC 5 + Angular js 1.x

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Verrrrrrrrrrrrrdi,

    Yes i want to use both dbContext in same transaction (UOW). So you mean the way to solve this case is using MSDTC ?

    As far as I know, Azure doesn't support it but it can be achieved in a dedicated server using MSDTC (I'm not %100 sure). We don't have a sample for EntityFramework Core. Did you have a problem ? We can try to help.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @ismcagdas,

    OK i wil take your solution using on premise server and enable MSDTC. I have enable MSDTC in my dedicated server but when i use both dbContext in same transaction it show error:

    The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used.

    If i add the constructor :

    public DbContextB()
            : base("ConnStringB")
        {
    
        }
        
        public DbContextB(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
    
        }
    
        public DbContextB(DbConnection existingConnection)
            : base(existingConnection, false)
        {
    
        }
    
        public DbContextB(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {
    
        }
    

    it show error

    Invalid Object Name dbo.TableB

    because it always use "Default" connection string and TableB is belong to DatabaseB which use DbContextB. can you help me?

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @ismcagdas,

    It solved. Thanks for your help

  • User Avatar
    0
    ismcagdas created
    Support Team

    @Verrrrrrrrrrrrrdi, Great :)