Would you be open to add this method or something like it to OrmLite?
I don’t think it exist right now.
Because of the coercion bug in MySql it would be very useful and potentially safer than the current API (for MySql at least) for non integer primary key columns.
This could be your own custom extension method that would be reusable for your application. I believe the following code might be close to your requirements:
public static class IDbConnectionExtensions
{
public static T SelectSingle<T>(this IDbConnection dbConn, Expression<Func<T, bool>> predicate) where T : new()
{
var results = dbConn.Select(predicate);
if (results.Count == 0)
throw new NotFoundException(typeof(T).Name + " not found.");
if (results.Count > 1)
throw new MultipleEntriesFoundException("Multiple entries found for " + typeof(T).Name + ".");
return results.First();
}
}
The behavior is a bit different to existing APIs, so naming and functionality might need to change to consider it as a PR into OrmLite. Let me know how you go with the custom extension method approach and if that suits instead, otherwise create a PR with the suggested APIs and we can review the proposal.