I am trying to add a product to the Cart, but it returns the following:
An error occurred while updating the entries. See the inner exception for details.
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, Dictionary
2 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
The thing is though, I don't have any Unique indexes. And I even tried removing all primary keys (just to see if that would change anything) - which it didn't.
The code:
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;
}
Also, if I create a new Order (which creates a new OrderID), it WILL add one item to the cart. BUT, It will never add anymore than 1 item to the cart under an existing OrderId!
What am I doing wrong? Why isn't this working?
Table DefinitionColumn Name Data Type Length Allow Nulls Unique Primary Key
UserId int 4 Yes No No
OrderId nvarchar 1000 No No Yes