This worked without issue in EF Core 8, but causes an error when trying to add migrations in EF Core 9.
If you have an enum array property on your model and set its column type to text[] in the context's ConfigureConventions method, trying to add a migration results in this error:
System.InvalidOperationException: Cannot scaffold C# literals of type 'System.Reflection.NullabilityInfoContext'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.<Fragment>g__AppendMethodCall|57_0(IMethodCallCodeFragment current, <>c__DisplayClass57_0&)
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Fragment(IMethodCallCodeFragment fragment, Int32 indent)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotations(String builderName, IAnnotatable annotatable, IndentedStringBuilder stringBuilder, Dictionary`2 annotations, Boolean inChainedCall, Boolean leadingNewline, MethodInfo hasAnnotationMethodInfo)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String modelBuilderName, IModel model, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Cannot scaffold C# literals of type 'System.Reflection.NullabilityInfoContext'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.
Here's some minimal code to reproduce this problem. Trying to create a migration (for example dotnet ef migrations add InitialSchema) will throw the above error using EF Core 9, but works fine in EF Core 8:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Npgsql;
public static class Program {
public static void Main(string[] args) {
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
builder.Services.AddDbContext<ApplicationDbContext>();
var app = builder.Build();
app.UseHttpsRedirection();
app.Run();
}
}
public enum TestEnum {
ValueOne,
ValueTwo,
}
[Table("data_model")]
public class DataModel {
[Key]
public Guid Id { get; set; }
public TestEnum[]? Tests { get; set; }
}
public class ApplicationDbContext : DbContext {
public required DbSet<DataModel> DataModels { get; set; }
private static NpgsqlDataSource? _ds = null;
private static NpgsqlDataSource DataSource {
get {
if (_ds != null) return _ds;
var cb = new NpgsqlConnectionStringBuilder();
cb.Username = "postgres";
cb.Password = "password";
cb.Host = "localhost";
cb.Database = "postgres";
cb.IncludeErrorDetail = true;
cb.CommandTimeout = 300;
var dsBuilder = new NpgsqlDataSourceBuilder(cb.ToString());
_ds = dsBuilder.Build();
return _ds;
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseNpgsql(DataSource, builder => {
builder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
});
}
protected override void ConfigureConventions(
ModelConfigurationBuilder configurationBuilder) {
configurationBuilder.Properties<TestEnum>().HaveColumnType("text");
configurationBuilder.Properties<TestEnum[]>().HaveColumnType("text[]");
}
}
The problem seems to be this line specifically, as removing it fixes the error (but the column is created as int[] instead of text[]):
configurationBuilder.Properties<TestEnum[]>().HaveColumnType("text[]");
Setting the column's type with [Column(TypeName = "text[]")] instead does not exhibit the same problem, so that may be a workaround for the time being (however for projects with lots of enum array columns across different enum types it's a lot more cumbersome than configuring via convention).
This worked without issue in EF Core 8, but causes an error when trying to add migrations in EF Core 9.
If you have an enum array property on your model and set its column type to
text[]in the context'sConfigureConventionsmethod, trying to add a migration results in this error:Here's some minimal code to reproduce this problem. Trying to create a migration (for example
dotnet ef migrations add InitialSchema) will throw the above error using EF Core 9, but works fine in EF Core 8:The problem seems to be this line specifically, as removing it fixes the error (but the column is created as
int[]instead oftext[]):Setting the column's type with
[Column(TypeName = "text[]")]instead does not exhibit the same problem, so that may be a workaround for the time being (however for projects with lots of enum array columns across different enum types it's a lot more cumbersome than configuring via convention).