I’ve been struggling with this problem for some time and can’t solve it. I read a few posts and tried several different solutions, but nothing worked. Now I feel like I am stopped and really need help.
I am using EF 5+ with DB and the first edmx file. I have 3 different tables in my database: 1. Calculation 2. Cost 3. Shift
Settlement has a Cost and Shift collection (with a link) associated with the Association in my edmx file.
I need to insert a new setting into my db with reference to the existing Cost and Shift collections. The shifts and costs included in my settlement object, which I am trying to insert, contain all the data associated with it, and none of them changes in any way (just as I got from db). Here in my method of inserting an object into my db.
public bool CreateSettlement(Settlement settlement) { bool _success; var _context = new EtaxiEnteties();// ObjectFactory.Get<IETaxiEntitiesContext>(); try { var _newSettlement = new Settlement { CreateDate = settlement.CreateDate, Driver = settlement.Driver, DriverID = settlement.DriverID, Car = settlement.Car, CarID = settlement.CarID, DocPath = settlement.DocPath }; foreach (var _shift in settlement.Shifts) { //var _sh = _context.Shifts.Find(_shift.ShiftID); //_context.Entry(_sh).CurrentValues.SetValues(_shift); _newSettlement.Shifts.Add(_shift); } foreach (var _cost in settlement.Costs) { ////var _sh = _context.Costs.Find(_cost.CostID); ////_context.Entry(_sh).CurrentValues.SetValues(_cost); _newSettlement.Costs.Add(_cost); } _context.Settlements.Add(_newSettlement); _success = _context.SaveChanges() > 0; } catch (Exception ex) { throw new Exception(ex.Message); } return _success; }
Any help on this would be greatly appreciated.
This is how I add Cost and Shift to my collection:
I create a settlement on the page:
_settlement = new Settlement { CreateDate = DateTime.Now, Driver = _driver, DriverID = _driver.DriverID, Car = _car, CarID = _car.CarID, DocPath = _path };
then when I create a PDF file with selected lines from 2 separated grid views:
foreach (GridDataItem _selectedRow in gwShifts.MasterTableView.Items) { if (_selectedRow.Selected) { var _shift = _diaryRepository.GetShiftByID((int) _selectedRow.GetDataKeyValue("ShiftID")).FirstOrDefault(); if (_shift != null) { _settlement.Shifts.Add(_shift); _settlementData.Shifts.Add(_shift); _settlementData.SplitPercentace = GetTemplateValue(_selectedRow, "lblSplit"); _settlementData.SettlementAmount = GetTemplateValue(_selectedRow, "lblSettlementAmount"); if (_settlementData.Shifts != null) { _tableShifts.AddCell( new PdfPCell( new Phrase( _settlementData.Shifts.FirstOrDefault().ShiftDate.ToShortDateString(), _bodyFont)) {Border = 0}); _tableShifts.AddCell( new PdfPCell( new Phrase( string.Format("{0:c}", _settlementData.Shifts.FirstOrDefault().GrossAmount), _bodyFont)) {Border = 0}); _tableShifts.AddCell( new PdfPCell( new Phrase( string.Format("{0:c}", _settlementData.Shifts.FirstOrDefault().MoneyAmount), _bodyFont)) {Border = 0}); _tableShifts.AddCell(new PdfPCell(new Phrase(_settlementData.SplitPercentace, _bodyFont)) {Border = 0}); _tableShifts.AddCell( new PdfPCell(new Phrase(_settlementData.SettlementAmount, _boldTableFont)) {Border = 0}); _totalAmount.AddRange(new[] { Convert.ToInt32( _settlementData.SettlementAmount.Replace(".", ""). Replace(",", "").Replace("kr", "")) }); _settlementData.Shifts.Remove(_shift); } } } } var _summaryCell = new PdfPCell(new Phrase("Upphæð: " + string.Format("{0:c}", _totalAmount.Sum()), _boldTableFont)) { Border = 0, Colspan = 5, HorizontalAlignment = Element.ALIGN_RIGHT, Padding = 5, BorderWidthTop = 1 }; _tableShifts.AddCell(_summaryCell); if (_totalAmount.Count != 0) _totalAmount.Clear(); }
there you see how I add Shift to this calculation object:
var _shift = _diaryRepository.GetShiftByID((int) _selectedRow.GetDataKeyValue("ShiftID")).FirstOrDefault(); if (_shift != null) { _settlement.Shifts.Add(_shift);
then I send this to the repository (see method above)
if(_driverRepository.CreateSettlement(_settlement)) { SetMessage("Uppgjör hefur verið skapað og sent bílstjóra ef e-póstur er skráður á viðkomandi bílstjóra.", "Uppgjör skapað"); pnlSettlement.Visible = false; pnlDocCreation.Visible = false; pnlResult.Visible = false; }
I also tried just adding the param parameter directly to the context, but got a similar error.