예제 #1
0
        public Task InsertAsync <T>(IDbConnection connection, IEnumerable <T> entities, IDbTransaction transaction, int?commandTimeout) where T : class
        {
            IClassMapper classMap   = SqlGenerator.Configuration.GetMap <T>();
            var          properties = classMap.Properties.Where(p => !p.IsKeyType(KeyType.NotAKey));

            foreach (var e in entities)
            {
                classMap.OnInsert(e);

                foreach (var column in properties)
                {
                    if (column.IsKeyType(KeyType.Guid))
                    {
                        Guid comb = SqlGenerator.Configuration.GetNextGuid();
                        column.PropertyInfo.SetValue(e, comb, null);
                    }
                }
            }

            string sql = SqlGenerator.Insert(classMap);

            return(connection.ExecuteAsync(sql, entities, transaction, commandTimeout, CommandType.Text));
        }
예제 #2
0
        public async Task <dynamic> InsertAsync <T>(IDbConnection connection, T entity, IDbTransaction transaction, int?commandTimeout) where T : class
        {
            IClassMapper classMap = SqlGenerator.Configuration.GetMap <T>();

            classMap.OnInsert(entity);
            List <IPropertyMap> nonIdentityKeyProperties = classMap.Properties.Where(p => p.IsAnyKeyType(KeyType.Guid) || p.IsAnyKeyType(KeyType.Assigned)).ToList();
            var identityColumn = classMap.Properties.SingleOrDefault(p => p.IsAnyKeyType(KeyType.Identity));

            foreach (var column in nonIdentityKeyProperties)
            {
                if (column.IsKeyType(KeyType.Guid))
                {
                    Guid comb = SqlGenerator.Configuration.GetNextGuid();
                    column.PropertyInfo.SetValue(entity, comb, null);
                }
            }

            IDictionary <string, object> keyValues = new ExpandoObject();
            string sql = SqlGenerator.Insert(classMap);

            if (identityColumn != null)
            {
                IEnumerable <long> result;
                if (SqlGenerator.SupportsMultipleStatements())
                {
                    sql   += SqlGenerator.Configuration.Dialect.BatchSeperator + SqlGenerator.IdentitySql(classMap);
                    result = await connection.QueryAsync <long>(sql, entity, transaction, commandTimeout, CommandType.Text);
                }
                else
                {
                    await connection.ExecuteAsync(sql, entity, transaction, commandTimeout, CommandType.Text);

                    sql    = SqlGenerator.IdentitySql(classMap);
                    result = await connection.QueryAsync <long>(sql, entity, transaction, commandTimeout, CommandType.Text);
                }

                long identityValue = result.First();
                if (identityColumn.PropertyInfo.PropertyType.Equals(typeof(long)))
                {
                    keyValues.Add(identityColumn.Name, identityValue);
                    identityColumn.PropertyInfo.SetValue(entity, identityValue, null);
                }
                else
                {
                    int identityInt = Convert.ToInt32(identityValue);
                    keyValues.Add(identityColumn.Name, identityInt);
                    identityColumn.PropertyInfo.SetValue(entity, identityInt, null);
                }
            }
            else
            {
                await connection.ExecuteAsync(sql, entity, transaction, commandTimeout, CommandType.Text);
            }

            foreach (var column in nonIdentityKeyProperties)
            {
                keyValues.Add(column.Name, column.PropertyInfo.GetValue(entity, null));
            }

            if (keyValues.Count == 1)
            {
                return(keyValues.First().Value);
            }

            return(keyValues);
        }