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中已經試過這個。 –