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

Commit 292496a

Browse files
committed
fix column.length load (#350)
(cherry picked from commit 4d990e8)
1 parent 37d4749 commit 292496a

File tree

5 files changed

+56
-17
lines changed

5 files changed

+56
-17
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

+19-16
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,25 @@ public MappingAttribute[] GetAttributes(Type type, MemberInfo memberInfo)
308308
var skipOnUpdate = behaviour != PropertySaveBehavior.Save ||
309309
prop.ValueGenerated.HasFlag(ValueGenerated.OnUpdate);
310310

311-
(result ??= new()).Add(
312-
new ColumnAttribute()
313-
{
314-
Name = storeObjectId != null ? prop.GetColumnName(storeObjectId.Value) : null,
315-
Length = prop.GetMaxLength() ?? 0,
316-
CanBeNull = prop.IsNullable,
317-
DbType = prop.GetColumnType(),
318-
DataType = dataType,
319-
IsPrimaryKey = isPrimaryKey,
320-
PrimaryKeyOrder = primaryKeyOrder,
321-
IsIdentity = isIdentity,
322-
IsDiscriminator = discriminator == prop,
323-
SkipOnInsert = skipOnInsert,
324-
SkipOnUpdate = skipOnUpdate
325-
}
326-
);
311+
var ca = new ColumnAttribute()
312+
{
313+
Name = storeObjectId != null ? prop.GetColumnName(storeObjectId.Value) : null,
314+
CanBeNull = prop.IsNullable,
315+
DbType = prop.GetColumnType(),
316+
DataType = dataType,
317+
IsPrimaryKey = isPrimaryKey,
318+
PrimaryKeyOrder = primaryKeyOrder,
319+
IsIdentity = isIdentity,
320+
IsDiscriminator = discriminator == prop,
321+
SkipOnInsert = skipOnInsert,
322+
SkipOnUpdate = skipOnUpdate
323+
};
324+
325+
var maxLen = prop.GetMaxLength();
326+
if (maxLen != null)
327+
ca.Length = maxLen.Value;
328+
329+
(result ??= new()).Add(ca);
327330

328331
// ValueConverterAttribute
329332
var converter = prop.GetValueConverter();

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

+1
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

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

+13
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

+7-1
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)