public virtual int InsertWithPrimaryKey(TEntity entity) { dynamic id; int primaryKeyValue = 0; string query = QB <TEntity> .InsertWithPrimaryKey(); if (Transaction != null) { id = Connection.Query(query, entity, transaction: Transaction).FirstOrDefault(); } else { id = Connection.Query(query, entity).FirstOrDefault(); } if (id != null) { var firstItem = (IDictionary <string, object>)id; foreach (var v in firstItem) { primaryKeyValue = Convert.ToInt32(v.Value); } } return(primaryKeyValue); }