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

Commit 871d121

Browse files
committed
Porting changes from 5.x.
1 parent 9cf9f54 commit 871d121

File tree

11 files changed

+174
-33
lines changed

11 files changed

+174
-33
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using System.Data;
45
using System.Linq;
56
using System.Linq.Expressions;
67
using System.Reflection;
@@ -132,6 +133,41 @@ static bool CompareProperty(IProperty property, MemberInfo memberInfo)
132133
{
133134
return CompareProperty(property.GetIdentifyingMemberInfo(), memberInfo);
134135
}
136+
137+
static DataType DbTypeToDataType(DbType dbType)
138+
{
139+
return dbType switch
140+
{
141+
DbType.AnsiString => DataType.VarChar,
142+
DbType.AnsiStringFixedLength => DataType.VarChar,
143+
DbType.Binary => DataType.Binary,
144+
DbType.Boolean => DataType.Boolean,
145+
DbType.Byte => DataType.Byte,
146+
DbType.Currency => DataType.Money,
147+
DbType.Date => DataType.Date,
148+
DbType.DateTime => DataType.DateTime,
149+
DbType.DateTime2 => DataType.DateTime2,
150+
DbType.DateTimeOffset => DataType.DateTimeOffset,
151+
DbType.Decimal => DataType.Decimal,
152+
DbType.Double => DataType.Double,
153+
DbType.Guid => DataType.Guid,
154+
DbType.Int16 => DataType.Int16,
155+
DbType.Int32 => DataType.Int32,
156+
DbType.Int64 => DataType.Int64,
157+
DbType.Object => DataType.Undefined,
158+
DbType.SByte => DataType.SByte,
159+
DbType.Single => DataType.Single,
160+
DbType.String => DataType.NVarChar,
161+
DbType.StringFixedLength => DataType.NVarChar,
162+
DbType.Time => DataType.Time,
163+
DbType.UInt16 => DataType.UInt16,
164+
DbType.UInt32 => DataType.UInt32,
165+
DbType.UInt64 => DataType.UInt64,
166+
DbType.VarNumeric => DataType.VarNumeric,
167+
DbType.Xml => DataType.Xml,
168+
_ => DataType.Undefined
169+
};
170+
}
135171

136172
public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = true) where T : Attribute
137173
{
@@ -184,16 +220,34 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
184220
return false;
185221
});
186222

187-
return new T[]{(T)(Attribute) new ColumnAttribute
223+
var dataType = DataType.Undefined;
224+
225+
if (prop.GetTypeMapping() is RelationalTypeMapping typeMapping)
226+
{
227+
if (typeMapping.DbType != null)
228+
{
229+
dataType = DbTypeToDataType(typeMapping.DbType.Value);
230+
}
231+
else
232+
{
233+
dataType = SqlDataType.GetDataType(typeMapping.ClrType).Type.DataType;
234+
}
235+
}
236+
237+
return new T[]
188238
{
189-
Name = prop.GetColumnName(),
190-
Length = prop.GetMaxLength() ?? 0,
191-
CanBeNull = prop.IsNullable,
192-
DbType = prop.GetColumnType(),
193-
IsPrimaryKey = isPrimaryKey,
194-
PrimaryKeyOrder = primaryKeyOrder,
195-
IsIdentity = isIdentity,
196-
}};
239+
(T)(Attribute)new ColumnAttribute
240+
{
241+
Name = prop.GetColumnName(),
242+
Length = prop.GetMaxLength() ?? 0,
243+
CanBeNull = prop.IsNullable,
244+
DbType = prop.GetColumnType(),
245+
DataType = dataType,
246+
IsPrimaryKey = isPrimaryKey,
247+
PrimaryKeyOrder = primaryKeyOrder,
248+
IsIdentity = isIdentity,
249+
}
250+
};
197251
}
198252
}
199253

Tests/LinqToDB.EntityFrameworkCore.BaseTests/ForMappingTestsBase.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using FluentAssertions;
56
using LinqToDB.Data;
67
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
@@ -87,5 +88,25 @@ public virtual void TestBulkCopyWithIdentity()
8788
t.Should().HaveCount(items.Count);
8889
}
8990

91+
[Test]
92+
public virtual async Task TestUIntTable()
93+
{
94+
using var context = CreateContext();
95+
context.UIntTable.Add(new UIntTable
96+
{
97+
Field16 = 1,
98+
Field16N = 2,
99+
Field32 = 3,
100+
Field32N = 4,
101+
Field64 = 5,
102+
Field64N = 6
103+
});
104+
105+
await context.SaveChangesAsync();
106+
107+
ulong field64 = 5;
108+
var item = await context.UIntTable.FirstOrDefaultAsyncLinqToDB(e => e.Field64 == field64);
109+
}
110+
90111
}
91112
}

Tests/LinqToDB.EntityFrameworkCore.BaseTests/Models/ForMapping/ForMappingContextBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ protected ForMappingContextBase(DbContextOptions options) : base(options)
1010

