db.Configuration.ProxyCreationEnabled无法正常工作[循环错误]

时间:2018-08-21 09:21:42

标签: c# .net asp.net-mvc entity-framework-6

我有一个简单的方法,可以对表进行一些简单的操作:

[HttpPost]
        public ActionResult SaveCoachingPlan(factCoachingPlan cp) {

        db.Configuration.ProxyCreationEnabled = false;

        try
        {

            factSubscription sub = db.factSubscriptions.Where(x => x.idSubscription == cp.idSubscription).FirstOrDefault();

            factCoachingPlan cpLast = (from s in db.factSubscriptions
                                           join c in db.factCoachingPlans on s.idSubscription equals c.idSubscription
                                           where c.idSubscription == cp.idSubscription && c.isActive == true
                                           select c).FirstOrDefault();
            if (sub != null)
            {
                if (cpLast != null)
                {
                    cpLast.isActive = false;
                    cpLast.DateEnd = DateTime.Now;
                    cpLast.intDateEnd = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));

                    db.SaveChanges();
                }

                factCoachingPlan cpNew = new factCoachingPlan();

                cpNew.idSubscription = sub.idSubscription;
                cpNew.isActive = true;
                cpNew.Workout = cp.Workout;
                cpNew.Diet = cp.Diet;
                cpNew.DateStart = DateTime.Now;
                cpNew.intDateStart = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));

                cpNew.DateEnd = sub.DateEnd;
                cpNew.intDateEnd = sub.intDateEnd;
                db.factCoachingPlans.Add(cpNew);

                db.SaveChanges();

                var res = new jsonResponse()
                {
                    rcode = "OK000",
                    message = "Coaching Plan modificato",
                    jsonData = cpNew
                };

                return Json(res);
            }
            else
            {
                var res = new jsonResponse()
                {
                    rcode = "ERR001",
                    jsonData = ""
                };
                return Json(res);
            }
        }

实际上,它只是将新记录添加到表中并返回它...停止

问题是,当我通过Ajax调用调用此方法时,出现此错误:

序列化“ MHSOnlineCoachingMVC.Models.factSubscription”类型的对象时检测到循环引用。

我试图通过在执行的顶部添加:db.Configuration.ProxyCreationEnabled = false;来解决此问题,但这不起作用...

这很奇怪,因为该命令修复了许多其他类似的参考循环错误...

我也尝试在全局级别实现IgnoreReference,但是没有办法...我仍然遇到该错误...

感谢支持

0 个答案:

没有答案