示例#1
0
        public static Database Open(string dbName)
        {
            bool fRebuild = false;

            // In the event that we call DeleteDatabase, we need to make sure it gets closed.
            using (Database db = new Database(dbName))
            {
                if (!db.DatabaseExists())
                    fRebuild = true;

                if (!fRebuild)
                {
                    try
                    {
                        if (db.Configurations.Count() < 1 || db.Configurations.First().Version != Configuration.CurrentVersion)
                            fRebuild = true;
                    }
                    catch
                    {
                        fRebuild = true;
                    }

                    if (fRebuild)
                    {
                        db.DeleteDatabase();
                    }
                }
            }

            Database result = new Database(dbName);
            if (fRebuild)
            {
                result.CreateDatabase();

                Configuration cfg = new Configuration() { Version = Configuration.CurrentVersion };
                result.Configurations.InsertOnSubmit(cfg);
                result.SubmitChanges();
            }
            return result;
        }