1111
public DbSet<WithIdentity> WithIdentity { get; set; } = null!;
1212
public DbSet<NoIdentity> NoIdentity { get; set; } = null!;
13-
13+
public DbSet<UIntTable> UIntTable { get; set; } = null!;
14+
public DbSet<StringTypes> StringTypes { get; set; } = null!;
1415
}
1516
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
4+
{
5+
public class StringTypes
6+
{
7+
[Key]
8+
public int Id { get; set; }
9+
10+
public string? AnsiString { get; set; }
11+
12+
public string? UnicodeString { get; set; }
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
4+
{
5+
public class UIntTable
6+
{
7+
[Key]
8+
public int ID { get; set; }
9+
10+
public ushort Field16 { get; set; }
11+
public uint Field32 { get; set; }
12+
public ulong Field64 { get; set; }
13+
public ushort? Field16N { get; set; }
14+
public uint? Field32N { get; set; }
15+
public ulong? Field64N { get; set; }
16+
}
17+
}

Tests/LinqToDB.EntityFrameworkCore.PomeloMySql.Tests/ForMappingTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using FluentAssertions;
5-
using LinqToDB.Data;
6-
using LinqToDB.EntityFrameworkCore.BaseTests;
1+
using LinqToDB.EntityFrameworkCore.BaseTests;
72
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
83
using LinqToDB.EntityFrameworkCore.PomeloMySql.Tests.Models.ForMapping;
94
using Microsoft.EntityFrameworkCore;
@@ -12,6 +7,8 @@ namespace LinqToDB.EntityFrameworkCore.PomeloMySql.Tests
127
{
138
public class ForMappingTests : ForMappingTestsBase
149
{
10+
private bool _isDbCreated;
11+
1512
public override ForMappingContextBase CreateContext()
1613
{
1714
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
@@ -21,8 +18,13 @@ public override ForMappingContextBase CreateContext()
2118
var options = optionsBuilder.Options;
2219
var ctx = new ForMappingContext(options);
2320

24-
//ctx.Database.EnsureDeleted();
25-
ctx.Database.EnsureCreated();
21+
if (!_isDbCreated)
22+
{
23+
ctx.Database.EnsureDeleted();
24+
ctx.Database.EnsureCreated();
25+
26+
_isDbCreated = true;
27+
}
2628

2729
return ctx;
2830
}

Tests/LinqToDB.EntityFrameworkCore.PostgreSQL.Tests/ForMappingTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using FluentAssertions;
5-
using LinqToDB.Data;
6-
using LinqToDB.EntityFrameworkCore.BaseTests;
1+
using LinqToDB.EntityFrameworkCore.BaseTests;
72
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
83
using LinqToDB.EntityFrameworkCore.PostgreSQL.Tests.Models.ForMapping;
94
using Microsoft.EntityFrameworkCore;
@@ -14,6 +9,8 @@ namespace LinqToDB.EntityFrameworkCore.PostgreSQL.Tests
149
[TestFixture]
1510
public class ForMappingTests : ForMappingTestsBase
1611
{
12+
private bool _isDbCreated;
13+
1714
public override ForMappingContextBase CreateContext()
1815
{
1916
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
@@ -23,8 +20,13 @@ public override ForMappingContextBase CreateContext()
2320
var options = optionsBuilder.Options;
2421
var ctx = new ForMappingContext(options);
2522

26-
//ctx.Database.EnsureDeleted();
27-
ctx.Database.EnsureCreated();
23+
if (!_isDbCreated)
24+
{
25+
ctx.Database.EnsureDeleted();
26+
ctx.Database.EnsureCreated();
27+
28+
_isDbCreated = true;
29+
}
2830

2931
return ctx;
3032
}

Tests/LinqToDB.EntityFrameworkCore.SQLite.Tests/ForMappingTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public override ForMappingContextBase CreateContext()
1818
var options = optionsBuilder.Options;
1919
var ctx = new ForMappingContext(options);
2020

21+
ctx.Database.OpenConnection();
2122
ctx.Database.EnsureCreated();
2223

2324
return ctx;

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/ForMappingTests.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using LinqToDB.EntityFrameworkCore.BaseTests;
1+
using System.Linq;
2+
using FluentAssertions;
3+
using LinqToDB.EntityFrameworkCore.BaseTests;
24
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
35
using LinqToDB.EntityFrameworkCore.SqlServer.Tests.Models.ForMapping;
46
using Microsoft.EntityFrameworkCore;
@@ -9,6 +11,8 @@ namespace LinqToDB.EntityFrameworkCore.SqlServer.Tests
911
[TestFixture]
1012
public class ForMappingTests : ForMappingTestsBase
1113
{
14+
private bool _isDbCreated;
15+
1216
public override ForMappingContextBase CreateContext()
1317
{
1418
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
@@ -18,10 +22,32 @@ public override ForMappingContextBase CreateContext()
1822
var options = optionsBuilder.Options;
1923
var ctx = new ForMappingContext(options);
2024

21-
//ctx.Database.EnsureDeleted();
22-
ctx.Database.EnsureCreated();
25+
if (!_isDbCreated)
26+
{
27+
ctx.Database.EnsureDeleted();
28+
ctx.Database.EnsureCreated();
29+
30+
_isDbCreated = true;
31+
}
2332

2433
return ctx;
2534
}
35+
36+
[Test]
37+
public void TestStringMappings()
38+
{
39+
using (var db = CreateContext())
40+
{
41+
var ms = LinqToDBForEFTools.GetMappingSchema(db.Model, db);
42+
var ed = ms.GetEntityDescriptor(typeof(StringTypes));
43+
44+
ed.Columns.First(c => c.MemberName == nameof(StringTypes.AnsiString)).DataType.Should()
45+
.Be(DataType.VarChar);
46+
47+
ed.Columns.First(c => c.MemberName == nameof(StringTypes.UnicodeString)).DataType.Should()
48+
.Be(DataType.NVarChar);
49+
}
50+
51+
}
2652
}
2753
}

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/Models/ForMapping/ForMappingContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2525
{
2626
b.HasKey(e => e.Id);
2727
});
28+
29+
modelBuilder.Entity<StringTypes>(b =>
30+
{
31+
b.Property(e => e.AnsiString).HasMaxLength(50).IsUnicode(false);
32+
b.Property(e => e.UnicodeString).HasMaxLength(50).IsUnicode();
33+
}
34+
);
2835
}
2936
}
3037
}

0 commit comments

Comments
 (0)