NpgsqlModelValidator rejects properties mapped via ValueConverter in EF Core 10
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 48/100
研究方向
Start by reading EF Core 10's ModelValidator.ValidatePropertyMapping and the related relational validation code described in the report; compare their behavior with EF Core 9. Reproduce the failure with the supplied ValueConverter example and check the provider's model validation tests for a suitable regression test. Done means converter-backed properties with a valid provider type mapping pass validation, while genuinely unmapped properties still fail.
由索引模型根據 Issue 內容生成。
描述
NpgsqlModelValidator rejects properties mapped via ValueConverter (no native type mapping) in EF Core 10
Environment
Microsoft.EntityFrameworkCore:10.0.2Npgsql.EntityFrameworkCore.PostgreSQL:10.0.0- .NET target framework:
net10.0 - PostgreSQL: any 10+
Symptoms
A model that maps a readonly struct (or any non-primitive CLR type) to a primitive database type via ValueConverter<TModel, TProvider> fails to build with:
System.InvalidOperationException: The property 'ItemDefinition.Name' could not be mapped
because it is of type 'LocalizedString', which is not a supported primitive type or a valid entity type.
Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by
using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IConventionTypeBase, IConventionModel, IDiagnosticsLogger`1)
at MUnique.OpenMU.Persistence.EntityFramework.EntityDataContext.OnModelCreating(...)
at ...
The same model builds and works correctly under EF Core 9 (Microsoft.EntityFrameworkCore 9.0.x + Npgsql.EntityFrameworkCore.PostgreSQL 9.0.x).
Root cause
EF Core 10's ModelValidator.ValidatePropertyMapping(IConventionTypeBase, IConventionModel, IDiagnosticsLogger<Validation>) runs:
IConventionProperty conventionProperty = structuralType.GetDeclaredProperties()
.FirstOrDefault(p =>
(!ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())
|| !p.IsImplicitlyCreated())
&& p.FindTypeMapping() == null);
if (conventionProperty != null)
ThrowPropertyNotMappedException(...);
FindTypeMapping() returns null for a property whose CLR type has no native relational mapping and whose mapping is established solely through a ValueConverter registered via OnModelCreating. EF Core 9's relational validator consulted the registered converter before this check; EF Core 10 does not.
Confirmed by decompiling both Microsoft.EntityFrameworkCore.Relational 10.0.0 and Npgsql.EntityFrameworkCore.PostgreSQL 10.0.0 with ILSpy.
Minimal reproduction
// Program.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql;
public readonly struct LocalizedString
{
public string Value { get; }
public LocalizedString(string? value) { Value = value; }
public static implicit operator string(LocalizedString ls) => ls.Value;
}
public class LocalizedStringConverter : ValueConverter<LocalizedString, string>
{
public LocalizedStringConverter() : base(ls => ls.Value ?? "", s => new LocalizedString(s)) { }
}
public class Item
{
public int Id { get; set; }
public LocalizedString Name { get; set; }
}
public class TestContext : DbContext
{
public DbSet<Item> Items => Set<Item>();
protected override void OnConfiguring(DbContextOptionsBuilder o)
=> o.UseNpgsql("Host=localhost;Database=test;Username=postgres;Password=postgres");
protected override void OnModelCreating(ModelBuilder m)
{
m.Entity<Item>(e =>
{
e.Property(p => p.Name).HasConversion(new LocalizedStringConverter());
});
}
}
public class Program
{
public static void Main()
{
using var ctx = new TestContext();
ctx.Database.OpenConnection(); // Triggers model validation
}
}
Expected: ctx.Database.OpenConnection() returns without throwing.
Actual: throws InvalidOperationException: The property 'Item.Name' could not be mapped because it is of type 'LocalizedString'.
The same code runs cleanly under EF Core 9 + Npgsql 9.0.4 (the same ValueConverter<LocalizedString, string> is sufficient to make EF Core 9 happy).
Impact
Many production codebases map readonly struct value objects (translated-text wrappers, currency, units of measure, JSON-shaped DTOs) through ValueConverter. EF Core 10's regression in this area forces users either to:
- mark the type as a complex type via
[ComplexType], which has different semantics and does not always work (verified against EF Core 10.0.2 with a converter-backed property on a complex type — the same exception is raised); - or to fork the provider with a custom
IModelValidatorthat catches the diagnostic (what I am doing locally to unblock OpenMU — seeffuhr91/OpenMU@fix/npgsql-validator-localizedstring).
Neither is a satisfying fix for the long term.
Suggested fix
ModelValidator.ValidatePropertyMapping(IConventionTypeBase, IConventionModel, IDiagnosticsLogger<Validation>) should consult property.GetValueConverter() (and, by extension, the resulting provider-side type mapping) before declaring the property unmapped. The EF Core 9 relational validator effectively did this; the EF Core 10 rewrite dropped the check.
A targeted fix could be:
IConventionProperty conventionProperty = structuralType.GetDeclaredProperties()
.FirstOrDefault(p =>
(!ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())
|| !p.IsImplicitlyCreated())
&& p.FindTypeMapping() == null
&& p.GetValueConverter() == null); // <-- new clause
This restores the EF Core 9 behaviour: properties with a registered ValueConverter are not flagged as "no type mapping", because the converter establishes the mapping to a supported provider type.
Suggested labels
bugarea: model validationseverity: highaffects: 10.0.0
Related
- OpenMU
ffuhr91/OpenMU@fix/npgsql-validator-localizedstring— a workingIModelValidatorsubclass that catches this diagnostic so the game server can boot while the upstream issue is open. - I also plan to file a separate report on the EF Core 10 SQL-generation regression that references a non-existent
ItemDefinitionTempId3column when serializing the OpenMUGameConfigurationaggregate.
- 主要語言
- C#
- 星號
- 1.8k
- 分支
- 268
- 平均合併
- 2 小時 35 分鐘
- 30 天內合併 PR
- 136
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
npgsql/efcore.pg 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 82/100
維護者通常 1 天內回覆
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSON可能已有人在做 @Ahmed7991 於 6 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 85/100
npgsql/efcore.pg#4093 · 1 則留言 ·
維護者通常 1 天內回覆
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by field可能已有人在做 @jeremy-morren 於 12 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01可能已有人在做 @Sorbitol 於 12 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 30/100
維護者通常 1 天內回覆
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never match可能已有人在做 @Mr444-dev 於 8 天前認領。 未關閉
難度 4/5 3-5 天 新手友好度 45/100
npgsql/efcore.pg#4099 · 1 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
難度 1/5 1-3 小時 新手友好度 90/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
SubtitleEdit/subtitleedit#16825 ·
維護者通常 1 天內回覆
-
ci-flake kind:tests size:S type:bug
難度 1/5 1 小時以內 新手友好度 82/100
elsa-workflows/elsa-foundation#4058 · 1 個 reaction ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
games-on-whales/wolf-ui#23 ·
-
Skills未關閉wayfinder:grilling
難度 2/5 1-3 小時 新手友好度 64/100
DigiBanks99/lopen#37 ·