我在多对多Activity
和History
我正在尝试将现有修改后的History
添加到新Actvity
,但一旦SaveChnages
被调用History
,记录将被视为新记录并重新插入。
我不确定我做错了什么
using (HistoryRepo posRepo = new HistoryRepo())
{
EF.Stock.History pos = posRepo.Get(p.Symbol);
pos.SaleDate = DateTime.UtcNow;
pos.OrderGL = p.TotalGainLossDollar;
pos.OrderGLPercent = p.TotalGainLossPercentage;
pos.CANSLIMRecordClosed = Mapper.Map<EF.Stock.CANSLIMRecord>(Data.Where(x => x.Symbol == p.Symbol).Select(x => { x.Type = "closed"; return x; }).FirstOrDefault());
histories.Add(posRepo.Update(pos)); //wrapper for _context.Entry(item).State = EntityState.Modified
}
稍后再说
using (ActivityRepo repo = new ActivityRepo())
{
Activity act = new Activity();
act.Histories = histories; //same collection from code snippet above
act.ElapsedTime = stopWatch.Elapsed.ToString("h'h 'm'm 's's'");
act = repo.Insert(act); //wrapper for _context.Entry(item).State = EntityState.Added
repo.Save(); //wrapper for _context.SaveChanges();
}