/// <summary> /// Defines the source attributes to upsert from. These should be in the same order as non-database generated /// columns in the target table. /// </summary> /// <param name="targetColumns"> /// The update source attributes. /// </param> /// <returns> /// The current <see cref="JsonInsert{T}"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="targetColumns"/> is null. /// </exception> public JsonInsert <T> Upsert([NotNull] params Expression <Func <T, object> >[] targetColumns) { if (targetColumns == null) { throw new ArgumentNullException(nameof(targetColumns)); } this.insertConflictAction = InsertConflictAction.Update; this.updateColumnExpressions.Clear(); this.updateColumnExpressions.AddRange(targetColumns); return(this); }
/// <summary> /// Specify the attributes to update when there is a constraint violation. /// </summary> /// <param name="constraintHintAttributes"> /// The constraint hint attributes. /// </param> /// <returns> /// The current <see cref="JsonInsert{T}"/>. /// </returns> public JsonInsert <T> OnConflict([NotNull] params Expression <Func <T, object> >[] constraintHintAttributes) { if (constraintHintAttributes == null) { throw new ArgumentNullException(nameof(constraintHintAttributes)); } this.insertConflictAction = InsertConflictAction.Update; this.constraintHintExpressions.Clear(); this.constraintHintExpressions.AddRange(constraintHintAttributes); return(this); }
static void TestViaInterfaceCase <T>(IEnumerable <T> data, DbContext context) where T : IHasId { var uploader = new NpgsqlBulkUploader(context); var properties = data .First() .GetType() .GetProperties() .Where(x => x.GetCustomAttribute <ColumnAttribute>() != null) .ToArray(); uploader.Insert(data, InsertConflictAction.UpdateProperty <T>(x => x.AddressId, properties)); }
static void TestDerived(BulkContext context) { context.Database.ExecuteSqlRaw("TRUNCATE addresses CASCADE"); var data = Enumerable.Range(0, 100000) .Select((x, i) => new Address2EF() { StreetName = streets[i % streets.Length], HouseNumber = i + 1, PostalCode = codes[i % codes.Length], ExtraHouseNumber = extraNumbers[i % extraNumbers.Length], Duration = new NpgsqlTypes.NpgsqlRange<DateTime>(DateTime.Now, DateTime.Now), LocalizedName = streets[i % streets.Length], Index2 = i }).ToList(); var uploader = new NpgsqlBulkUploader(context); var sw = Stopwatch.StartNew(); uploader.Insert(data); sw.Stop(); Console.WriteLine($"Derived: dynamic solution inserted {data.Count} records for {sw.Elapsed }"); // Trace.Assert(context.Addresses.Count() == data.Count); uploader.Insert(data.Take(100), InsertConflictAction.UpdateProperty<Address2EF>( x => x.AddressId, x => x.PostalCode)); uploader.Insert(data.Take(100), InsertConflictAction.UpdateProperty<Address2EF>( x => x.AddressId, x => x.Index2)); Console.WriteLine($"Derived: derived objects are inserted"); var data2 = Enumerable.Range(0, 100000) .Select((x, i) => new Address2EF() { StreetName = streets[i % streets.Length], HouseNumber = i + 1, PostalCode = codes[i % codes.Length], ExtraHouseNumber = extraNumbers[i % extraNumbers.Length], Duration = new NpgsqlTypes.NpgsqlRange<DateTime>(DateTime.Now, DateTime.Now), LocalizedName = streets[i % streets.Length], Index2 = i }).ToList(); uploader.Update(data2); }
/// <summary> /// Specifies that on a constraint violation, the row will not be inserted and no error will be raised. /// </summary> /// <returns> /// The current <see cref="JsonInsert{T}"/>. /// </returns> public JsonInsert <T> OnConflictDoNothing() { this.insertConflictAction = InsertConflictAction.DoNothing; return(this); }