我有一个使用EF Core 2.2.6和SQLite的UWP应用程序。我尝试保存一个名为“ 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在调用Add时清楚地填充StartDate值为空?
答案 0 :(得分:0)
Okey问题出在我的实体模型中。 StartDate值定义如下:
public DateTime StartDate { get; }
总是通过构造函数添加的值。事实证明,EF Core至少需要一个私人安装员。所以我不得不调整它看起来像这样:
public DateTime StartDate { get; private set; }