为什么循环第二次时报这个错?
public ActionResult Conversion(int[] EG_IDArr, int[] CountArr, int PointSum, int MC_ID) { using (TransactionScope ts = new TransactionScope()) { MemCard mc = db.MemCards.Find(MC_ID); ExchangLog el = new ExchangLog(); el.S_ID = 1;//默认为自己店铺ID el.U_ID = 1;//默认为当前用户ID el.MC_ID = mc.MC_ID; el.MC_Name = mc.MC_Name; el.MC_CardID = mc.MC_CardID; for (int i = 0; i < EG_IDArr.Length; i++) { //修改 礼品(ExchangGifts)(已兑换的数量 EG_ExchangNum) ExchangGift eg = db.ExchangGifts.Find(EG_IDArr[i]); eg.EG_ExchangNum += CountArr[i]; db.Entry(eg).State = EntityState.Modified; db.SaveChanges(); //添加 兑换记录(ExchangLogs) el.EG_ID = eg.EG_ID; el.EG_GiftCode = eg.EG_GiftCode; el.EG_GiftName = eg.EG_GiftName; el.EL_Number = CountArr[i]; el.EL_Point = eg.EG_Point * CountArr[i]; el.EL_CreateTime = DateTime.Now; db.ExchangLogs.Add(el); db.SaveChanges(); //return Content("<script>alert('礼品兑换成功!');history.go(-1);</script>"); } //修改 会员(MemCards)(当前积分 MC_Point) mc.MC_Point -= PointSum; db.Entry(mc).State = EntityState.Modified; db.SaveChanges(); ts.Complete(); return Content("礼品兑换成功"); } }
数组的数据只有一条就不会报错
不是写的很清楚.要保存的实体数据错误了.不符合验证特性的配置
实体数据没有错,EG_IDArr数组中只有一条就操作成功,有多条数据时,在循环第二次时就会报上面这个错
@长毛象: 别闹..代码说你错了.你就是错了.在异常详细信息里能找到具体是哪个字段不匹配.看下实体的值和验证特性就知道了
@吴瑞祥: 谢了,今天有运行了一下又没有报错