Breaking change in 2.2: Column is null
See original GitHub issueI 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.
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (4 by maintainers)
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
Even I have face the same issue