TryGet, TryUpdate, and TryDelete Operations

These scenarios demonstrate the difference between operations that fail silently and operations that fail with an exception.

Scenario Prototype

public interface ITryCrudScenario<TEmployeeClassification>
   where TEmployeeClassification : class, IEmployeeClassification, new()
{
    /// <summary>
    /// Create a new EmployeeClassification row, returning the new primary key.
    /// </summary>
    int Create(TEmployeeClassification classification);

    /// <summary>
    /// Delete a EmployeeClassification row using its primary key. If no matching row is found, an exception is thrown.
    /// </summary>
    /// <remarks>The specific exception thrown is not defined.</remarks>
    void DeleteByKeyOrException(int employeeClassificationKey);

    /// <summary>
    /// Delete a EmployeeClassification row using its primary key. Returns true if the row was updated, false if no matching row was found.
    /// </summary>
    bool DeleteByKeyWithStatus(int employeeClassificationKey);

    /// <summary>
    /// Delete a EmployeeClassification row using an object. If no matching row is found, an exception is thrown.
    /// </summary>
    /// <remarks>The specific exception thrown is not defined.</remarks>
    void DeleteOrException(TEmployeeClassification classification);

    /// <summary>
    /// Delete a EmployeeClassification row using an object. Returns true if the row was updated, false if no matching row was found.
    /// </summary>
    bool DeleteWithStatus(TEmployeeClassification classification);

    /// <summary>
    /// Gets an EmployeeClassification row by its name. If no row is found, an exception is thrown.
    /// </summary>
    /// <remarks>The specific exception thrown is not defined.</remarks>
    TEmployeeClassification FindByNameOrException(string employeeClassificationName);

    /// <summary>
    /// Gets an EmployeeClassification row by its name. Assume the name is unique. If no row is found, a null is returned.
    /// </summary>
    TEmployeeClassification? FindByNameOrNull(string employeeClassificationName);

    /// <summary>
    /// Gets an EmployeeClassification row by its primary key. If no row is found, an exception is thrown.
    /// </summary>
    /// <remarks>The specific exception thrown is not defined.</remarks>
    TEmployeeClassification GetByKeyOrException(int employeeClassificationKey);

    /// <summary>
    /// Gets an EmployeeClassification row by its primary key. If no row is found, a null is returned.
    /// </summary>
    TEmployeeClassification? GetByKeyOrNull(int employeeClassificationKey);

    /// <summary>
    /// Update a EmployeeClassification row. If no matching row is found, an exception is thrown.
    /// </summary>
    /// <remarks>The specific exception thrown is not defined.</remarks>
    void UpdateOrException(TEmployeeClassification classification);

    /// <summary>
    /// Update a EmployeeClassification row. Returns true if the row was updated, false if no matching row was found.
    /// </summary>
    bool UpdateWithStatus(TEmployeeClassification classification);
}

ADO.NET

The trick to the Update and Delete methods is to read back the row counts from ExecuteNonQuery.

