Skip to content

Commit c34a3dd

Browse files
committed
2 parents cc0addb + 5b9fac7 commit c34a3dd

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

EFCore.BulkExtensions.Core/BulkConfig.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class BulkConfig
3636
/// </remarks>
3737
public bool SetOutputNonIdentityColumns { get; set; } = true;
3838

39+
/// <summary>
40+
/// Automatically exclude properties marked as Timestamp / RowVersion during Bulk operations.
41+
/// </summary>
42+
public bool AutoExcludeTimestamp { get; set; } = true;
3943
/// <summary>
4044
/// Used only when SetOutputIdentity is set to true, and when changed to True then columns that were no included in Upsert are not loaded.
4145
/// </summary>

EFCore.BulkExtensions.Core/TableInfo.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,16 @@ public void LoadData<T>(BulkContext context, Type? type, IEnumerable<T> entities
195195
}
196196
}
197197

198-
Schema = customTableName != null ? customSchema : entityType.GetSchema() ?? defaultSchema;
199-
200198
var entityTableName = entityType.GetTableName();
201-
TableName = customTableName ?? entityTableName;
199+
var entityViewName = entityType.GetViewName();
200+
bool isView = entityTableName == null && entityViewName != null;
201+
202+
if(isView)
203+
Schema = customTableName != null ? customSchema : entityType.GetViewSchema() ?? defaultSchema;
204+
else
205+
Schema = customTableName != null ? customSchema : entityType.GetSchema() ?? defaultSchema;
206+
207+
TableName = customTableName ?? (isView ? entityViewName : entityTableName);
202208

203209
string? sourceSchema = null;
204210
string? sourceTableName = null;
@@ -224,12 +230,14 @@ public void LoadData<T>(BulkContext context, Type? type, IEnumerable<T> entities
224230
}
225231
TempTableName = sourceTableName ?? $"{TableName}{TempTableSufix}";
226232

227-
if (entityTableName is null)
233+
if ((!isView && entityTableName is null) || (isView && entityViewName is null))
228234
{
229-
throw new ArgumentException("Entity does not contain a table name");
235+
throw new ArgumentException("Entity does not contain a table or view name");
230236
}
231237

232-
ObjectIdentifier = StoreObjectIdentifier.Table(entityTableName, entityType.GetSchema());
238+
ObjectIdentifier = isView
239+
? StoreObjectIdentifier.View(entityViewName!, entityType.GetViewSchema())
240+
: StoreObjectIdentifier.Table(entityTableName!, entityType.GetSchema());
233241

234242
var allProperties = new List<IProperty>();
235243
foreach (var entityProperty in entityType.GetProperties())
@@ -346,6 +354,23 @@ public void LoadData<T>(BulkContext context, Type? type, IEnumerable<T> entities
346354
)?.GetColumnName(ObjectIdentifier);
347355
}
348356

357+
if (BulkConfig.AutoExcludeTimestamp)
358+
{
359+
var timestampProps = allProperties
360+
.Where(p => p.IsConcurrencyToken && p.ValueGenerated == ValueGenerated.OnAddOrUpdate)
361+
.Select(p => p.Name)
362+
.ToList();
363+
364+
BulkConfig.PropertiesToExclude ??= new();
365+
foreach (var prop in timestampProps)
366+
{
367+
if (!BulkConfig.PropertiesToExclude.Contains(prop))
368+
BulkConfig.PropertiesToExclude.Add(prop);
369+
}
370+
}
371+
372+
373+
349374
// timestamp/row version properties are only set by the Db, the property has a [Timestamp] Attribute or is configured in FluentAPI with .IsRowVersion()
350375
// They can be identified by the columne type "timestamp" or .IsConcurrencyToken in combination with .ValueGenerated == ValueGenerated.OnAddOrUpdate
351376
string timestampDbTypeName = nameof(TimestampAttribute).Replace("Attribute", "").ToLower(); // = "timestamp";

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ Its assembly is [Strong-Named](https://docs.microsoft.com/en-us/dotnet/standard/
7878
| 1.x | NetStandard 1.4 | EF Core 1 | NetCore(1.0+) |
7979

8080
Supports follows official [.Net lifecycle](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core), currently v.9 as latest and v.8(LTS).
81-
**At the moment *Pomelo.EntityFrameworkCore.MySql* still does not have full Release for EF9 so its nuget is published as 'rc' and Main package as 9.0.0-rc.1 (mysql adapter is ommited from main release version 9.0.1)
8281

8382
## Usage
8483
It's pretty simple and straightforward.
@@ -406,7 +405,7 @@ R { BulkRead ----------------| |--Read -----|--Read -------|--Read
406405
```
407406
- Projects Composition:
408407

409-
| Num | Nuget | Reference | Transitive dep. | Note |
408+
| Num | Nuget | References | Transitive dep. | Note |
410409
| --- | -------------------------------------- | ----------- | --------------- | ------------ |
411410
| [0] | EFCore.BulkExtensions.Core | | | shared |
412411
| [1] | EFCore.BulkExtensions.***SqlServer*** | [0] | | per provider |

0 commit comments

Comments
 (0)