Open Closed

An entity object cannot be referenced by multiple instances #1231


0
gunpal5 created

I am getting this issue If I assign "User" Entity to my CustomEntities. It seems to be that Abp Framework is using different dbcontext than the actual Application

Is there any way to assign a User Entity to my application Entities?

var user = await userManager.GetUserByIdAsync(input.UserId);
            if (user == null)
                throw new UserFriendlyException("Invalid User Id");          

            VideoProject vp = new VideoProject();
            vp.Buyer = user;
 await videoProjectRepository.InsertAsync(vp);

            await CurrentUnitOfWork.SaveChangesAsync();
[InvalidOperationException: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.]
   System.Data.Entity.Core.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity) +5065205
   System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName) +67
   System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddEntityToObjectStateManager(IEntityWrapper wrappedEntity, Boolean doAttach) +44
   System.Data.Entity.Core.Objects.DataClasses.EntityReference.AddEntityToObjectStateManager(IEntityWrapper wrappedEntity, Boolean doAttach) +22
   System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach) +28
   System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach) +188
   System.Data.Entity.Core.Objects.DataClasses.EntityReference`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach) +113
   System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach) +172
   System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity) +396
   System.Data.Entity.Internal.Linq.<>c__DisplayClassd.<Add>b__c() +58
   System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +160
   System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) +110
   System.Data.Entity.DbSet`1.Add(TEntity entity) +83
   Abp.EntityFramework.Repositories.EfRepositoryBase`3.InsertAsync(TEntity entity) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Repositories\EfRepositoryBaseOfTEntityAndTPrimaryKey.cs:80
   Castle.Proxies.EfRepositoryBase`3Proxy_19.InsertAsync_callback(VideoProject entity) +4
   Castle.Proxies.Invocations.EfRepositoryBase`3_InsertAsync_23.InvokeMethodOnTarget() +69
   Castle.DynamicProxy.AbstractInvocation.Proceed() +86
   Abp.Domain.Uow.UnitOfWorkInterceptor.PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:62
   Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:41
   Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:35
   Castle.DynamicProxy.AbstractInvocation.Proceed() +114
   Castle.Proxies.EfRepositoryBase`3Proxy_19.InsertAsync(VideoProject entity) +141
   K2Innovators.BizMov.VideoProjects.<CreateNewProjectForBuyer>d__5.MoveNext() in D:\OnlineTFS\K2Innovators\BizMov\K2Innovators.BizMov.Application\VideoProjects\VideoProjectAppService.cs:61
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Abp.Threading.&lt;AwaitTaskWithPostActionAndFinallyAndGetResult&gt;d__5`1.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:120
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Abp.Threading.&lt;AwaitTaskWithFinallyAndGetResult&gt;d__3`1.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:91
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   K2Innovators.BizMov.Web.Areas.Mpa.Controllers.&lt;GetCompletedView&gt;d__12.MoveNext() in D:\OnlineTFS\K2Innovators\BizMov\K2Innovators.BizMov.Web\Areas\Mpa\Controllers\VideoPackageListController.cs:143
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Abp.Threading.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:120
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   K2Innovators.BizMov.Web.Areas.Mpa.Controllers.<PaymentComplete>d__11.MoveNext() in D:\OnlineTFS\K2Innovators\BizMov\K2Innovators.BizMov.Web\Areas\Mpa\Controllers\VideoPackageListController.cs:106
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +42
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +73
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +37
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +69
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +230
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +42
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +124
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +27
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +32
   Castle.Proxies.VideoPackageListControllerProxy.EndExecuteCore_callback(IAsyncResult asyncResult) +4
   Castle.Proxies.Invocations.Controller_EndExecuteCore.InvokeMethodOnTarget() +55
   Castle.DynamicProxy.AbstractInvocation.Proceed() +86
   Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:29
   Castle.DynamicProxy.AbstractInvocation.Proceed() +114
   Castle.Proxies.VideoPackageListControllerProxy.EndExecuteCore(IAsyncResult asyncResult) +142
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +26
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +40
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   Castle.Proxies.VideoPackageListControllerProxy.EndExecute_callback(IAsyncResult asyncResult) +26
   Castle.Proxies.Invocations.Controller_EndExecute.InvokeMethodOnTarget() +55
   Castle.DynamicProxy.AbstractInvocation.Proceed() +86
   Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:29
   Castle.DynamicProxy.AbstractInvocation.Proceed() +114
   Castle.Proxies.VideoPackageListControllerProxy.EndExecute(IAsyncResult asyncResult) +142
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +24
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +27
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +29
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +23
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129

9 Answer(s)
  • 0
    hikalkan created
    Support Team

    Hi,

    Where have you written that code? In application service method or MVC controller action? If MVC controller action, then it should be marked as UnitOfWork and should be virtual. Thus, both db operations will share same UOW, and thus, same dbcontext.

  • 0
    gunpal5 created

    Hello,

    I have used UserId for now to avoid this issue.

    This code was written in application service, which was called by a MVC controller. The controller's action method already had UoW attribute and virtual keyword. I will try to add virtual keyword to application service method too, just to try if that work or not.

    Thanks, Gunpal Jain

  • 0
    hikalkan created
    Support Team

    Thanks, we will also try such a case.

  • 0
    hikalkan created
    Support Team

    We found that this is a bug (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1083">https://github.com/aspnetboilerplate/as ... ssues/1083</a>). Will fix and release a nuget version in a very short time.

  • 0
    gunpal5 created

    Hello,

    Thank you very much.

    I'm really enjoying working on this framework. This is an excellent work.

    I would like to contribute on this project when I get time. I can help to write few scaffolders for application services, and angular views with typescript codes. I will do that after I finish with my current projects.

    Regards, Gunpal Jain

  • 0
    hikalkan created
    Support Team

    Thank you very much. Looking forward for your contributions :)

  • 0
    dribnuz created

    Hi hikalkan ,

    I'm having this issue problem. My project is running on version v0.9.1 and need to update to version v0.9.3 to resolved it: #1083.

    But latest current version 1.3.1 and i can't runt the command : get-project -all | get-package | ?{ $_.Id -like 'Abp*' } | update-package

    So i try to run command as below ; get-project -all | update-package Abp -Version 0.9.3

    It seem just update but just one file/dll and the problem still occur. Can you advise on this? or how to update package all Abp to 0.9.3?

    Thanks

  • 0
    dribnuz created

    Hi hikalkan ,

    Just try my luck by running command as below ; get-project -all | get-package | ?{ $_.Id -like 'Abp*' } | update-package -Version 0.9.3

    It update with some error message but the problem resolved.

    Please advise this is proper command or i assume this as solution.

    Thanks

  • 0
    hikalkan created
    Support Team

    I hadn't know this type of upgrading. But it's fine if it worked :)