public class TryCrudScenario : SqlServerScenarioBase, ITryCrudScenario<EmployeeClassification>
{
    public TryCrudScenario(string connectionString) : base(connectionString)
    { }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        const string sql = @"INSERT INTO HR.EmployeeClassification (EmployeeClassificationName)
                    OUTPUT Inserted.EmployeeClassificationKey
                    VALUES(@EmployeeClassificationName )";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationName", classification.EmployeeClassificationName);
            return (int)cmd.ExecuteScalar();
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        const string sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", employeeClassificationKey);
            var rowCount = cmd.ExecuteNonQuery();
            if (rowCount != 1)
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        const string sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", employeeClassificationKey);
            return 1 == cmd.ExecuteNonQuery();
        }
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        const string sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", classification.EmployeeClassificationKey);
            var rowCount = cmd.ExecuteNonQuery();
            if (rowCount != 1)
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        const string sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", classification.EmployeeClassificationKey);
            return 1 == cmd.ExecuteNonQuery();
        }
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        const string sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationName = @EmployeeClassificationName;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationName", employeeClassificationName);
            using (var reader = cmd.ExecuteReader())
            {
                if (!reader.Read())
                    throw new DataException($"No row was found for '{employeeClassificationName}'.");

                return new EmployeeClassification()
                {
                    EmployeeClassificationKey = reader.GetInt32(reader.GetOrdinal("EmployeeClassificationKey")),
                    EmployeeClassificationName = reader.GetString(reader.GetOrdinal("EmployeeClassificationName"))
                };
            }
        }
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        const string sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationName = @EmployeeClassificationName;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationName", employeeClassificationName);
            using (var reader = cmd.ExecuteReader())
            {
                if (!reader.Read())
                    return null;

                return new EmployeeClassification()
                {
                    EmployeeClassificationKey = reader.GetInt32(reader.GetOrdinal("EmployeeClassificationKey")),
                    EmployeeClassificationName = reader.GetString(reader.GetOrdinal("EmployeeClassificationName"))
                };
            }
        }
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        const string sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", employeeClassificationKey);
            using (var reader = cmd.ExecuteReader())
            {
                if (!reader.Read())
                    throw new DataException($"No row was found for key {employeeClassificationKey}.");

                return new EmployeeClassification()
                {
                    EmployeeClassificationKey = reader.GetInt32(reader.GetOrdinal("EmployeeClassificationKey")),
                    EmployeeClassificationName = reader.GetString(reader.GetOrdinal("EmployeeClassificationName"))
                };
            }
        }
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        const string sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", employeeClassificationKey);
            using (var reader = cmd.ExecuteReader())
            {
                if (!reader.Read())
                    return null;

                return new EmployeeClassification()
                {
                    EmployeeClassificationKey = reader.GetInt32(reader.GetOrdinal("EmployeeClassificationKey")),
                    EmployeeClassificationName = reader.GetString(reader.GetOrdinal("EmployeeClassificationName"))
                };
            }
        }
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        const string sql = @"UPDATE HR.EmployeeClassification
                    SET EmployeeClassificationName = @EmployeeClassificationName
                    WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", classification.EmployeeClassificationKey);
            cmd.Parameters.AddWithValue("@EmployeeClassificationName", classification.EmployeeClassificationName);
            var rowCount = cmd.ExecuteNonQuery();
            if (rowCount != 1)
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        const string sql = @"UPDATE HR.EmployeeClassification
                    SET EmployeeClassificationName = @EmployeeClassificationName
                    WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@EmployeeClassificationKey", classification.EmployeeClassificationKey);
            cmd.Parameters.AddWithValue("@EmployeeClassificationName", classification.EmployeeClassificationName);
            return 1 == cmd.ExecuteNonQuery();
        }
    }
}

Chain

public class TryCrudScenario : ITryCrudScenario<EmployeeClassification>
{
    readonly SqlServerDataSource m_DataSource;

    public TryCrudScenario(SqlServerDataSource dataSource)
    {
        m_DataSource = dataSource;
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        return m_DataSource.Insert(classification).ToInt32().Execute();
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        var count = m_DataSource.DeleteByKey<EmployeeClassification>(employeeClassificationKey).Execute();
        if (count == 0)
            throw new MissingDataException();
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        return 1 == m_DataSource.DeleteByKey<EmployeeClassification>(employeeClassificationKey).Execute();
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var count = m_DataSource.Delete(classification).Execute();
        if (count == 0)
            throw new MissingDataException();
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        return 1 == m_DataSource.Delete(classification).Execute();
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        return m_DataSource.From<EmployeeClassification>(new { employeeClassificationName })
            .ToObject().Execute();
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        return m_DataSource.From<EmployeeClassification>(new { employeeClassificationName })
            .ToObjectOrNull().Execute();
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        return m_DataSource.GetByKey<EmployeeClassification>(employeeClassificationKey).ToObject().Execute();
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        return m_DataSource.GetByKey<EmployeeClassification>(employeeClassificationKey).ToObjectOrNull().Execute();
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        m_DataSource.Update(classification).Execute();
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        return 1 == m_DataSource.Update(classification, UpdateOptions.IgnoreRowsAffected).Execute();
    }
}

Dapper

Like ADO.NET, this uses the row counts returned from the Execute command.

