Skip to content

Commit 933f35a

Browse files
Update bulk-insert.md
1 parent 8d2e26d commit 933f35a

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

docs2/pages/documentations/bulk-insert.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@ bulk.DestinationTableName = "Customers";
1010
bulk.BulkInsert(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.BulkInsert(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.BulkInsert(invoices.SelectMany(x => x.Items).ToList());
13+
bulk.DestinationTableName = "Customers";
14+
bulk.InsertIfNotExists = true;
15+
bulk.AutoMapOutputIdentity = true;
16+
bulk.BulkInsert(customers);
2717
```
2818

2919
[Try it (DataTable)](https://dotnetfiddle.net/UtvblA)
@@ -101,6 +91,7 @@ bulk.BulkInsert(customers);
10191
[Try it (Entity)](https://dotnetfiddle.net/04NuC3)
10292

10393
### Insert and include/exclude properties
94+
10495
You want to insert your entities but only for specific properties.
10596

10697
- `ColumnInputExpression`: This option let you choose which properties to map.
@@ -121,23 +112,30 @@ bulk.BulkInsert(customers);
121112
You want to insert entities but only those that don't already exist in the database.
122113

123114
- `InsertIfNotExists`: This option let you insert only entity that doesn't already exists.
124-
- `PrimaryKeyExpression`: This option let you customize the key to use to check if the entity already exists or not.
115+
- `PrimaryKeyExpression`: This option let you customize the key to use to check if the entity already exists or not. This option disable the Auto Mapping.
116+
- `AutoMapKeyExpression`: This option let you customize the key with an expression and keep the Auto Mapping.
117+
- `AutoMapKeyName`: This option let you customize the key by names and keep the Auto Mapping.
125118

126119
```csharp
127120
bulk.InsertIfNotExists = true;
128-
bulk.ColumnPrimaryKeyExpression = c => c.Code;
121+
bulk.AutoMapKeyExpression = c => c.Code;
129122
bulk.BulkInsert(customers);
130123
```
131-
[Try it (DataTable)](https://dotnetfiddle.net/waYK0E)
132-
133124
[Try it (Entity)](https://dotnetfiddle.net/DLMhLv)
134125

126+
```csharp
127+
bulk.InsertIfNotExists = true;
128+
bulk.AutoMapKeyName = "Code";
129+
bulk.BulkInsert(customers);
130+
```
131+
[Try it (DataTable)](https://dotnetfiddle.net/waYK0E)
132+
135133

136134
### Insert related child entities
137135
You want to insert related child entities.
138136

139137
```csharp
140-
bulk.ColumnOutputExpression = invoice => invoice.InvoiceID;
138+
bulk.AutoMapOutputIdentity = true;
141139
bulk.BulkInsert(invoices);
142140

143141
// SET foreign key value
@@ -151,10 +149,10 @@ bulk.BulkInsert(invoices.SelectMany(x => x.Items).ToList());
151149
### Insert with returning identity value
152150
By default, the `BulkInsert` method doesn't returns the identity when inserting.
153151

154-
You can return the identity by specifying the column as `Output`.
152+
You can return the identity by specifying it should be returned.
155153

156154
```csharp
157-
bulk.ColumnOutputExpression = c => c.CustomerID;
155+
bulk.AutoMapOutputIdentity = true;
158156
bulk.BulkInsert(customers);
159157
```
160158
[Try it (DataTable)](https://dotnetfiddle.net/g5pSS1)

0 commit comments

Comments
 (0)