早期的版本一直用的是EF,可是EF一直有个让人很不爽的东西须要mapping 实体对象;若是没有映射的状况下连查询都没办法;javascript
因此后来开始使用dapper 可是dapper都是直接用的是sql,这个对查询来讲还好,可是新增,更新就很麻烦。java
基于以上的缘由就打算对dapper进行扩展,实现传入实体对象就能直接更新 和新增;不用再去写sql语句。sql
下面直接贴上代码app
新增async
public async Task<int> Insert<T>(T entity) where T : class { try { string sql = SqlHelper.Insert<T>(sqlAdapter); var res = await dbConnection.ExecuteAsync(sql, entity, dbTransaction); return res; } catch (Exception ex) { throw ex; } finally { if (dbTransaction == null) { this.Close(); } } }
更新this
public async Task<int> Update<T>(T entity) where T : class { try { string sql = SqlHelper.Update<T>(sqlAdapter); var res = await dbConnection.ExecuteAsync(sql, entity, dbTransaction); return res; } catch (Exception ex) { throw ex; } finally { if (dbTransaction == null) { this.Close(); } } }
须要nuget引入对象
cd.dapper.extension
开发小记,一天一篇blog