public class TryCrudScenario : ScenarioBase, ITryCrudScenario<EmployeeClassification>
{
    public TryCrudScenario(string connectionString) : base(connectionString)
    {
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var con = OpenConnection())
            return (int)con.Insert(classification);
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        {
            var rowCount = con.Execute(sql, new { employeeClassificationKey });
            if (rowCount != 1)
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
            return 1 == con.Execute(sql, new { employeeClassificationKey });
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        {
            var rowCount = con.Execute(sql, classification);
            if (rowCount != 1)
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
            return 1 == con.Execute(sql, classification);
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        var sql = @"SELECT  ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationName = @EmployeeClassificationName;";

        using (var con = OpenConnection())
            return con.QuerySingle<EmployeeClassification>(sql, new { employeeClassificationName });
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        var sql = @"SELECT  ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationName = @EmployeeClassificationName;";

        using (var con = OpenConnection())
            return con.QuerySingleOrDefault<EmployeeClassification>(sql, new { employeeClassificationName });
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        var sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
            return con.QuerySingle<EmployeeClassification>(sql, new { employeeClassificationKey });
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        var sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
            return con.QuerySingleOrDefault<EmployeeClassification>(sql, new { employeeClassificationKey });
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"UPDATE HR.EmployeeClassification
                    SET EmployeeClassificationName = @EmployeeClassificationName
                    WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
        {
            var rowCount = con.Execute(sql, classification);
            if (rowCount != 1)
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"UPDATE HR.EmployeeClassification
                    SET EmployeeClassificationName = @EmployeeClassificationName
                    WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        using (var con = OpenConnection())
            return 1 == con.Execute(sql, classification);
    }
}

DbConnector

DbConnector's NonQuery returns the number of affected rows, if the non-query ran successfully, from the ADO.NET ExecuteNonQuery command.

public class TryCrudScenario : ScenarioBase, ITryCrudScenario<EmployeeClassification>
{
    public TryCrudScenario(string connectionString) : base(connectionString)
    {
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        return DbConnector.Scalar<int>(
        @"INSERT INTO " + EmployeeClassification.TableName
        + @"(
                EmployeeClassificationName,
                IsEmployee,
                IsExempt
            ) 
            OUTPUT Inserted.EmployeeClassificationKey
            VALUES (
                @EmployeeClassificationName,
                @IsEmployee,
                @IsExempt
            )"
        , classification)
        .Execute();
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @employeeClassificationKey;";

        var rowCount = DbConnector.NonQuery(sql, new { employeeClassificationKey }).Execute();
        if (rowCount != 1)
            throw new DataException($"No row was found for key {employeeClassificationKey}.");
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @employeeClassificationKey;";

        return 1 == DbConnector.NonQuery(sql, new { employeeClassificationKey }).Execute();
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        var rowCount = DbConnector.NonQuery(sql, classification).Execute();
        if (rowCount != 1)
            throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"DELETE HR.EmployeeClassification WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        return 1 == DbConnector.NonQuery(sql, classification).Execute();
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        var sql = @"SELECT  ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationName = @employeeClassificationName;";


        return DbConnector.ReadSingle<EmployeeClassification>(sql, new { employeeClassificationName }).Execute();
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        var sql = @"SELECT  ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationName = @employeeClassificationName;";


        return DbConnector.ReadSingleOrDefault<EmployeeClassification>(sql, new { employeeClassificationName }).Execute();
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        var sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationKey = @employeeClassificationKey;";


        return DbConnector.ReadSingle<EmployeeClassification>(sql, new { employeeClassificationKey }).Execute();
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        var sql = @"SELECT ec.EmployeeClassificationKey, ec.EmployeeClassificationName
                    FROM HR.EmployeeClassification ec
                    WHERE ec.EmployeeClassificationKey = @employeeClassificationKey;";


        return DbConnector.ReadSingleOrDefault<EmployeeClassification>(sql, new { employeeClassificationKey }).Execute();
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"UPDATE HR.EmployeeClassification
                    SET EmployeeClassificationName = @EmployeeClassificationName
                    WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        var rowCount = DbConnector.NonQuery(sql, classification).Execute();
        if (rowCount != 1)
            throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var sql = @"UPDATE HR.EmployeeClassification
                    SET EmployeeClassificationName = @EmployeeClassificationName
                    WHERE EmployeeClassificationKey = @EmployeeClassificationKey;";

        return 1 == DbConnector.NonQuery(sql, classification).Execute();
    }
}

Entity Framework 6

public class TryCrudScenario : ITryCrudScenario<EmployeeClassification>
{
    private Func<OrmCookbookContext> CreateDbContext;

