1

Mein aktuelles Projekt folgende "Unit of Work" Design pattern.I zwei Tisch haben like--Entity Framework Selbstreferenzierung erfasst Schleife in UOW Muster

enter image description here

wo MAINSCHMSTID Fremdschlüssel im Detail Tabelle ist .when ich bin Aktualisierung table.i bin immer Daten vor Savechanges wie this--

enter image description here

enter image description here

Mein Update-Methode Code:

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

Hinweis, dass mit MAINSCHMSTID 1, ich bin immer es Objekt ist (MAINTENANCESCHMASTER) also.In MAINTENANCESCHMASTER gibt es ein MAINTENANCESCHDETAIL Objekt und diese Selbst Referenzierung Schleife wird on.Finally dies tut nicht speichert die data.It eine Java-Script-Warnung geben: enter image description here

In Console -

enter image description here I Entity framework self referencing loop detected gefolgt. Aber kein Ergebnis bekommen. Kann jemand vorschlagen, was zu tun ist?

EDIT:

In AngularJS-Controller meine Methode bearbeiten -

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); 
       }); 
      } 
     } 

Fehler in der Konsole - 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

In 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

Was wollen Sie ** den Code zu tun? Was macht es eigentlich **? Gibt es Ausnahmen? – mjwills

+0

Ich möchte, dass es keinen Fehler zeigt, wird die Master-und Detail-Tabelle speichern.Es wirft keine Ausnahme.Nur zeigt eine Java-Skript-Warnung des Fehlers. –

+0

Ich bearbeite meine Frage. Sie können den Fehler sehen. –

Antwort

0

Beim Versuch, eine serialisierte Version des Modells an das Frontend zurückzugeben, wird ein Fehler ausgegeben. Sie können es umgehen, indem Sie die ReferenceLoopHandling-Eigenschaft in Ihrem Serializer festlegen:

+1

Wie Sie in der Frage lesen können, hat OP dies bereits versucht. –

Verwandte Themen