locked
Multitenant Application set connection string RRS feed

  • Question

  • User-485744083 posted

    Hi

    I am creating a multitenant application that will store the users data either in separate databases or a single database using a TenantId to query data.

    I have got as far as the user being able to register using the asp identity framework and I store the tenantId and connection string info with the user and subsequently store this in a claims token.

    My question is what's the best way to set this id and connection string dynamically. I have a Product repository that uses a base repository that creates the dbContext and sets the connection string. Do I just pass the Id and Connection String into the controller and onto the Product repository and then onto the base repository from the claims token.

    Any advice would be greatly appreciated.

    Thanks

    John 

    Monday, August 4, 2014 3:18 PM

Answers

  • User-2007877324 posted

    Do I just pass the Id and Connection String into the controller and onto the Product repository and then onto the base repository from the claims token

    Yes, I believe that is the right approach. Pass the connection string to the DBContext constructor. May be your code looks like this

    public ProductRepository : BaseRepository
    {
        public ProductRepository(connectionstring) : base(connectionstring)
        {}
    }
    
    Public BaseRepository : DBContext
    {
        public BaseRepository(connectionstring) : base(connectionstring)
        {}
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 5:53 AM

All replies

  • User197322208 posted

    You can modify the connection string before sending to repository

    Monday, August 4, 2014 9:17 PM
  • User-2007877324 posted

    Do I just pass the Id and Connection String into the controller and onto the Product repository and then onto the base repository from the claims token

    Yes, I believe that is the right approach. Pass the connection string to the DBContext constructor. May be your code looks like this

    public ProductRepository : BaseRepository
    {
        public ProductRepository(connectionstring) : base(connectionstring)
        {}
    }
    
    Public BaseRepository : DBContext
    {
        public BaseRepository(connectionstring) : base(connectionstring)
        {}
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 5:53 AM
  • User-485744083 posted

    Thank you for the replies.

    Yes my code will look very similar but that I have an Interface for the repository but I guess that just adds another layer.

    You've given me the confidence that I'm on the right track this is all very new to me.

    Thanks again

    John

    Tuesday, August 5, 2014 8:06 AM