    public TryCrudScenario(Func<OrmCookbookContext> dBContextFactory)
    {
        CreateDbContext = dBContextFactory;
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            context.EmployeeClassification.Add(classification);
            context.SaveChanges();
            return classification.EmployeeClassificationKey;
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassification.Find(employeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassification.Remove(temp);
                context.SaveChanges();
            }
            else
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassification.Find(employeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassification.Remove(temp);
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassification.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassification.Remove(temp);
                context.SaveChanges();
            }
            else
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassification.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassification.Remove(temp);
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassification.Where(ec => ec.EmployeeClassificationName == employeeClassificationName).Single();
        }
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassification.Where(ec => ec.EmployeeClassificationName == employeeClassificationName).SingleOrDefault();
        }
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassification.Find(employeeClassificationKey) ?? throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassification.Find(employeeClassificationKey);
        }
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Get a fresh copy of the row from the database
            var temp = context.EmployeeClassification.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                //Copy the changed fields
                temp.EmployeeClassificationName = classification.EmployeeClassificationName;
                context.SaveChanges();
            }
            else
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Get a fresh copy of the row from the database
            var temp = context.EmployeeClassification.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                //Copy the changed fields
                temp.EmployeeClassificationName = classification.EmployeeClassificationName;
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }
}

Entity Framework Core

public class TryCrudScenario : ITryCrudScenario<EmployeeClassification>
{
    private Func<OrmCookbookContext> CreateDbContext;

    public TryCrudScenario(Func<OrmCookbookContext> dBContextFactory)
    {
        CreateDbContext = dBContextFactory;
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            context.EmployeeClassifications.Add(classification);
            context.SaveChanges();
            return classification.EmployeeClassificationKey;
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassifications.Find(employeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassifications.Remove(temp);
                context.SaveChanges();
            }
            else
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassifications.Find(employeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassifications.Remove(temp);
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassifications.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassifications.Remove(temp);
                context.SaveChanges();
            }
            else
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Find the row you wish to delete
            var temp = context.EmployeeClassifications.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                context.EmployeeClassifications.Remove(temp);
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassifications.Where(ec => ec.EmployeeClassificationName == employeeClassificationName).Single();
        }
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassifications.Where(ec => ec.EmployeeClassificationName == employeeClassificationName).SingleOrDefault();
        }
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassifications.Find(employeeClassificationKey) ?? throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        using (var context = CreateDbContext())
        {
            return context.EmployeeClassifications.Find(employeeClassificationKey);
        }
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Get a fresh copy of the row from the database
            var temp = context.EmployeeClassifications.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                //Copy the changed fields
                temp.EmployeeClassificationName = classification.EmployeeClassificationName;
                context.SaveChanges();
            }
            else
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var context = CreateDbContext())
        {
            //Get a fresh copy of the row from the database
            var temp = context.EmployeeClassifications.Find(classification.EmployeeClassificationKey);
            if (temp != null)
            {
                //Copy the changed fields
                temp.EmployeeClassificationName = classification.EmployeeClassificationName;
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }
}

LINQ to DB

