Switching Model Backend

3 posts, 1 answers
  1. Ryan
    Ryan avatar
    9 posts
    Member since:
    Sep 2012

    Posted 24 Apr 2015 Link to this post

    Hello. I'm using an database-first entities model connected to SQL Server 2014 in dev, and SQL Azure in production.

    Is there an easier way to switch the Backend setting to Azure on deployment?

    Currently I have to manually go to the model, right-click Show Model Settings, set the Backend to Azure and save. (Then manually change it back after deploying)

    Cheers,

    Ryan

  2. Answer
    Simeon Simeonov
    Admin
    Simeon Simeonov avatar
    24 posts

    Posted 29 Apr 2015 Link to this post

    Hello Ryan,

    Thank you for contacting us.

    Yes it is possible to base the model settings on configuration and as a result have different settings for local development and production. This is done by extending the context class for your domain model (the EntitiesModel partial class generated by the Rlinq). How you can do this is explained here. Please note that you will have to add your implementation in the same assembly where your model is and your extension partial class should be with the same name as the context partial class.

    In your extension partial class you can extend the CustomizeBackendConfiguration method and in it you can set the Backend settings based on environment configurations. You can set the Backend like this:

    public partial class EntitiesModel
    {
        static partial void CustomizeBackendConfiguration(ref Telerik.OpenAccess.BackendConfiguration config)
        {
            config.Backend = "Azure";
            //backend.Backend = "MsSql";
        }
    }


    1/ One possible way to determine programmatically if you are running on local environment or Azure is like this:
    public partial class EntitiesModel
    {
        static partial void CustomizeBackendConfiguration(ref Telerik.OpenAccess.BackendConfiguration config)
        {
            // Local environment (Azure emulated or unit tests)
            if (RoleEnvironment.IsAvailable == false || RoleEnvironment.IsEmulated)
            {
                backend.Backend = "MsSql";
            }
            // Azure - production environment
            else
            {
                config.Backend = "Azure";
            }
        }
    }

    Please note that this solution couples your implementation with Azure, so it is advisable to extract that conditional check (is local environment) in an external component which you can replace.

    2/ Another solution is to base this code on the Azure configuration store functionality which allows you to set different configurations depending on the platform on which the application is running. How you can do this is explained here and here. You can set different configuration settings in the ServiceConfiguration.Cloud.cscfg and the ServiceConfiguration.Local.cscfg configuration files in your Azure cloud service. Then using the CloudConfigurationManager.GetSetting() method you can resolve the Backend and all other platform dependent settings from that configurations files.

    Also please note that your connection string is stored in the App.Config, so it can also be resolved depending on the platform you project is running on.

    I hope you find this information helpful. If you have any further questions please don't hesitate to contact us.

    Regards,
    Simeon Simeonov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Ryan
    Ryan avatar
    9 posts
    Member since:
    Sep 2012

    Posted 29 Apr 2015 in reply to Simeon Simeonov Link to this post

    Thanks, this works great.

    I put the backend into my app config so that it's automatically set to Azure when my web app is published.

    static partial void CustomizeBackendConfiguration(ref BackendConfiguration config)
    {           
          config.Backend = ConfigurationManager.AppSettings["TelerikDataAccessBackend"];
    }

    Cheers,

    R

Back to Top