У меня есть приложение UWP Applicatoin, использующее EF Core 2.2.6 вместе с SQLite. Я пытаюсь сохранить сущность с именем «RecurringPayment», в которой есть ограничение, что значение StartDate должно быть установлено. Я добавляю и сохраняю его с этим кодом:
cont.RecurringPayments.Add(payment1.RecurringPayment);
cont.SaveChanges();
Объект payment1.RecurringPayment
имеет эти значения прямо перед вызовом Add
:

Но когда вызывается SaveChanges
, выдается это исключение:
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- Microsoft.Data.Sqlite.SqliteException : SQLite Error 19: 'NOT NULL constraint failed: RecurringPayments.StartDate'.
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList`1 entries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
at MoneyFox.Application.Tests.Payments.Commands.CreatePayment.CreatePaymentCommandTests.CreatePaymentWithRecurring_PaymentSaved() in C:\Users\padruttn\Documents\git\MoneyFox.Windows\Src\MoneyFox.Application.Tests\Payments\Commands\CreatePayment\CreatePaymentCommandTests.cs:line 55
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Таблица имеет такой SQL-код создания:
CREATE TABLE "RecurringPayments" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_RecurringPayments" PRIMARY KEY AUTOINCREMENT,
"Amount" REAL NOT NULL,
"CategoryId" INTEGER NULL,
"ChargedAccountId" INTEGER NOT NULL,
"EndDate" TEXT NULL,
"IsEndless" INTEGER NOT NULL,
"Note" TEXT NULL,
"Recurrence" INTEGER NOT NULL,
"StartDate" TEXT NOT NULL,
"TargetAccountId" INTEGER NULL,
"Type" INTEGER NOT NULL, "CreationTime" TEXT NOT NULL DEFAULT '0001-01-01 00:00:00', "ModificationDate" TEXT NOT NULL DEFAULT '0001-01-01 00:00:00',
CONSTRAINT "FK_RecurringPayments_Categories_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "Categories" ("Id") ON DELETE SET NULL,
CONSTRAINT "FK_RecurringPayments_Accounts_ChargedAccountId" FOREIGN KEY ("ChargedAccountId") REFERENCES "Accounts" ("Id") ON DELETE CASCADE,
CONSTRAINT "FK_RecurringPayments_Accounts_TargetAccountId" FOREIGN KEY ("TargetAccountId") REFERENCES "Accounts" ("Id") ON DELETE SET NULL
)
Я понимаю, что это исключение неверно или почемуEfCore считает, что значение StartDate пусто, когда оно явно заполнено при вызове Add?