-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EF Core 9 generates incorrect compiled models for negative enum sentinel values #35142
Comments
The error is |
@Atulin I am not able to reproduce this. Following this change: #31923, the generated code should contain var role = runtimeEntityType.AddProperty(
"Role",
typeof(EClubMemberRoles),
propertyInfo: typeof(Sensor).GetProperty("Role", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(Sensor).GetField("<Role>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly),
valueGenerated: ValueGenerated.OnAdd);
role.SetSentinelFromProviderValue(-1);
role.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.None);
role.AddAnnotation("Relational:DefaultValue", EClubMemberRoles.User); Repro code: public class ContextOfDoom : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.EnableSensitiveDataLogging()
.LogTo(Console.WriteLine, LogLevel.Information)
.UseNpgsql();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Sensor>(b =>
{
b.Property(cm => cm.Role)
.IsRequired()
.HasDefaultValue(EClubMemberRoles.User)
.HasSentinel((EClubMemberRoles)(-1));
});
}
}
public class Sensor
{
public int Id { get; set; }
public EClubMemberRoles Role { get; set; }
}
public enum EClubMemberRoles
{
User,
Admin
} |
I don't see the enum registered with NpgSQL according to its documentation. Your code probably treats the value as a plain int because of that, while mine somehow differs because the enums are registered. I will edit your code shortly, soon as I'm home. |
@ajcvickers Here's a reproduction: https://github.com/Atulin/EfEnumTest ModelEntityType.cs#L51 has the un-parenthesised Could it be related to NpgSQL, not to EF itself, after all? As a side note, when the context is placed in a global namespace, it seems |
EF Core version: 9.0
Database provider: Npgsql (efcore.pg)
Target framework: .NET 9
Operating system: Windows 10
IDE: Rider 2024.3
Configuration:
Compiled model:
As you can see, it omits the parentheses around the
-1
which leads to the error.The text was updated successfully, but these errors were encountered: