Exemplo n.º 1
0
        protected async Task <int> Update(TEntity entity, IPrimaryKey pk)
        {
            int rows = 0;

            try
            {
                if (dbc.Connection.State != ConnectionState.Open)
                {
                    await dbc.Open();
                }

                if (unitOfWork != null)
                {
                    await unitOfWork.Enlist();
                }

                using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection))
                {
                    if (SqlCommandType == Constants.DBCommandType.SPROC)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                    }
                    MapFromObject.Execute(entity, cmd);
                    cmd.Parameters.Add(new SqlParameter("@pk", pk.Key));
                    rows = await cmd.ExecuteNonQueryAsync();
                }
            }
            catch (SqlException ex)
            {
                logger.LogError(ex.Message);
                if (unitOfWork != null)
                {
                    logger.LogError("Transaction will be rolled back.");
                }
                //Rethrow the exception
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message);
            }
            logger.LogInformation($"Update complete for {typeof(TEntity)} entity.");
            return(rows);
        }
Exemplo n.º 2
0
        protected async Task <object> Add(TEntity entity, PrimaryKey pk)
        {
            object result = null;
            int    rows   = 0;

            try
            {
                if (dbc.Connection.State != ConnectionState.Open)
                {
                    await dbc.Open();
                }

                if (unitOfWork != null)
                {
                    await unitOfWork.Enlist();
                }

                using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection))
                {
                    if (SqlCommandType == Constants.DBCommandType.SPROC)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                    }
                    MapFromObject.Execute(entity, cmd);

                    //If Composite, then returns an array of objects
                    if (pk.IsComposite)
                    {
                        //returns CompositeKey
                        rows = await cmd.ExecuteNonQueryAsync();

                        result = rows;
                    }
                    //If Identity, then it's numeric
                    else if (pk.IsIdentity)
                    {
                        //returns PK
                        result = await cmd.ExecuteScalarAsync();
                    }
                    else
                    {
                        //Else it's a natural key, so return rows added
                        cmd.Parameters.Add(new SqlParameter("@pk", pk.Key));
                        rows = await cmd.ExecuteNonQueryAsync();

                        result = rows;
                    }
                }
            }
            catch (SqlException ex)
            {
                logger.LogError(ex.Message);
                if (unitOfWork != null)
                {
                    logger.LogError("Transaction will be rolled back.");
                }
                //Rethrow the exception
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message);
            }

            logger.LogInformation($"Add complete for {typeof(TEntity)} entity.");
            return(result);
        }