Home > Software engineering >  SQLite Error 19: 'NOT NULL constraint failed' - Are strings nullable by default in C# 8?
SQLite Error 19: 'NOT NULL constraint failed' - Are strings nullable by default in C# 8?

Time:04-12

I am having this issue after migrating my project from .NET 5 to .NET 6. I am using the latest version of SQLite as the database.

It seems to be assuming that string is no longer nullable by default instead of nullable. Whenever Entity Framework tries to write to a field that is not a primary or foreign key I get the error SQLite Error 19: 'NOT NULL constraint failed. In .NET 5 I was not getting this error.

I also noticed that VS 2022 Intellisense has a green indicator line under each of the property names that cause this error. It states Non-nullable property must contain a non null-value when exiting constructor. Consider declaring the property as nullable.

When I add the nullable operator, ?, to the property types causing the error, the green indicator line and the SQLite Error 19: 'NOT NULL constraint failed' go away.

Example of code the causes the error:

    [Table("ApplicationUser")]
    public class ApplicaitonUser : IdentityUser
    {
        // Inherited properties https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-6.0
        [Key]
        public new int Id { get; set; } // every other property causes the SQLite 19 error
        
        [PersonalData]
        public string FirstName { get; set; }

        [PersonalData]
        public string MiddleName { get; set; }
        
        [PersonalData]
        public string LastName { get; set; }

        [PersonalData]
        public string PreferredName { get; set; }

        // Address Table Foreign Key relationship navigation property 
        [PersonalData]
        ICollection<UsAddress> UsAddresses { get; set; }


    }

CodePudding user response:

Microsoft introduced in C# 8 "nullable aware context".

In a nullable aware context:

  • A variable of a reference type T must be initialized with non-null, and may never be assigned a value that may be null.
  • A variable of a reference type T? may be initialized with null or assigned null, but is required to be checked against null before de-referencing.
  • A variable m of type T? is considered to be non-null when you apply the null-forgiving operator, as in m!.

The distinctions between a non-nullable reference type T and a nullable reference type T? are enforced by the compiler's interpretation of the preceding rules. A variable of type T and a variable of type T? are represented by the same .NET type.

Nullable reference types

Examples:

string notNull = "Hello";
string? nullable = default;
notNull = nullable!; // null forgiveness

This feature can be turned on and off using the <nullable>enable</nullable or disable attribute in the Project file. Since the feature is disable by default, the attribute can also be removed to disable it.

Nullable contexts

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

Additional information:

Learn techniques to resolve nullable warnings

Update a codebase with nullable reference types to improve null diagnostic warnings

  • Related