Skip to content

EF Core 7 non-nullable string property with empty string throws not-null constraint exception #32304

@khteh

Description

@khteh

Just a simple entity class with non-nullable string property results in the following exception when the value is empty and the object is persisted to the DB. Is there any change in EF Core 7.0 recently that causes this?

Include stack traces

An exception occurred in the database while saving changes for context type '"MyAppDomainModel.Infrastructure.Data.MyAppDbContext"'."
""Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
 ---> Npgsql.PostgresException (0x80004005): 23502: null value in column \"SourcingProduct_Title\" of relation \"OrderItems\" violates not-null constraint

DETAIL: Failing row contains (18600dfd-b28f-47ed-a185-33207ca26ec5, cb0cb5a8-957f-4cc0-9bf1-b95d917575ee, 5, 2.8, , null, null, null, null, null, null, null, 3c465a4f-ea5d-417f-bf3f-5e974c0c0ef7, 2023-11-14 09:59:42.36258+00, 2023-11-14 09:59:42.362581+00).
   at Npgsql.Internal.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|234_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
  Exception data:
    Severity: ERROR
    SqlState: 23502
    MessageText: null value in column \"SourcingProduct_Title\" of relation \"OrderItems\" violates not-null constraint
    Detail: Failing row contains (18600dfd-b28f-47ed-a185-33207ca26ec5, cb0cb5a8-957f-4cc0-9bf1-b95d917575ee, 5, 2.8, , null, null, null, null, null, null, null, 3c465a4f-ea5d-417f-bf3f-5e974c0c0ef7, 2023-11-14 09:59:42.36258+00, 2023-11-14 09:59:42.362581+00).
    SchemaName: public
    TableName: OrderItems
    ColumnName: SourcingProduct_Title
    File: execMain.c
    Line: 1975
    Routine: ExecConstraints
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)"

Include verbose output

Please include --verbose output when filing bugs about the dotnet ef or Package Manager Console tools.

Use triple-tick fences for tool output. For example:

C:\Stuff\AllTogetherNow\FiveOh>dotnet ef dbcontext list --verbose
Using project 'C:\Stuff\AllTogetherNow\FiveOh\FiveOh.csproj'.
...
Finding DbContext classes in the project...
Found DbContext 'BlogContext'.
BlogContext

Include provider and version information

EF Core version: 7.0.13
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) PostgreSQL
Target framework: (e.g. .NET 6.0) .Net 7.0
Operating system: Ubuntu 23.10
IDE: (e.g. Visual Studio 2022 17.4) VSCode 1.84.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions