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

Commit 9f9dd36

Browse files
committed
2 parents 2c94b48 + 83dc47e commit 9f9dd36

File tree

22 files changed

+397
-82
lines changed

22 files changed

+397
-82
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,25 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
158158
}
159159

160160
var isIdentity = prop.GetAnnotations()
161-
.Any(a => a.Name.EndsWith(":ValueGenerationStrategy") && a.Value?.ToString() == "IdentityColumn");
162-
163-
if (!isIdentity && isPrimaryKey)
164-
{
165-
isIdentity = prop.ValueGenerated == ValueGenerated.OnAdd;
166-
}
161+
.Any(a =>
162+
{
163+
if (a.Name.EndsWith(":ValueGenerationStrategy"))
164+
return a.Value?.ToString().Contains("Identity") == true;
165+
166+
if (a.Name.EndsWith(":Autoincrement"))
167+
return a.Value is bool b && b;
168+
169+
// for postgres
170+
if (a.Name == "Relational:DefaultValueSql")
171+
{
172+
if (a.Value is string str)
173+
{
174+
return str.ToLower().Contains("nextval");
175+
}
176+
}
177+
178+
return false;
179+
});
167180

168181
var storeObjectId = GetStoreObjectIdentifier(et);
169182

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using JetBrains.Annotations;
1919
using Microsoft.EntityFrameworkCore.Diagnostics;
20+
using Microsoft.Extensions.Caching.Memory;
2021

2122
namespace LinqToDB.EntityFrameworkCore
2223
{
@@ -84,10 +85,10 @@ public override int GetHashCode()
8485
#endregion
8586
}
8687

87-
readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new ConcurrentDictionary<ProviderKey, IDataProvider>();
88+
readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new();
8889

89-
private readonly MemoryCache _schemaCache = new MemoryCache(
90-
new MemoryCacheOptions
90+
private readonly MemoryCache _schemaCache = new(
91+
new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()
9192
{
9293
ExpirationScanFrequency = TimeSpan.FromHours(1.0)
9394
});

Source/LinqToDB.EntityFrameworkCore/linq2db.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="linq2db" Version="3.3.0" />
26+
<PackageReference Include="linq2db" Version="3.4.0" />
2727
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.2" />
2828
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
2929
<PrivateAssets>all</PrivateAssets>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using LinqToDB.Data;
6+
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
7+
using NUnit.Framework;
8+
9+
namespace LinqToDB.EntityFrameworkCore.BaseTests
10+
{
11+
public abstract class ForMappingTestsBase : TestsBase
12+
{
13+
public abstract ForMappingContextBase CreateContext();
14+
15+
[Test]
16+
public virtual void TestIdentityMapping()
17+
{
18+
using var context = CreateContext();
19+
using var connection = context.CreateLinqToDbConnection();
20+
21+
var ed = connection.MappingSchema.GetEntityDescriptor(typeof(WithIdentity));
22+
var pk = ed.Columns.Where(c => c.IsPrimaryKey).Single();
23+
24+
pk.IsIdentity.Should().BeTrue();
25+
}
26+
27+
[Test]
28+
public virtual void TestNoIdentityMapping()
29+
{
30+
using var context = CreateContext();
31+
using var connection = context.CreateLinqToDbConnection();
32+
33+
var ed = connection.MappingSchema.GetEntityDescriptor(typeof(NoIdentity));
34+
var pk = ed.Columns.Where(c => c.IsPrimaryKey).Single();
35+
36+
pk.IsIdentity.Should().BeFalse();
37+
}
38+
39+
[Test]
40+
public virtual void TestTableCreation()
41+
{
42+
using var context = CreateContext();
43+
using var connection = context.CreateLinqToDbConnection();
44+
45+
using var t1 = connection.CreateTempTable<WithIdentity>();
46+
using var t2 = connection.CreateTempTable<NoIdentity>();
47+
}
48+
49+
50+
[Test]
51+
public virtual void TestBulkCopyNoIdentity()
52+
{
53+
using var context = CreateContext();
54+
using var connection = context.CreateLinqToDbConnection();
55+
56+
using var t = connection.CreateTempTable<NoIdentity>();
57+
58+
var items = new List<NoIdentity>()
59+
{
60+
new() {Id = Guid.NewGuid(), Name = "John Doe"},
61+
new() {Id = Guid.NewGuid(), Name = "Jane Doe"}
62+
};
63+
64+
t.BulkCopy(items);
65+
66+
67+
items.Should().BeEquivalentTo(t);
68+
}
69+
70+
[Test]
71+
public virtual void TestBulkCopyWithIdentity()
72+
{
73+
using var context = CreateContext();
74+
using var connection = context.CreateLinqToDbConnection();
75+
76+
using var t = connection.CreateTempTable<WithIdentity>();
77+
78+
var items = new List<WithIdentity>()
79+
{
80+
new() {Id = 1, Name = "John Doe"},
81+
new() {Id = 2, Name = "Jane Doe"}
82+
};
83+
84+
t.BulkCopy(items);
85+
86+
87+
t.Should().HaveCount(items.Count);
88+
}
89+
90+
}
91+
}

Tests/LinqToDB.EntityFrameworkCore.BaseTests/LinqToDB.EntityFrameworkCore.BaseTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1213
<PackageReference Include="linq2db.Tools" Version="3.1.6" />
1314
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
1415
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
4+
{
5+
public abstract class ForMappingContextBase : DbContext
6+
{
7+
protected ForMappingContextBase(DbContextOptions options) : base(options)
8+
{
9+
}
10+
11+
public DbSet<WithIdentity> WithIdentity { get; set; } = null!;
12+
public DbSet<NoIdentity> NoIdentity { get; set; } = null!;
13+
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
4+
{
5+
public class NoIdentity
6+
{
7+
public Guid Id { get; set; }
8+
public string Name { get; set; } = null!;
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
2+
{
3+
public class WithIdentity
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; } = null!;
7+
}
8+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using LinqToDB.Data;
6+
using LinqToDB.EntityFrameworkCore.BaseTests;
7+
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
8+
using LinqToDB.EntityFrameworkCore.PomeloMySql.Tests.Models.ForMapping;
9+
using Microsoft.EntityFrameworkCore;
10+
11+
namespace LinqToDB.EntityFrameworkCore.PomeloMySql.Tests
12+
{
13+
public class ForMappingTests : ForMappingTestsBase
14+
{
15+
public override ForMappingContextBase CreateContext()
16+
{
17+
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
18+
optionsBuilder.UseMySql("Server=DBHost;Port=3306;Database=TestData;Uid=TestUser;Pwd=TestPassword;charset=utf8;");
19+
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);
20+
21+
var options = optionsBuilder.Options;
22+
var ctx = new ForMappingContext(options);
23+
24+
//ctx.Database.EnsureDeleted();
25+
ctx.Database.EnsureCreated();
26+
27+
return ctx;
28+
}
29+
30+
}
31+
}

Tests/LinqToDB.EntityFrameworkCore.PomeloMySql.Tests/LinqToDB.EntityFrameworkCore.PomeloMySql.Tests.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,4 @@
1717
<ProjectReference Include="..\LinqToDB.EntityFrameworkCore.BaseTests\LinqToDB.EntityFrameworkCore.BaseTests.csproj" />
1818
</ItemGroup>
1919

20-
<ItemGroup>
21-
<Folder Include="Models\" />
22-
</ItemGroup>
23-
2420
</Project>

0 commit comments

Comments
 (0)