public class TryCrudScenario : ITryCrudScenario<EmployeeClassification>
{
    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = new OrmCookbook())
        {
            return db.InsertWithInt32Identity(classification);
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        using (var db = new OrmCookbook())
        {
            var rowCount = db.EmployeeClassification.Where(d => d.EmployeeClassificationKey == employeeClassificationKey).Delete();
            if (rowCount != 1)
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        using (var db = new OrmCookbook())
        {
            return 1 == db.EmployeeClassification.Where(d => d.EmployeeClassificationKey == employeeClassificationKey).Delete();
        }
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = new OrmCookbook())
        {
            var rowCount = db.EmployeeClassification.Where(d => d.EmployeeClassificationKey == classification.EmployeeClassificationKey).Delete();
            if (rowCount != 1)
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = new OrmCookbook())
        {
            return 1 == db.EmployeeClassification.Where(d => d.EmployeeClassificationKey == classification.EmployeeClassificationKey).Delete();
        }
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        using (var db = new OrmCookbook())
        {
            return db.EmployeeClassification.Where(ec => ec.EmployeeClassificationName == employeeClassificationName).Single();
        }
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        using (var db = new OrmCookbook())
        {
            return db.EmployeeClassification.Where(ec => ec.EmployeeClassificationName == employeeClassificationName).SingleOrDefault();
        }
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        using (var db = new OrmCookbook())
        {
            return db.EmployeeClassification.Where(ec => ec.EmployeeClassificationKey == employeeClassificationKey).Single();
        }
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        using (var db = new OrmCookbook())
        {
            return db.EmployeeClassification.Where(ec => ec.EmployeeClassificationKey == employeeClassificationKey).SingleOrDefault();
        }
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = new OrmCookbook())
        {
            var rowCount = db.Update(classification);
            if (rowCount != 1)
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = new OrmCookbook())
        {
            return 1 == db.Update(classification);
        }
    }
}

LLBLGen Pro

public class TryCrudScenario : ITryCrudScenario<EmployeeClassificationEntity>
{
    public int Create(EmployeeClassificationEntity classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var adapter = new DataAccessAdapter())
        {
            adapter.SaveEntity(classification, true, recurse: false);
            return classification.EmployeeClassificationKey;
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        using (var adapter = new DataAccessAdapter())
        {
            // delete directly
            int count = adapter.DeleteEntitiesDirectly(typeof(EmployeeClassificationEntity),
                                           new RelationPredicateBucket(EmployeeClassificationFields.EmployeeClassificationKey
                                                                                       .Equal(employeeClassificationKey)));
            if (count <= 0)
            {
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
            }
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        using (var adapter = new DataAccessAdapter())
        {
            // delete directly
            int count = adapter.DeleteEntitiesDirectly(typeof(EmployeeClassificationEntity),
                                                       new RelationPredicateBucket(EmployeeClassificationFields.EmployeeClassificationKey
                                                                                                   .Equal(employeeClassificationKey)));
            return count > 0;
        }
    }

    public void DeleteOrException(EmployeeClassificationEntity classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var adapter = new DataAccessAdapter())
        {
            classification.IsNew = false;
            if (!adapter.DeleteEntity(classification))
            {
                throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
            }
        }
    }

