1

我下面的“工作单位”设计当前项目pattern.I有两个表like--实体框架自参照环

enter image description here

其中MAINSCHMSTID是详细表的外键。当我更新table.i我的SaveChanges之前获取数据,如this--

enter image description here

enter image description here

我的更新方法代码:

public void Update(TEntity entity) 
    { 
     db.Entry<TEntity>(entity).State = EntityState.Modified;      
    } 

通知与MAINSCHMSTID 1,我得到它的对象(MAINTENANCESCHMASTER)also.In MAINTENANCESCHMASTER有一个MAINTENANCESCHDETAIL对象,这种自我参照循环会on.Finally这样做不保存data.It是给Java脚本警报: enter image description here

在控制台 -

enter image description here 我跟着Entity framework self referencing loop detected。但没有结果。任何人都可以建议做什么?

编辑:

在AngularJs控制器我的编辑方法 -

function save() { 

      scope.submitted = true; 
      if (scope.maintenanceForm.$valid) { 

       var result = null; 
       var operation = null; 
       if (scope.isEdit) { 

        operation = "edit"; 
        scope.maintenance.MAINTENANCESCHDETAILS = scope.maintenance.maintenanceItemList; 
        result = maintenanceService.edit(scope.maintenance); 
       } 
       else { 

        operation = "save"; 
        scope.maintenance.MAINTENANCESCHDETAILS = scope.maintenance.maintenanceItemList; 
        result = maintenanceService.save(scope.maintenance); 
       } 

       result.then(function (data) { 

        if (data == "") { 

         alert("Maintenance Schedule Item Already Exist"); 
         initialize(); 
        } 
        else { 

         alert("saved successfully"); 

         if (!scope.isEdit) { 
          scope.maintenance.MAINSCHMSTID = data.ID; 
         } 
         convertSvc.updateCollection(scope.maintenanceViews, scope.maintenance, operation, "MAINSCHMSTID"); 
         initialize(); 
        } 

       }, function (e) { 
        alert(e); 
       }); 
      } 
     } 

错误控制台 - enter image description here

Server Error in '/' Application.

A circular reference was detected while serializing an object of type 'VMS.DATA.MAINTENANCESCHMASTER'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'VMS.DATA.MAINTENANCESCHMASTER'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: A circular reference was detected while serializing an object of type 'VMS.DATA.MAINTENANCESCHMASTER'.]
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +2533 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +371 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +778
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +2751 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +371 System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +186
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +2699 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +371 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +778
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +2751 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +371 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) +57 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat) +83
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj) +44 System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) +817
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList 1 filters, ActionResult actionResult) +106
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9644037 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

在Global.asax.cs--

protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 


      HttpConfiguration config = GlobalConfiguration.Configuration; 

      config.Formatters.JsonFormatter 
         .SerializerSettings 
         .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
     } 
+0

您希望代码执行哪些操作?它实际上**做了什么?它是否会抛出任何异常? – mjwills

+0

我希望它不会显示任何错误,将保存master和detail table.it不会引发任何异常。只显示一个java脚本提示错误。 –

+0

我编辑我的问题,你可以看到错误。 –

回答

0

尝试将模型的序列化版本返回到前端时出现错误。您可以通过在您的序列化程序中设置ReferenceLoopHandling属性来避开它:

var list = JsonConvert.SerializeObject(model, 
    Formatting.None, 
    new JsonSerializerSettings() { 
     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore 
}); 

return Content(list, "application/json"); 
+1

正如你可以在问题OP中已经试过这个。 –