Multi-tenant in SQL Azure using federation

3 posts, 0 answers
  1. Mike
    Mike avatar
    7 posts
    Member since:
    Sep 2009

    Posted 26 Apr 2012 Link to this post

    I'm doing some experiments on using SQL Azure federations to enable muti-tenancy in a web app with Openaccess. The main thing I'm trying to accomplish is making sure to run
    string federationCmdText = String.Format(@"USE FEDERATION FED_1 (TenantID={0}) WITH FILTERING=ON, RESET", tenantID);
    context.ExecuteNonQuery(federationCmdText);
    on every connection, before any subsequent queries on that connection are executed.
    How can I accomplish this with Openaccess?

    Thanks,
    Mike
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 27 Apr 2012 Link to this post

    Hello Mike,
    You can set SQL statements that are executed whenever we open a new connection. The list is bound to the backend configuration in your generated context class.

    Please add a partial class to your generated context class, override OnDatabaseOpen and add your string to

    backendConfiguration.ConnectionPool.InitSQL.Add("...");

    Greetings,
    Jan Blessenohl
    the Telerik team
    Follow @OpenAccessORM Twitter channel to get first the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
  4. Mike
    Mike avatar
    7 posts
    Member since:
    Sep 2009

    Posted 27 Apr 2012 Link to this post

    Worked great thank you. This is what I ended up doing:

    public partial class EntitiesModel
        {
     
            Guid tenantID;
             
            public EntitiesModel(Guid TenantID)
                : base(connectionStringName, backend, metadataSource)
            {
                this.tenantID = TenantID;
            }
     
            protected override void OnDatabaseOpen(Telerik.OpenAccess.BackendConfiguration backendConfiguration, Telerik.OpenAccess.Metadata.MetadataContainer metadataContainer)
            {
     
                string federationCmdText = String.Format(@"USE FEDERATION FED_1 (TenantID='{0}') WITH FILTERING=ON, RESET", tenantID.ToString());
                backendConfiguration.ConnectionPool.InitSQL.Add(federationCmdText);
     
                base.OnDatabaseOpen(backendConfiguration, metadataContainer);
     
            }
     
        }

Back to Top