Skip to content

Commit 07b4d1b

Browse files
Update bulk-merge.md
1 parent f9fa66f commit 07b4d1b

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

docs2/pages/documentations/bulk-merge.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@ bulk.DestinationTableName = "Customers";
1010
bulk.BulkMerge(customers);
1111

1212
// Easy to customize
13-
bulk.DestinationTableName = "Invoices";
14-
bulk.ColumnOutputExpression = invoice => invoice.InvoiceID;
15-
bulk.ColumnInputExpression = invoice => new
16-
{
17-
invoice.InvoiceID,
18-
invoice.Number
19-
};
20-
21-
bulk.BulkMerge(invoices);
22-
23-
// SET foreign key value
24-
invoices.ForEach(x => x.Items.ForEach(y => y.InvoiceID = x.InvoiceID));
25-
bulk.DestinationTableName = "InvoiceItems";
26-
bulk.BulkMerge(invoices.SelectMany(x => x.Items).ToList());
13+
bulk.DestinationTableName = "Customers";
14+
bulk.BatchSize = 100;
15+
bulk.AutoMapOutputDirection = true;
16+
bulk.BulkMerge(customers);
2717
```
2818
[Try it (Entity)](https://dotnetfiddle.net/qpe8bV)
2919

@@ -74,11 +64,15 @@ bulk.BulkMergeAsync(customers, cancellationToken);
7464
The `options` parameter let you use a lambda expression to customize the way entities are inserted/updated.
7565

7666
```csharp
77-
bulk.ColumnPrimaryKeyExpression = c => c.Code;
67+
bulk.AutoMapKeyExpression = c => c.Code;
7868
bulk.BulkMerge(customers);
7969
```
8070
[Try it (Entity)](https://dotnetfiddle.net/5wMQ6X)
8171

72+
```csharp
73+
bulk.AutoMapKeyName = "Code";
74+
bulk.BulkMerge(customers);
75+
```
8276
[Try it (DataTable)](https://dotnetfiddle.net/JJIPCB)
8377

8478
## Real Life Scenarios
@@ -111,34 +105,47 @@ bulk.BulkMerge(customers);
111105
```
112106
[Try it (Entity)](https://dotnetfiddle.net/W4TJkK)
113107

108+
```csharp
109+
var columnMapping = new ColumnMapping("CreatedDate");
110+
111+
columnMapping.IgnoreOnMergeUpdate = true;
112+
113+
bulk.ColumnMappings.Add("CustomerID", true);
114+
bulk.ColumnMappings.Add("UpdatedDate");
115+
bulk.ColumnMappings.Add("Name");
116+
bulk.ColumnMappings.Add(columnMapping);
117+
bulk.BulkMerge(dtCustomers);
118+
```
119+
[Try it (DataTable)](https://dotnetfiddle.net/TIfeSG)
120+
114121
### Merge with custom key
115122
You want to merge entities, but you don't have the primary key. The `ColumnPrimaryKeyExpression` let you use as a key any property or combination of properties.
116123

117124
```csharp
118-
bulk.ColumnPrimaryKeyExpression = c => c.Code
119-
bulk.BulkMerge(customers);
125+
bulk.AutoMapKeyExpression = c => c.Code;
126+
bulk.BulkMerge(customers);
120127
```
121128
[Try it (Entity)](https://dotnetfiddle.net/Xlcdxq)
122129

130+
```csharp
131+
bulk.AutoMapKeyName = "Code";
132+
bulk.BulkMerge(customers);
133+
```
123134
[Try it (DataTable)](https://dotnetfiddle.net/9KOxdW)
124135

136+
125137
### Merge with related child entities
126138
You want to merge entities but also merge related child entities.
127139

128140
```csharp
129141
bulk.DestinationTableName = "Invoices";
130-
bulk.ColumnOutputExpression = invoice => invoice.InvoiceID;
131-
bulk.ColumnInputExpression = invoice => new
132-
{
133-
invoice.InvoiceID,
134-
invoice.Number
135-
};
142+
bulk.AutoMapOutputIdentity = true;
136143

137144
// SET foreign key value
138145
invoices.ForEach(x => x.Items.ForEach(y => y.InvoiceID = x.InvoiceID));
139146
bulk.BulkMerge(invoices);
140147
```
141-
[Try it (Entity)](https://dotnetfiddle.net/rhq5ZM)
148+
[Try it (Entity)](https://dotnetfiddle.net/LLDcvy)
142149

143150
[Try it (DataTable)](https://dotnetfiddle.net/rhq5ZM)
144151

0 commit comments

Comments
 (0)