@@ -52,7 +52,7 @@ public async Task InsertEntities_MultipleTimes(InsertStrategy strategy)
5252 var insertedEntities = await _context . InsertWithStrategyAsync ( strategy , entities ,
5353 onConflict : new OnConflictOptions < TestEntity >
5454 {
55- Update = e => e ,
55+ Update = ( inserted , excluded ) => inserted ,
5656 } ) ;
5757
5858 // Assert
@@ -82,7 +82,7 @@ public async Task InsertEntities_MultipleTimes_WithGuidId(InsertStrategy strateg
8282 var insertedEntities = await _context . InsertWithStrategyAsync ( strategy , entities ,
8383 onConflict : new OnConflictOptions < TestEntityWithGuidId >
8484 {
85- Update = e => e ,
85+ Update = ( inserted , excluded ) => inserted ,
8686 } ) ;
8787
8888 // Assert
@@ -112,7 +112,7 @@ public async Task InsertEntities_MultipleTimes_With_Conflict_On_Id(InsertStrateg
112112 o => o . CopyGeneratedColumns = true ,
113113 onConflict : new OnConflictOptions < TestEntity >
114114 {
115- Update = e => e ,
115+ Update = ( inserted , excluded ) => inserted ,
116116 } ) ;
117117
118118 // Assert
@@ -146,9 +146,9 @@ public async Task InsertEntities_WithConflict_SingleColumn(InsertStrategy strate
146146 {
147147 e . Name ,
148148 } ,
149- Update = e => new TestEntity
149+ Update = ( inserted , excluded ) => new TestEntity
150150 {
151- Name = e . Name + " - Conflict" ,
151+ Name = inserted . Name + " - Conflict" ,
152152 } ,
153153 } ) ;
154154
@@ -203,15 +203,14 @@ public async Task InsertEntities_WithConflict_RawCondition(InsertStrategy strate
203203 var entities = new List < TestEntity >
204204 {
205205 new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity1", Price = 20 } ,
206- new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity2", Price = 30 } ,
206+ new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity2", Price = 600 } ,
207207 } ;
208208
209209 await _context . ExecuteBulkInsertAsync ( entities , onConflict : new OnConflictOptions < TestEntity >
210210 {
211211 Match = e => new
212212 {
213213 e . Name ,
214- // ...other columns to match on
215214 }
216215 } ) ;
217216
@@ -220,14 +219,16 @@ public async Task InsertEntities_WithConflict_RawCondition(InsertStrategy strate
220219 {
221220
222221 Match = e => new { e . Name } ,
223- Update = e => new TestEntity { Price = e . Price } ,
224- RawWhere = "EXCLUDED.some_price > INSERTED.some_price"
222+ Update = ( inserted , excluded ) => new TestEntity
223+ {
224+ Price = excluded . Price + inserted . Price ,
225+ } ,
226+ RawWhere = "EXCLUDED.some_price != INSERTED.some_price"
225227 } ) ;
226228
227229 // Assert
228- Assert . Equal ( 2 , insertedEntities . Count ) ;
229- Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity1" && e . Price == 20 ) ;
230- Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity2" && e . Price == 30 ) ;
230+ Assert . Single ( insertedEntities ) ;
231+ Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity1" && e . Price == 30 ) ;
231232 }
232233
233234 [ SkippableTheory ]
@@ -242,6 +243,49 @@ public async Task InsertEntities_WithConflict_ExpressionCondition(InsertStrategy
242243 _context . SaveChanges ( ) ;
243244 _context . ChangeTracker . Clear ( ) ;
244245
246+ var entities = new List < TestEntity >
247+ {
248+ new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity1", Price = 20 } ,
249+ new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity2", Price = 600 } ,
250+ } ;
251+
252+ await _context . ExecuteBulkInsertAsync ( entities , onConflict : new OnConflictOptions < TestEntity >
253+ {
254+ Match = e => new
255+ {
256+ e . Name ,
257+ }
258+ } ) ;
259+
260+ // Act
261+ var insertedEntities = await _context . InsertWithStrategyAsync ( strategy , entities , _ => { } , new OnConflictOptions < TestEntity >
262+ {
263+
264+ Match = e => new { e . Name } ,
265+ Update = ( inserted , excluded ) => new TestEntity
266+ {
267+ Price = excluded . Price + inserted . Price ,
268+ } ,
269+ Where = ( inserted , excluded ) => excluded . Price != inserted . Price ,
270+ } ) ;
271+
272+ // Assert
273+ Assert . Single ( insertedEntities ) ;
274+ Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity1" && e . Price == 30 ) ;
275+ }
276+
277+ [ SkippableTheory ]
278+ [ InlineData ( InsertStrategy . InsertReturn ) ]
279+ [ InlineData ( InsertStrategy . InsertReturnAsync ) ]
280+ public async Task InsertEntities_WithConflict_ComplexExpressionCondition ( InsertStrategy strategy )
281+ {
282+ Skip . If ( _context . IsProvider ( ProviderType . MySql ) ) ;
283+
284+ // Arrange
285+ _context . TestEntities . Add ( new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity1", Price = 10 } ) ;
286+ _context . SaveChanges ( ) ;
287+ _context . ChangeTracker . Clear ( ) ;
288+
245289 var entities = new List < TestEntity >
246290 {
247291 new TestEntity { TestRun = _run , Name = $ "{ _run } _Entity1", Price = 20 } ,
@@ -252,13 +296,13 @@ public async Task InsertEntities_WithConflict_ExpressionCondition(InsertStrategy
252296 var insertedEntities = await _context . InsertWithStrategyAsync ( strategy , entities , _ => { } , new OnConflictOptions < TestEntity >
253297 {
254298 Match = e => new { e . Name } ,
255- Update = e => new TestEntity { Price = e . Price } ,
256- Where = ( inserted , excluded ) => excluded . Price > inserted . Price && excluded . Price > 15 ? inserted . Name . Trim ( ) . Contains ( "Entity1" ) : inserted . Name . Trim ( ) . Contains ( "Entity2 ") ,
299+ Update = ( inserted , excluded ) => new TestEntity { Price = ( excluded . Price > 15 ? 15 : 10 ) } ,
300+ Where = ( inserted , excluded ) => excluded . Price > inserted . Price && inserted . Name . Trim ( ) . Contains ( "Entity1" ) ,
257301 } ) ;
258302
259303 // Assert
260304 Assert . Equal ( 2 , insertedEntities . Count ) ;
261- Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity1" && e . Price == 20 ) ;
305+ Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity1" && e . Price == 15 ) ;
262306 Assert . Contains ( insertedEntities , e => e . Name == $ "{ _run } _Entity2" && e . Price == 30 ) ;
263307 }
264308
@@ -284,7 +328,7 @@ public async Task InsertEntities_WithConflict_MultipleColumns(InsertStrategy str
284328 var insertedEntities = await _context . InsertWithStrategyAsync ( strategy , entities , _ => { } , new OnConflictOptions < TestEntity >
285329 {
286330 Match = e => new { e . Name } ,
287- Update = e => new TestEntity { Name = e . Name + " - Conflict" , Price = 0 }
331+ Update = ( inserted , excluded ) => new TestEntity { Name = inserted . Name + " - Conflict" , Price = 0 }
288332 } ) ;
289333
290334 // Assert
0 commit comments