private static string GetTableName(Type type) { if (TypeTableName.TryGetValue(type.TypeHandle, out string name)) { return(name); } if (SqlMapperExtensions.TableNameMapper != null) { name = SqlMapperExtensions.TableNameMapper(type); } else { var info = type; //NOTE: This as dynamic trick falls back to handle both our own Table-attribute as well as the one in EntityFramework var tableAttrName = info.GetCustomAttribute <TableAttribute>(false)?.Name ?? (info.GetCustomAttributes(false).FirstOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic)?.Name; if (tableAttrName != null) { name = tableAttrName; } else { name = type.Name + "s"; if (type.IsInterface && name.StartsWith("I")) { name = name.Substring(1); } } } TypeTableName[type.TypeHandle] = name; return(name); }
/// <summary> /// 扩展新增操作,并返回自增列值 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="conn">连接对象</param> /// <param name="entity">实体对象</param> /// <param name="identityId"></param> /// <param name="transaction">是否事务</param> /// <returns></returns> public static long Insert <T>(this IDbConnection conn, T entity, out long identityId, bool isTransaction = true) where T : class { identityId = 0; var type = typeof(T); if (type.IsArray) { type = type.GetElementType(); } else if (type.IsGenericType) { type = type.GetGenericArguments()[0]; } var allProperties = TypePropertiesCache(type); var tableMapper = type.GetCustomAttribute <TableAttribute>(); var name = SqlMapperExtensions.TableNameMapper != null?SqlMapperExtensions.TableNameMapper(type) : (tableMapper == null ? type.Name : tableMapper.Name); var sqlStr = new StringBuilder(); var valColumns = new StringBuilder(); sqlStr.AppendFormat("INSERT INTO {0} (", name); for (var i = 0; i < allProperties.Count; i++) { sqlStr.AppendFormat("`{0}`,", allProperties[i].Name); valColumns.AppendFormat("@{0},", allProperties[i].Name); } sqlStr.Length = sqlStr.Length - 1; valColumns.Length = valColumns.Length - 1; sqlStr.AppendFormat(") VALUES ({0});", valColumns.ToString()); sqlStr.Append("SELECT ROW_COUNT() AS ROW,LAST_INSERT_ID() AS IDENTITY"); IDbTransaction trans = null; if (isTransaction) { trans = conn.BeginTransaction(); conn = trans.Connection; } using (IDataReader reader = conn.ExecuteReader(sqlStr.ToString(), entity, transaction: trans)) { if (reader.Read()) { //最新id identityId = reader.GetInt64(1); //影响行数 return(reader.GetInt64(0)); } else { return(0); } } }
/// <summary> /// https://dapper-tutorial.net/knowledge-base/43032797/get-table-name-from-tableattribute-dapper-contrib /// </summary> /// <param name="modelType">Defaults to <see cref="TModel"/> when not specified</param> /// <returns></returns> protected string GetTableName <T>() { return(Cache.CacheExecution(() => { if (SqlMapperExtensions.TableNameMapper != null) { return SqlMapperExtensions.TableNameMapper(typeof(T)); } string getTableName = "GetTableName"; MethodInfo getTableNameMethod = typeof(SqlMapperExtensions).GetMethod(getTableName, BindingFlags.NonPublic | BindingFlags.Static); if (getTableNameMethod == null) { throw new ArgumentOutOfRangeException($"Method '{getTableName}' is not found in '{nameof(SqlMapperExtensions)}' class."); } return getTableNameMethod.Invoke(null, new object[] { typeof(T) }) as string; }, $"TABLENAME_{typeof(T).FullName}")); }