现有的修改条目重新插入EF

时间:2017-11-08 21:02:17

标签: entity-framework-6

我在多对多ActivityHistory

中有两个实体

我正在尝试将现有修改后的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();

}

0 个答案:

没有答案