I'm having trouble when I want to insert a new registry in the database. My application throws that exception:
System.InvalidOperationException: Can not insert entity with the same key if key is not database generated.
The code is:
Code: Select all
stock = new Stock(); stock.MasterProduct = _dataContext.MasterProducts.Single(mp => mp.MpReference == reference); stock.StBatch = batch; stock.Transportunit = _dataContext.Transportunits.Single(t => t.Barcodelabel == tuBarcode); stock.StDate = (bbd.HasValue) ? bbd.Value : (packedDate.HasValue) ? packedDate.Value : bbd.GetValueOrDefault(); stock.StBbdd = bbd; stock.StPackagingd = packedDate; stock.StQuantity = quantity; stock.StockState = _dataContext.StockStates.Single(s => s.SsId == (int)StockStateEnum.Available); stock.StOrigin = origin; stock.Quarentined = isQuarentined ? 1 : 0; stock.Warehouse = _dataContext.Warehouses.Single(w => w.Warehouseid == warehouseId); stock.StUnitservice = unitsService; stock.Internalorderid = internalOrderId; stock.Orderlineid = orderLineId; _dataContext.Stocks.InsertOnSubmit(stock); _dataContext.SubmitChanges();
Thanks in advance.