private static void EnsureRepositoryVersionIsCorrect(DefaultRepositoryProvider seedContext, InitializeArgs args)
        {
            var versionSetting = seedContext.Settings.FirstOrDefault(s => s.Key == "RepositoryVersion");

            if (versionSetting == null)
            {
                versionSetting          = new SettingDto();
                versionSetting.Key      = "RepositoryVersion";
                versionSetting.IsSystem = true;
                seedContext.Settings.Add(versionSetting);
            }

            var newVersion = args.SchemaVersion.ToString(CultureInfo.InvariantCulture);

            if (!Equals(versionSetting.Value, newVersion))
            {
                versionSetting.Value = newVersion;
            }

            seedContext.SaveChanges();
        }
        public void Initialize(DefaultRepositoryProvider seedContext, InitializeArgs args)
        {
            // EnsureRepositoryIsSeeded
            var  hasChanges = false;
            bool allowChanges;

            try
            {
                allowChanges = Convert.ToBoolean(seedContext.Settings.Where(s => s.Key == "AllowAutomaticRepositoryUpgrades").Select(s => s.Value).FirstOrDefault() ?? "True");
            }
            catch (Exception ex)
            {
                // NOTE: Assume Repository database is not seeded so try to generate everything
                if (ex.Message.Contains("Vidyano.Settings") || (ex.InnerException != null && ex.InnerException.Message.Contains("Vidyano.Settings")))
                {
                    using var transaction = seedContext.Database.BeginTransaction();
                    try
                    {
                        foreach (var script in Resources.RepositorySql.Split(new[] { "GO" }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Where(s => s.Length > 0))
                        {
                            seedContext.Database.ExecuteSqlRaw(script);
                        }

                        seedContext.SaveChanges();

                        hasChanges = true;

                        transaction.Commit();
                    }
                    catch (Exception initEx)
                    {
                        ServiceLocator.GetService <IExceptionService>().Log(initEx);

                        try
                        {
                            transaction.Rollback();
                        }
                        catch
                        {
                            // Ignore exception on rollback
                        }

                        throw;
                    }
                    finally
                    {
                        allowChanges = true;
                    }
                }
                else
                {
                    throw;
                }
            }

            if (UpdateDbSchema(seedContext, allowChanges))
            {
                hasChanges = true;
            }

            if (RepositorySchemaSynchronizer.NewerVersion == null)
            {
                EnsureRepositoryVersionIsCorrect(seedContext, args);

                var currentVersion = seedContext.Settings.Where(s => s.Key == "ServiceVersion").Select(s => s.Value).FirstOrDefault();
                if (args.UpdateMetadata(allowChanges, currentVersion))
                {
                    hasChanges = true;
                }
            }

            if (hasChanges)
            {
                seedContext.SaveChanges();
            }

            args.EnsureTargetSchemaIsSynchronized();
        }
Exemple #3
0
 /// <inheritdoc />
 public virtual void PersistChanges()
 {
     context.SaveChanges();
 }