Breaking change in 2.2: Column is null
I just upgraded my projects from ASP.NET Core 2.1 to version 2.2, including all other packages, with one being the Npgsql.EntityFrameworkCore.PostgreSQL package. After I did the upgrade I received an error while running my application. The error was: InvalidCastException: Column is null.
In order to locate the issue I tried to add a new migration. Since I didn't change the DbContext I expected it to create an empty migration. That was not the case. It created a migration, which changed the type of a column in one table from Guid? to Guid. This also explains the error, because the table contains entries where the value in that column is null, which it didn't expect.
I have the same Guid? definition in multiple tables and it broke only in this one table. What is different in this table, compared to the other is, that I create an index on the column.
In short I used something like the following snippet in my ModelCreation method in version 2.1:
class MyTable {
// ...
public Guid? MyGuidColumn {get;set;}
}
OnModelCreating {
// ....
e.HasIndex(p => p.MyGuidColumn).IsUnique(false);
}
In version 2.2 I have to use the following, in order for npgsql to recognize the column as nullable:
OnModelCreating {
// ....
e.Property(p => p.MyGuidColumn).IsRequired(false);
e.HasIndex(p => p.MyGuidColumn).IsUnique(false);
}
I don't know if this was an intended change, because it was not very intuitive to notice, that creating an Index automatically makes the nullable a not nullable column, if the column isn't explicitly created using the Property function.
@faryu I attempted to reproduce this in austindrenski/EFCore.PG-765, but was unable to do so.
Could you please link to a repository that provides a complete (minimal) reproduction of this issue?
Ok, I will try to reproduce the issue in a sample.
So far I didn't manage to reproduce the issue in a sample project. Since it isn't a big issue we could leave it as it is for now and wait to see if others also face this issue.
Okay, in that case I'll close for now, but please let us know if you encounter this issue again.
I have faced this issue too after upgrade from version 2.1 to 2.2, but i was can't reproduce this in a sample project. I hope that stack trace could help you to find the problem.
[InvalidCastException: Column is null]
Npgsql.NpgsqlDefaultDataReader.GetFieldValue(Int32 column) +731
Npgsql.NpgsqlDataReader.GetString(Int32 ordinal) +31
lambda_method(Closure , DbDataReader ) +459
Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader) +18
Microsoft.EntityFrameworkCore.Query.Internal.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer) +281
Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute(TState state, Func`3 operation, Func`3 verifySucceeded) +160
Microsoft.EntityFrameworkCore.Query.Internal.Enumerator.MoveNext() +100
Microsoft.EntityFrameworkCore.Query.Internal.<_TrackEntities>d__17`2.MoveNext() +139
Microsoft.EntityFrameworkCore.Query.Internal.EnumeratorExceptionInterceptor.MoveNext() +172
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Load(IQueryable`1 source) +107
Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.Load(INavigation navigation, InternalEntityEntry entry) +117
Microsoft.EntityFrameworkCore.ChangeTracking.NavigationEntry.Load() +64
Microsoft.EntityFrameworkCore.Internal.LazyLoader.Load(Object entity, String navigationName) +79
Microsoft.EntityFrameworkCore.Proxies.Internal.LazyLoadingInterceptor.Intercept(IInvocation invocation) +258
Castle.DynamicProxy.AbstractInvocation.Proceed() +130
Castle.Proxies.LojaProxy.get_Empresa() +126
lambda_method(Closure , Loja ) +59
AutoMapper.DelegateBasedResolver`2.Resolve(ResolutionResult source) +97
AutoMapper.NullReferenceExceptionSwallowingResolver.Resolve(ResolutionResult source) +41
AutoMapper.PropertyMap.<ResolveValue>b__6(ResolutionResult current, IValueResolver resolver) +10
System.Linq.Enumerable.Aggregate(IEnumerable`1 source, TAccumulate seed, Func`3 func) +152
AutoMapper.PropertyMap.ResolveValue(ResolutionContext context) +169
AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +81
Unfortunately the stack trace doesn't really tell us what's going on here... I hope you (or someone else) manages to reproduce this...
Hi,
i had the same error and i think i know how the error can be reproduced.
I had a table with a nullable field and the field was defined as required in the entity type configuration. I removed the IsRequired() call and after that all worked like a charm.
Here an example table so that the problem can be better understood.
create table public.test (
id bigint not null primary key,
name text not null unique,
created_on timestamp with time zone not null default transaction_timestamp(),
created_by text not null default current_user,
last_changed_on timestamp with time zone default null,
last_changed_by text default null
);
Here the entity type configuration.
public class TestConfiguration : IEntityTypeConfiguration<TestModel>
{
public virtual void Configure(EntityTypeBuilder<TestModel> builder)
{
builder.Property(e => e.Id)
.HasColumnName("id")
.ValueGeneratedOnAdd();
builder
.Property(e => e.Name)
.IsRequired()
.HasColumnName("name");
builder
.Property(e => e.CreatedOn)
.IsRequired()
.HasColumnName("created_on");
builder
.Property(e => e.CreatedBy)
.IsRequired()
.HasColumnName("created_by");
builder
.Property(e => e.LastChangedBy)
.IsRequired() // <-- I removed the IsRequired() and after that all worked like a charm and the error "Column is null" was fixed
.HasColumnName("last_changed_by");
builder
.Property(e => e.LastChangedOn)
.HasColumnName("last_changed_on");
}
}
I hope that helps to correct the error. And thank you for the great work.
Kind regards, Stefan
Same problem here, I'm trying to isolate it and create a demo.
@eschgi if you had a property with IsRequired() mapped to a nullable database column containing an actual null, then it makes total sense that you would get this error... By saying IsRequired() you're telling EF Core that there will never be nulls coming from the database... If you're managing your database schema yourself, you need to take care that your model corresponds to the reality of the database.
@tiago156 if you run into a different issue from the one above and can reproduce it, please post back here so we can investigate.
Even I have face the same issue
Same issue.... Is there a way to identify exactly which column is causing the issue ?
Also I'm getting file paths in the stack trace that probably belong to the maintainers like so
C:\projects\npgsql\src\Npgsql\NpgsqlDefaultDataReader.cs