gpt4 book ai didi

c# - 不能将重复值插入到唯一索引中(但是...)

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:00 24 4
gpt4 key购买 nike

我正在尝试将产品添加到购物车,但它返回以下内容:

更新条目时发生错误。有关详细信息,请参阅内部异常。

Inner exception: System.Data.SqlServerCe.SqlCeException (0x80004005): A duplicate value cannot be inserted into a unique index. [ Table name = Cart,Constraint name = PK_Cart_0000000000000997 ] at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() at System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteNonQuery() at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2
identifierValues, List
1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

Stack trace at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Objects.ObjectContext.SaveChanges() at JTS.Security.Identity.Push.ToCart(Int32 userId, String partNumber, String productDescription, Int32 quantity, Decimal price, Decimal lineTotal, String orderId, DateTime dateTime, Boolean isBoxed) in c:\Users\Jase\Documents\Visual Studio 2012\WebSites\One\App_Code\JTS.cs:line 251

但问题是,我没有任何唯一索引。我什至尝试删除所有主键(只是为了看看这是否会改变任何东西)——但它没有。

代码:

public bool ToCart(int userId,
string partNumber,
string productDescription,
int quantity,
decimal price,
decimal lineTotal,
string orderId,
DateTime dateTime,
bool isBoxed)
{
bool addedToCart = false;

try
{
Cart cart = new Cart()
{
UserId = userId,
PartNumber = partNumber,
Description = productDescription,
Quantity = quantity,
Price = price,
LineTotal = lineTotal,
OrderId = orderId,
OrderDate = dateTime,
IsBoxed = isBoxed
};

database.AddToCarts(cart);
database.SaveChanges();

addedToCart = true;
}
catch (Exception exception)
{
addedToCart = false;
//Response(exception.Message);
addToCartExceptionDetails = exception.Message +
Environment.NewLine + Environment.NewLine + "Inner exception" +
exception.InnerException +
Environment.NewLine + Environment.NewLine + "Stack trace" +
exception.StackTrace;
}

return addedToCart;
}

此外,如果我创建一个新订单(这会创建一个新的 OrderID),它会向购物车添加一项。但是,它永远不会在现有 OrderId 下向购物车添加超过 1 件商品!

我做错了什么?为什么这不起作用?

表定义
Column Name   Data Type   Length   Allow Nulls     Unique    Primary Key
UserId int 4 Yes No No
OrderId nvarchar 1000 No No Yes

最佳答案

如果您有一个表,您在其中删除并重新添加了内容,则在插入 ID 等于现有 ID 的新行时可能会出现此错误你为什么不检查这个Link形成更多信息

关于c# - 不能将重复值插入到唯一索引中(但是...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886267/

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