Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit e08577f

Browse files
committed
Added better Insert skip check.
1 parent d9d50d6 commit e08577f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
263263
}
264264
}
265265

266+
var behaviour = prop.GetBeforeSaveBehavior();
267+
var skipOnInsert = prop.ValueGenerated.HasFlag(ValueGenerated.OnAdd);
268+
269+
if (skipOnInsert)
270+
{
271+
skipOnInsert = isIdentity || behaviour != PropertySaveBehavior.Save;
272+
}
273+
274+
var skipOnUpdate = behaviour != PropertySaveBehavior.Save ||
275+
prop.ValueGenerated.HasFlag(ValueGenerated.OnUpdate);
276+
266277
return new T[]
267278
{
268279
(T)(Attribute)new ColumnAttribute
@@ -275,7 +286,9 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
275286
IsPrimaryKey = isPrimaryKey,
276287
PrimaryKeyOrder = primaryKeyOrder,
277288
IsIdentity = isIdentity,
278-
IsDiscriminator = discriminator == prop
289+
IsDiscriminator = discriminator == prop,
290+
SkipOnInsert = skipOnInsert,
291+
SkipOnUpdate = skipOnUpdate
279292
}
280293
};
281294
}

Tests/LinqToDB.EntityFrameworkCore.PostgreSQL.Tests/Models/NpgSqlEntities/NpgSqlEnititesContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2424
modelBuilder.Entity<EntityWithArrays>(entity =>
2525
{
2626
});
27+
28+
modelBuilder.Entity<EntityWithXmin>(entity =>
29+
{
30+
entity.UseXminAsConcurrencyToken();
31+
});
32+
2733
}
2834

2935
public virtual DbSet<Event> Events { get; set; } = null!;
3036
public virtual DbSet<EntityWithArrays> EntityWithArrays { get; set; } = null!;
37+
public virtual DbSet<EntityWithXmin> EntityWithXmin { get; set; } = null!;
3138

3239
}
3340
}

Tests/LinqToDB.EntityFrameworkCore.PostgreSQL.Tests/NpgSqlTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ where Sql.Ext.PostgreSQL().Overlaps(m.Guids, guids)
8787
}
8888
}
8989

90+
[Test]
91+
public void TestConcurrencyToken()
92+
{
93+
using var db = CreateNpgSqlEntitiesContext();
94+
95+
var toInsert = Enumerable.Range(1, 10)
96+
.Select(i => new EntityWithXmin { Value = "Str" + i })
97+
.ToArray();
98+
99+
db.BulkCopy(toInsert);
100+
}
101+
102+
90103
[Test]
91104
public void TestUnnest()
92105
{

0 commit comments

Comments
 (0)