    public bool DeleteWithStatus(EmployeeClassificationEntity classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var adapter = new DataAccessAdapter())
        {
            classification.IsNew = false;
            return adapter.DeleteEntity(classification);
        }
    }

    public EmployeeClassificationEntity FindByNameOrException(string employeeClassificationName)
    {
        using (var adapter = new DataAccessAdapter())
        {
            return new LinqMetaData(adapter).EmployeeClassification
                                .Single(ec => ec.EmployeeClassificationName == employeeClassificationName);
        }
    }

    public EmployeeClassificationEntity? FindByNameOrNull(string employeeClassificationName)
    {
        using (var adapter = new DataAccessAdapter())
        {
            return new LinqMetaData(adapter).EmployeeClassification
                                .SingleOrDefault(ec => ec.EmployeeClassificationName == employeeClassificationName);
        }
    }

    public EmployeeClassificationEntity GetByKeyOrException(int employeeClassificationKey)
    {
        using (var adapter = new DataAccessAdapter())
        {
            var ec = adapter.FetchNewEntity<EmployeeClassificationEntity>(
                                    new RelationPredicateBucket(EmployeeClassificationFields.EmployeeClassificationKey
                                                                                            .Equal(employeeClassificationKey)));
            if (ec.IsNew)
            {
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
            }
            return ec;
        }
    }

    public EmployeeClassificationEntity? GetByKeyOrNull(int employeeClassificationKey)
    {
        using (var adapter = new DataAccessAdapter())
        {
            var ec = adapter.FetchNewEntity<EmployeeClassificationEntity>(
                                    new RelationPredicateBucket(EmployeeClassificationFields.EmployeeClassificationKey
                                                                                            .Equal(employeeClassificationKey)));
            return ec.IsNew ? null : ec;
        }
    }

    public void UpdateOrException(EmployeeClassificationEntity classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var adapter = new DataAccessAdapter())
        {
            var toPersist = adapter.FetchNewEntity<EmployeeClassificationEntity>(
                                    new RelationPredicateBucket(EmployeeClassificationFields.EmployeeClassificationKey
                                                                                            .Equal(classification.EmployeeClassificationKey)));
            if (!toPersist.IsNew)
            {
                toPersist.EmployeeClassificationName = classification.EmployeeClassificationName;
                if (adapter.SaveEntity(toPersist))
                {
                    // succeeded.
                    return;
                }
                // save failed...
            }
            throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassificationEntity classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var adapter = new DataAccessAdapter())
        {
            var toPersist = adapter.FetchNewEntity<EmployeeClassificationEntity>(
                                    new RelationPredicateBucket(EmployeeClassificationFields.EmployeeClassificationKey
                                                                                            .Equal(classification.EmployeeClassificationKey)));
            if (!toPersist.IsNew)
            {
                toPersist.EmployeeClassificationName = classification.EmployeeClassificationName;
                return adapter.SaveEntity(toPersist, refetchAfterSave: false, recurse: false);
            }
            return false;
        }
    }
}

NHibernate

In some cases you'll need to catch a StaleStateException as there is no TryUpdate or TryDelete.

public class TryCrudScenario : ITryCrudScenario<EmployeeClassification>
{
    readonly ISessionFactory m_SessionFactory;

    public TryCrudScenario(ISessionFactory sessionFactory)
    {
        m_SessionFactory = sessionFactory;
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var session = m_SessionFactory.OpenSession())
        {
            session.Save(classification);
            session.Flush();
            return classification.EmployeeClassificationKey;
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        using (var session = m_SessionFactory.OpenSession())
        {
            session.Delete(new EmployeeClassification() { EmployeeClassificationKey = employeeClassificationKey });
            session.Flush();
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        using (var session = m_SessionFactory.OpenSession())
        {
            var temp = session.Get<EmployeeClassification>(employeeClassificationKey);
            if (temp == null)
                return false;
            session.Delete(temp);
            session.Flush();
            return true;
        }
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var session = m_SessionFactory.OpenSession())
        {
            session.Delete(classification);
            session.Flush();
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var session = m_SessionFactory.OpenSession())
        {
            try
            {
                session.Delete(classification);
                session.Flush();
                return true;
            }
            catch (StaleStateException ex) when (ex.Message.Contains("Batch update returned unexpected row count from update;", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
        }
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        using (var session = m_SessionFactory.OpenStatelessSession())
        {
            return session.QueryOver<EmployeeClassification>()
                .Where(ec => ec.EmployeeClassificationName == employeeClassificationName)
                .List().Single();
        }
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        using (var session = m_SessionFactory.OpenStatelessSession())
        {
            return session.QueryOver<EmployeeClassification>()
                .Where(ec => ec.EmployeeClassificationName == employeeClassificationName)
                .List().SingleOrDefault();
        }
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        using (var session = m_SessionFactory.OpenStatelessSession())
            return session.Get<EmployeeClassification>(employeeClassificationKey) ?? throw new DataException($"No row was found for key {employeeClassificationKey}.");
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        using (var session = m_SessionFactory.OpenStatelessSession())
            return session.Get<EmployeeClassification>(employeeClassificationKey);
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var session = m_SessionFactory.OpenSession())
        {
            session.Update(classification);
            session.Flush();
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var session = m_SessionFactory.OpenSession())
        {
            try
            {
                session.Update(classification);
                session.Flush();
                return true;
            }
            catch (StaleStateException ex) when (ex.Message.Contains("Batch update returned unexpected row count from update;", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
        }
    }
}

RepoDb

public class TryCrudScenario : BaseRepository<EmployeeClassification, SqlConnection>,
    ITryCrudScenario<EmployeeClassification>
{
    public TryCrudScenario(string connectionString)
        : base(connectionString, RDB.Enumerations.ConnectionPersistency.Instance)
    { }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        return Insert<int>(classification);
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        var rowCount = Delete(employeeClassificationKey);
        if (rowCount != 1)
            throw new DataException($"No row was found for key {employeeClassificationKey}.");
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        return 1 == Delete(employeeClassificationKey);
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        var rowCount = Delete(classification);
        if (rowCount != 1)
            throw new DataException($"No row was found for key {classification.EmployeeClassificationKey}.");
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        return 1 == Delete(classification);
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        var entity = Query(e => e.EmployeeClassificationName == employeeClassificationName).FirstOrDefault();
        if (null == entity)
            throw new DataException($"Message");

        return entity;
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        return Query(e => e.EmployeeClassificationName == employeeClassificationName).FirstOrDefault();
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        var entity = Query(employeeClassificationKey).FirstOrDefault();
        if (null == entity)
            throw new DataException($"Message");

        return entity;
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        return Query(employeeClassificationKey).FirstOrDefault();
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        var rowCount = Update(classification);
        if (rowCount != 1)
            throw new DataException($"Message");
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        return 1 == Update(classification);
    }
}

ServiceStack

Like ADO.NET, this uses the row counts returned from the Execute command.

public class TryCrudScenario : ITryCrudScenario<EmployeeClassification>
{
    private readonly IDbConnectionFactory _dbConnectionFactory;

    public TryCrudScenario(IDbConnectionFactory dbConnectionFactory)
    {
        _dbConnectionFactory = dbConnectionFactory;
    }

    public int Create(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            return (int)db.Insert(classification, true);
        }
    }

    public void DeleteByKeyOrException(int employeeClassificationKey)
    {
        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            var deleted = db.DeleteById<EmployeeClassification>(employeeClassificationKey);
            if (deleted != 1)
                throw new DataException($"No row was found for key {employeeClassificationKey}.");
        }
    }

