Freigeben über


Always Drop and Create Local Database .NET Backend for Azure Mobile Services

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)
        {