public virtual void InitializeDatabase()
        {
            var scriptReplacements = new Dictionary <string, string>()
            {
                { "pref", _connectionSettings.Schema }
            };
            var scriptInitializer = new ScriptInitializer(typeof(InstallScripts), scriptReplacements);

            scriptInitializer.ScriptManager.ThrowOnUnknownScriptTypes = false;

            using (SenderDbContext context = GetDbContext())
            {
                var  creator  = (RelationalDatabaseCreator)context.Database.GetService <IDatabaseCreator>();
                bool dbExists = creator.Exists();
                if (dbExists)
                {
                    bool tableExists = CheckTableExists(context);
                    if (tableExists == false)
                    {
                        ScriptExtractor.ExtractFromDbContext(context);
                        scriptInitializer.ExecuteScripts(context);
                    }
                }
                else
                {
                    context.Database.EnsureCreated();
                    scriptInitializer.ExecuteScripts(context);
                }
            }
        }
        public virtual bool CheckTableExists(SenderDbContext context)
        {
            string sql = $@"
                SELECT 1 FROM sys.tables AS T
                INNER JOIN sys.schemas AS S ON T.schema_id = S.schema_id
                WHERE S.Name = '{_connectionSettings.Schema}' AND T.Name = '{DefaultTableNameConstants.DispatchTemplates}'";

            using (DbConnection connection = context.Database.GetDbConnection())
            {
                connection.Open();
                using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    object result     = command.ExecuteScalar();
                    int    intResult  = (int)result;
                    bool   boolresult = intResult > 0;
                    return(boolresult);
                }
            }
        }