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

Commit b857ebb

Browse files
committed
fix column.length load (#350)
(cherry picked from commit 4d990e8) (cherry picked from commit 292496a) (cherry picked from commit dc68fa6) # Conflicts: # Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs
1 parent 6f81d86 commit b857ebb

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,9 @@ public MappingAttribute[] GetAttributes(Type type, MemberInfo memberInfo)
302302
var skipOnUpdate = behaviour != PropertySaveBehavior.Save ||
303303
prop.ValueGenerated.HasFlag(ValueGenerated.OnUpdate);
304304

305-
(result ??= new()).Add(
306-
new ColumnAttribute()
305+
var ca = new ColumnAttribute()
307306
{
308307
Name = prop.GetColumnName(),
309-
Length = prop.GetMaxLength() ?? 0,
310308
CanBeNull = prop.IsNullable,
311309
DbType = prop.GetColumnType(),
312310
DataType = dataType,
@@ -316,8 +314,13 @@ public MappingAttribute[] GetAttributes(Type type, MemberInfo memberInfo)
316314
IsDiscriminator = discriminator == prop,
317315
SkipOnInsert = skipOnInsert,
318316
SkipOnUpdate = skipOnUpdate
319-
}
320-
);
317+
};
318+
319+
var maxLen = prop.GetMaxLength();
320+
if (maxLen != null)
321+
ca.Length = maxLen.Value;
322+
323+
(result ??= new()).Add(ca);
321324

322325
// ValueConverterAttribute
323326
var converter = prop.GetValueConverter();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ protected ForMappingContextBase(DbContextOptions options) : base(options)
1212
public DbSet<NoIdentity> NoIdentity { get; set; } = null!;
1313
public DbSet<UIntTable> UIntTable { get; set; } = null!;
1414
public DbSet<StringTypes> StringTypes { get; set; } = null!;
15+
public DbSet<TypesTable> Types { get; set; } = null!;
1516

1617
public DbSet<WithDuplicateProperties> WithDuplicateProperties { get; set; } = null!;
1718

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
5+
{
6+
public class TypesTable
7+
{
8+
[Key]
9+
public int Id { get; set; }
10+
11+
public DateTime? DateTime { get; set; }
12+
public string? String { get; set; }
13+
14+
// add more if needed for tests
15+
}
16+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ public void TestStringMappings()
5454
}
5555
}
5656

57+
[Test(Description = "https://github.com/linq2db/linq2db.EntityFrameworkCore/issues/349")]
58+
public void TestColumnLengthMappings()
59+
{
60+
using (var db = CreateContext())
61+
{
62+
var ms = LinqToDBForEFTools.GetMappingSchema(db.Model, db, null);
63+
var ed = ms.GetEntityDescriptor(typeof(TypesTable));
64+
65+
ed.Columns.First(c => c.MemberName == nameof(TypesTable.DateTime)).Length.Should().BeNull();
66+
ed.Columns.First(c => c.MemberName == nameof(TypesTable.String)).Length.Should().Be(100);
67+
}
68+
}
69+
5770
[Test]
5871
public void TestDialectUse()
5972
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3232
b.Property(e => e.UnicodeString).HasMaxLength(50).IsUnicode();
3333
}
3434
);
35-
35+
36+
modelBuilder.Entity<TypesTable>(b =>
37+
{
38+
b.Property(e => e.DateTime);
39+
b.Property(e => e.String).HasMaxLength(100);
40+
});
41+
3642
modelBuilder.Entity<WithInheritance>(b =>
3743
{
3844
b.HasDiscriminator(x => x.Discriminator);

0 commit comments

Comments
 (0)