    public bool DeleteByKeyWithStatus(int employeeClassificationKey)
    {
        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            return db.DeleteById<EmployeeClassification>(employeeClassificationKey) == 1;
        }
    }

    public void DeleteOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            var deleted = db.DeleteById<EmployeeClassification>(classification.Id);
            if (deleted != 1)
                throw new DataException($"No row was found for key {classification.Id}.");
        }
    }

    public bool DeleteWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            return db.DeleteById<EmployeeClassification>(classification.Id) == 1;
        }
    }

    public EmployeeClassification FindByNameOrException(string employeeClassificationName)
    {
        EmployeeClassification? record = null;
        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            record = db.Single<EmployeeClassification>(
                r => r.EmployeeClassificationName == employeeClassificationName);
        }

        if (record == null)
            throw new DataException($"No row was found with name {employeeClassificationName}.");
        return record;
    }

    public EmployeeClassification? FindByNameOrNull(string employeeClassificationName)
    {
        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            return db.Single<EmployeeClassification>(
                r => r.EmployeeClassificationName == employeeClassificationName);
        }
    }

    public EmployeeClassification GetByKeyOrException(int employeeClassificationKey)
    {
        EmployeeClassification? record = null;
        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            record = db.SingleById<EmployeeClassification>(employeeClassificationKey);
        }

        if (record == null)
            throw new DataException($"No row was found for key {employeeClassificationKey}.");
        return record;
    }

    public EmployeeClassification? GetByKeyOrNull(int employeeClassificationKey)
    {
        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            return db.SingleById<EmployeeClassification>(employeeClassificationKey);
        }
    }

    public void UpdateOrException(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            var updated = db.Update(classification);
            if (updated != 1)
                throw new DataException($"No row was found for key {classification.Id}.");
        }
    }

    public bool UpdateWithStatus(EmployeeClassification classification)
    {
        if (classification == null)
            throw new ArgumentNullException(nameof(classification), $"{nameof(classification)} is null.");

        using (var db = _dbConnectionFactory.OpenDbConnection())
        {
            return db.Update(classification) == 1;
        }
    }
}