/// <inheritdoc /> public override async Task InsertAsync(T[] models, bool loadGeneratedKeys = true) { // Build Insert Query if (_insertQuery == null) { _insertQuery = $@" INSERT INTO [{Table}] ({QueryNonKeyFieldCols}) VALUES ({QueryNonKeyGeneratedModelRefs});"; } var autoIncField = PrimaryKey.FirstOrDefault( pk => pk.FieldAttribute.IsGenerated ); // Do Insert foreach (var model in models) { if (autoIncField == null || !loadGeneratedKeys) { await ExecuteAsync(_insertQuery, model); continue; } // Update and save generated id to model var results = await QueryAsync <int>( _insertQuery + @"SELECT CAST(SCOPE_IDENTITY() as int)", model ); TypeAccessor[model, autoIncField.ModelField] = results.Single(); model.ChangedProperties.Clear(); } }
/// <summary> /// Initializes a new instance of the TableInfo class. /// </summary> /// <param name="columns">The columns.</param> /// <param name="allModelProperties">All model properties</param> /// <param name="onAfterMaterialize">Method info accessor for calling OnAfterMaterialize over <seealso cref="IMaterialize"/>IMaterialize /// If Model doesn't implement <seealso cref="IMaterialize"/> then null.</param> /// <exception cref="ArgumentNullException">When columns is null.</exception> public TableInfo(IEnumerable <ColumnInfo> columns, IEnumerable <PropertyInfo> allModelProperties, MethodInfo onAfterMaterialize) { Check.NotNull(columns, nameof(columns)); Check.NotNull(allModelProperties, nameof(allModelProperties)); _columns = columns.ToDictionary(columnInfo => columnInfo.Name, columnInfo => columnInfo, StringComparer.CurrentCultureIgnoreCase); this.OnAfterMaterialize = onAfterMaterialize; this.AllModelProperties = allModelProperties; _properties = new Lazy <Dictionary <string, ColumnInfo> >(() => _columns.ToDictionary(columnInfo => columnInfo.Value.PropertyInfo.Name, columnInfo => columnInfo.Value, StringComparer.CurrentCultureIgnoreCase)); _identityPrimaryKey = new Lazy <ColumnInfo>(() => PrimaryKey.FirstOrDefault(p => p.AutoIncrementMethodType == AutoIncrementMethodType.Identity)); }