question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Breaking change in 2.2: Column is null

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
avin-kavishcommented, May 11, 2019

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

0reactions
parvezdesignworkscommented, Mar 23, 2019

Even I have face the same issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Core: `SqlNullValueException: Data is ...
After a bit of troubleshooting, I found that the issue came from my integer columns being null. I simply changed my int column...
Read more >
Breaking changes included in EF Core 3.x
Complete list of breaking changes introduced in Entity Framework Core 3.x. ... properties except the primary key to nullable columns.
Read more >
Migrating to null safety
Migrate your package's code, preferably using the interactive migration tool. Statically analyze your package's code. Test to make sure your changes work. If ......
Read more >
How to set a date to NULL in Field Calculator
Solved: I'm using ArcPro 2.2.1 How do I set a date to Null instead of 1/1/1900 in the field calculator of a table...
Read more >
Migration Guide: SQL, Datasets and DataFrame
In Spark 3.1, when spark.sql.ansi.enabled is false, Spark always returns null if the sum of decimal type column overflows. In Spark 3.0 or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found