Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Always Drop And Create Local Database Net Backend For Azure Mobile Services

- 05 Jun 2014

In my early testing I wanted to always seed my Local Database.  By default the db was only dropped and reseeded if you changed the models.  There is a simple code change to do this.

In WebApiConfig.cs change the class implementing IDatabaseInitializer that you pass into Database.SetInitializer from DropCreateDatabaseIfModelChanges to DropCreateDatabaseAlways

Example

Previous (only updated if DataObject schema changes):

    public static class WebApiConfig
    {
        public static void Register()
        {
            // Use this class to set configuration options for your mobile service
            ConfigOptions options = new ConfigOptions();
            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));
            // To display errors in the browser during development, uncomment the following
            // line. Comment it out again when you deploy your service for production use.
            // config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            Database.SetInitializer(new MobileService3Initializer());
        }
    }
    public class MobileService3Initializer : DropCreateDatabaseIfModelChanges<MobileService3Context>
    {
        protected override void Seed(MobileService3Context context)
        {
            

Now (always recreates the Database and reseeds):

 

   public static class WebApiConfig
    {
        public static void Register()
        {
            // Use this class to set configuration options for your mobile service
            ConfigOptions options = new ConfigOptions();
            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));
            // To display errors in the browser during development, uncomment the following
            // line. Comment it out again when you deploy your service for production use.
            // config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            Database.SetInitializer(new MobileService3Initializer());
        }
    }
    public class MobileService3Initializer : DropCreateDatabaseAlways<MobileService3Context>
    {
        protected override void Seed(MobileService3Context context)
        {

<< Go Back