Esempio n. 1
0
        public async Task <int> InsertAsync(T entity)
        {
            Guard.ArgumentNotNull(entity, nameof(entity));
            var segments = Context.Runtime.GetCrudSegments(this.EntityType);
            var sql      = segments.InsertSql;
            var field    = GetAutoGenerationField();

            var connection = this.GetWritingConnection();

            if (field != null)
            {
                using (var scope = new DbTransactionScope())
                {
                    var mergedSql = string.Concat(sql, ";", System.Environment.NewLine, Context.Runtime.GetDataSource(typeof(T)).DatabaseProvider.GetLastedInsertId());
                    var id        = await connection.ExecuteScalarAsync(mergedSql, entity);

                    scope.Complete();

                    PropertySetter.SetProperty(field.Field, entity, id);
                    return(1);
                }
            }

            var count = await connection.ExecuteAsync(sql, entity);

            return(count);
        }
Esempio n. 2
0
        public sealed override T Build()
        {
            T instance = InstanceFactory.Create(_propertyOverwriters);

            var overwrittenProperties =
                from property in _properties
                join overwriter in _propertyOverwriters on property.Name equals overwriter.PropertyName
                select(Property : property, Overwriter : overwriter);

            foreach (var entry in overwrittenProperties)
            {
                PropertySetter.SetProperty(instance, entry.Property, entry.Overwriter.GetValue());
            }

            return(instance);
        }