コード例 #1
0
        void VerifyDatabase()
        {
#if WPF || WinForms || ASP
            string initialPath  = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), "InitialData.xml");
            bool   isExists     = dashboardContext.Database.Exists();
            bool   isCompatible = isExists && dashboardContext.Database.CompatibleWithModel(false);
            if (!isCompatible)
            {
                if (isExists)
                {
                    dashboardContext.Database.Delete();
                    dashboardContext = new DashboardContext();
                }
                dashboardContext.Database.Create();
                model = Model.GetPopulated(initialPath);
                dashboardContext.Seed(model);
            }
#elif WINDOWS_UWP
            dashboardContext.Database.EnsureDeleted();
            dashboardContext.Database.Migrate();
            Assembly assembly = Assembly.Load(new AssemblyName("DashboardUWP"));
            var      stream   = assembly.GetManifestResourceStream("DashboardUWP.InitialData.xml");
            model = Model.GetPopulated(stream);
            dashboardContext.Seed(model);
#endif
        }
コード例 #2
0
        void VerifyDatabase()
        {
#if WPF || WinForms || ASP
            string initialPath = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), "InitialData.xml");
#if NETCORE
            if (dashboardContext.Database.EnsureCreated())
            {
                model = Model.GetPopulated(initialPath);
                dashboardContext.Seed(model);
            }
#else
            bool isExists     = dashboardContext.Database.Exists();
            bool isCompatible = false;
            try
            {
                isCompatible = isExists && dashboardContext.Database.CompatibleWithModel(false) && dashboardContext.Products.Count() > 0;
            }
            catch { }
            if (!isCompatible)
            {
                try
                {
                    if (isExists)
                    {
                        try
                        {
                            dashboardContext.Database.Delete();
                        }
                        catch { }
                        dashboardContext = new DashboardContext();
                    }
                    dashboardContext.Database.Create();
                }
                catch
                {
                    try
                    {
                        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), dashboardContext.Database.Connection.Database);
                        File.Delete(path + ".mdf");
                        File.Delete(path + "_log.ldf");
                        dashboardContext.Database.Create();
                    }
                    catch (Exception ex)
                    {
                        Error = ex.Message;
                        return;
                    }
                }
                model = Model.GetPopulated(initialPath);
                dashboardContext.Seed(model);
            }
#endif
#elif WINDOWS_UWP
            dashboardContext.Database.EnsureDeleted();
            dashboardContext.Database.Migrate();
            Assembly assembly = Assembly.Load(new AssemblyName("DashboardUWP"));
            var      stream   = assembly.GetManifestResourceStream("DashboardUWP.InitialData.xml");
            model = Model.GetPopulated(stream);
            dashboardContext.Seed(model);
#endif
        }