Esempio n. 1
0
        /// <summary>
        /// Method to update an Entity.
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="pkName"></param>
        /// <param name="entityPK"></param>
        /// <returns>The updated Entity.</returns>
        public async Task <int> SetDefaultAsync(string tableName, string pkName, int entityPK)
        {
            int result = 0;

            log.Warn($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Setting Default => {tableName}.{pkName}");
            log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Entity Primary Key => {entityPK}");

            if (tableName.IsNullOrWhiteSpace())
            {
                ArgumentNullException e = new ArgumentNullException(nameof(tableName));
                log.Error(e.Output(), e);
                return(0);
            }

            if (pkName.IsNullOrWhiteSpace())
            {
                ArgumentNullException e = new ArgumentNullException(nameof(pkName));
                log.Error(e.Output(), e);
                return(0);
            }

            if (entityPK < 0)
            {
                IndexOutOfRangeException e = new IndexOutOfRangeException(nameof(pkName));
                log.Error(e.Output(), e);
                return(0);
            }

            string query = "";

            // Becareful : SQLiteException maybe occurs whithout  ` or/and '.
            //log.Debug(query = $"UPDATE `{tableName}` SET `IsDefault` = 0");
            log.Debug(query = $"UPDATE {QN(tableName)} SET {QN("IsDefault")} = {Q("0")}");
            try
            {
#pragma warning disable EF1000 // Query is already generated by Linq. Becareful ! Linq on Linq cause SQLiteException.
                result = await(Connector as DbContext).Database.ExecuteSqlCommandAsync(query);
#pragma warning restore EF1000 // Query is already generated by Linq. Becareful ! Linq on Linq cause SQLiteException.
                Save();
            }
            catch (SQLiteException se)
            {
                log.Error(query);
                log.Error(se.Output(), se);
                return(0);
            }
            catch (Exception se)
            {
                log.Fatal(query);
                log.Fatal(se.Output(), se);
                return(0);
            }

            log.Info($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Affected Rows => {result}");

            // Becareful : SQLiteException maybe occurs whithout ` or/and '.
            log.Debug(query = $"UPDATE {QN(tableName)} SET {QN("IsDefault")} = {Q("1")} WHERE _rowid_ = {Q(entityPK.ToString())}");
            try
            {
#pragma warning disable EF1000 // Query is already generated by Linq. Becareful ! Linq on Linq cause SQLiteException.
                result = await(Connector as DbContext).Database.ExecuteSqlCommandAsync(query);
#pragma warning restore EF1000 // Query is already generated by Linq. Becareful ! Linq on Linq cause SQLiteException.
                Save();
            }
            catch (SQLiteException se)
            {
                log.Error(query);
                log.Error(se.Output(), se);
                return(0);
            }
            catch (Exception se)
            {
                log.Fatal(query);
                log.Fatal(se.Output(), se);
                return(0);
            }

            log.Info($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : {result} Affected Rows");
            return(result);
        }