简体   繁体   中英

update database in asp.net

i'm biulding a web application and when the users log in and trying to buy somthing from web for another time this error apears

SqlException: Cannot insert explicit value for identity column in table 'orderFactors' when IDENTITY_INSERT is set to OFF.

    public IActionResult AddToCart(int itemId)
    {
        var product = _context.Products.Include(p => p.Item).SingleOrDefault(p => p.ItemId == itemId);
        if (product != null)
        {
            int userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier).ToString());
            var order = _context.orderFactors.FirstOrDefault(o => o.UserId == userId && !o.IsFinally);
            if (order != null) 
            {
                var orderDetail = _context.orderDetails.FirstOrDefault(o => o.OrderId == order.OrderId && o.ProductId == product.Id);
                if (orderDetail != null) 
                {
                    orderDetail.Count += 1;
                }
                else
                {
                    _context.orderFactors.Add(order);
                    _context.orderDetails.Add(new OrderDetail()
                    {
                        OrderId = order.OrderId,
                        ProductId = product.Id,
                        price = product.Item.Price,
                        Count = 1
                    });
                }
            }
            else
            {
                order = new OrderFactor()
                {
                    IsFinally = false,
                    CreateDate= DateTime.Now,
                    UserId= userId

                };
                _context.orderFactors.Add(order);
                _context.SaveChanges();
                _context.orderDetails.Add(new OrderDetail()
                {
                    OrderId = order.OrderId,
                    ProductId = product.Id,
                    price = product.Item.Price,
                    Count = 1
                });
            }
            _context.SaveChanges();
        }
        return RedirectToAction("ShowCart");
    }

You are reading from the DB here

 var order = _context.orderFactors.FirstOrDefault(o => o.UserId == userId && !o.IsFinally);

But then you try to add the value again here if orderDetail is not null

_context.orderFactors.Add(order);

The order object that you have read will have an Id and therefore the DB thinks you are trying to add the same Id to that table.

Without seeing exactly how you are inserting/updating this it is tough to answer. That said, the error message is fairly explanatory (for once.). You are trying to enter a value into an identity/primary key field. That field is (most likely) auto generated/incremented by your database to guarantee uniqueness.

Somewhere in your saveChanges funtion you are either inserting (most likely) an ID or you are trying to update an ID. If those are marked identity, they cannot be altered unless you set IDENTITY_INSERT ON .

Double check your saveChanges model and the query behind it to make sure you're not trying to update/change/insert your primary key value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM