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

Commit 51835a4

Browse files
committed
Added better Insert skip check.
1 parent ff21fd5 commit 51835a4

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

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

262+
var behaviour = prop.BeforeSaveBehavior;
263+
var skipOnInsert = prop.ValueGenerated.HasFlag(ValueGenerated.OnAdd);
264+
265+
if (skipOnInsert)
266+
{
267+
skipOnInsert = isIdentity || behaviour != PropertySaveBehavior.Save;
268+
}
269+
270+
var skipOnUpdate = behaviour != PropertySaveBehavior.Save ||
271+
prop.ValueGenerated.HasFlag(ValueGenerated.OnUpdate);
272+
262273
return new T[]
263274
{
264275
(T)(Attribute)new ColumnAttribute
@@ -271,7 +282,9 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
271282
IsPrimaryKey = isPrimaryKey,
272283
PrimaryKeyOrder = primaryKeyOrder,
273284
IsIdentity = isIdentity,
274-
IsDiscriminator = discriminator == prop
285+
IsDiscriminator = discriminator == prop,
286+
SkipOnInsert = skipOnInsert,
287+
SkipOnUpdate = skipOnUpdate
275288
}
276289
};
277290
}
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
@@ -26,10 +26,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2626
modelBuilder.Entity<EntityWithArrays>(entity =>
2727
{
2828
});
29+
30+
modelBuilder.Entity<EntityWithXmin>(entity =>
31+
{
32+
entity.ForNpgsqlUseXminAsConcurrencyToken();
33+
});
34+
2935
}
3036

3137
public virtual DbSet<Event> Events { get; set; } = null!;
3238
public virtual DbSet<EntityWithArrays> EntityWithArrays { get; set; } = null!;
39+
public virtual DbSet<EntityWithXmin> EntityWithXmin { get; set; } = null!;
3340

3441
}
3542
}

0 commit comments

Comments
 (0)