gpt4 book ai didi

c# - Web API 核心 : "FOREIGN KEY constraint failed" error during POST request SQLite

转载 作者:行者123 更新时间:2023-12-02 02:14:16 25 4
gpt4 key购买 nike

我正在使用 .Net Web API 核心和 Entity Framework Core 为产品商店构建 API。我的产品实体如下所示

public class Product
{
[Key]
public long ID { get; set; }

[Required]
[MaxLength(80)]
public string Name { get; set; }
............
}

我的订单实体是

public class Order
{
[Key]
public long ID { get; set; }
public List<OrderItem> OrderItems { get; set; }
..........
}

我有 OrderItem 实体,它是在用户将产品添加到卡片时创建的,但尚未创建订单时:

 public class OrderItem
{
[Key]
public long ID { get; set; }

public string UID { get; set; }

public long OrderID { get; set; }
public Order Order { get; set; }

[Required]
public long ProductID { get; set; }
public Product Product { get; set; }

public int Quantity { get; set; }
}

它表明用户已将带有 ProductID(FK) 和此数量的产品添加到他的卡中。但尚未创建任何订单(因此 OrderID FK 尚未为空)。

当我发出将带有 ProductID、Quantity 和 UID 的 OrderItem 添加到数据库的发布请求时,如下所示:

[HttpPost]
public IActionResult Post([FromBody]JObject newItem)
{
OrderItem oItem = newItem.ToObject<OrderItem>();

context.OrderItems.Add(oItem);
context.SaveChanges();
return StatusCode(200);
}

在我调用 SaveChanges() 的行上抛出异常,并显示文本“FOREIGN KEY Constraint failed”。我正在添加具有有效 ProductID 的对象,JObject 已正常转换。我尝试使用 SQLite 浏览器添加相同的对象手动,它没有给出任何错误。关于 OrderID,我还没有给它一个值,外键可以为空,不是吗?

请帮助我,我不明白是什么原因导致了这个问题。附:我在 Mac 操作系统上使用 Visual Studio 2017。

UPD。

这是输出(我安慰 JObject 以查看是否正确):

    Application started. Press Ctrl+C to shut down.
Jobject received is:{
"ProductID": 2,
"UID": "6c523a76-dd6e-7782-2bc9-9b8baba36e5d",
"Quantity": "1"
}
OrderItem casted is: ProductID=2; UID=6c523a76-dd6e-7782-2bc9-9b8baba36e5d
fail: Microsoft.EntityFrameworkCore.DbContext[1]
An exception occurred in the database while saving changes.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.Sqlite.SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.
at Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc, Sqlite3Handle db)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
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)
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.Sqlite.SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.
at Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc, Sqlite3Handle db)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
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)

最佳答案

我尝试将“long OrderID”更改为“long? OrderID”

关于c# - Web API 核心 : "FOREIGN KEY constraint failed" error during POST request SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com