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

Commit 0c3a1d6

Browse files
committed
Added better Insert skip check.
1 parent 2c64d3e commit 0c3a1d6

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

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

259+
var behaviour = prop.GetBeforeSaveBehavior();
260+
var skipOnInsert = prop.ValueGenerated.HasFlag(ValueGenerated.OnAdd);
261+
262+
if (skipOnInsert)
263+
{
264+
skipOnInsert = isIdentity || behaviour != PropertySaveBehavior.Save;
265+
}
266+
267+
var skipOnUpdate = behaviour != PropertySaveBehavior.Save ||
268+
prop.ValueGenerated.HasFlag(ValueGenerated.OnUpdate);
269+
259270
return new T[]
260271
{
261272
(T)(Attribute)new ColumnAttribute
@@ -268,7 +279,9 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
268279
IsPrimaryKey = isPrimaryKey,
269280
PrimaryKeyOrder = primaryKeyOrder,
270281
IsIdentity = isIdentity,
271-
IsDiscriminator = discriminator == prop
282+
IsDiscriminator = discriminator == prop,
283+
SkipOnInsert = skipOnInsert,
284+
SkipOnUpdate = skipOnUpdate
272285
}
273286
};
274287
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace LinqToDB.EntityFrameworkCore.PostgreSQL.Tests.Models.NpgSqlEntities
5+
{
6+
public class EntityWithXmin
7+
{
8+
[Key]
9+
public int Id { get; set; }
10+
11+
public uint xmin { get; set; }
12+
public string Value { get; set; } = null!;
13+
}
14+
}

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)