コード例 #1
0
 /// <summary>
 /// :: Important ::
 /// Aim: Update DB to latest version using currentDBVersion variable.
 /// 
 /// Working: 
 /// Just like migrations in Rails.
 /// Each case in the switch has changes in that specific DB version
 /// default case implies latest version and so no changes
 /// At the end of each case we add goto to next case to fall through until we reach the latest version
 /// 
 /// Notes:
 /// Target version will always be 'one' version higher than the current one. Increments in DB versions is always by 1
 /// All the changes to schema from the targetVersion will be run
 /// </summary>
 private void UpdateDB()
 {
     using (var db = new MedicineContext(App.DbConnection))
     {
         var adapter = db.CreateDatabaseSchemaUpdater();
         switch (GetNextDBVersion(adapter.DatabaseSchemaVersion))
         {
             case 0:
                 // Version 0. Initial DB
                 goto case 1;
             case 1:
                 // Version 1 changes
                 // New Columns
                 adapter.AddColumn<Medicine>("IsUsing");
                 adapter.AddColumn<Medicine>("Quantity");
                 adapter.AddColumn<Medicine>("QuantityType");
                 adapter.AddColumn<Medicine>("ReminderIds");
                 adapter.AddColumn<Medicine>("DosageType");
                 // New Tables
                 adapter.AddTable<Group>();
                 adapter.AddTable<Medicine_Group>();
                 goto default;
             default:
                 // DB upto date
                 break;
         }
         adapter.DatabaseSchemaVersion = currentDBVersion;
         adapter.Execute();
     }
 }
コード例 #2
0
 /// <summary>
 /// Creates the Medicines Catalogue DB if it doesn't exist
 /// Adds a sample medicine
 /// </summary>
 private void CreateDB()
 {
     using (var db = new MedicineContext(DbConnection))
     {
         if (!db.DatabaseExists())
         {
             db.CreateDatabase();
             InsertSampleData(db);
             db.SubmitChanges();
             var adapter = db.CreateDatabaseSchemaUpdater();
             adapter.DatabaseSchemaVersion = currentDBVersion;
             adapter.Execute();
         }
     }
 }