Detached entity errors on AttachCopy()

5 posts, 0 answers
  1. John
    John avatar
    6 posts
    Member since:
    Mar 2009

    Posted 03 Jan 2014 Link to this post


    I am using the generated Repositories from the ORM.  I have a Company and a User.  A Company has a User. 
    If I create a new Company, retrieve an existing user from the database (GetBy, detached entitiy), then assign the user to the Company and try to create add the new Company, I receive "Object references between two different object scopes are not allowed" in the Repository AddNew on the dataContext.AttachCopy() method.

    This is confusing because I thought detached entities were how to handle this.  The Repositories all provide detached entities, and attach entities when required, so is this not the correct way to handle this?  Why does the User object still have a context if it is detached?



  2. Boris Georgiev
    Boris Georgiev avatar
    190 posts

    Posted 08 Jan 2014 Link to this post

    Hi John,

    I assume that you are using Web API templates generated by Add OpenAccess Service Wizzard. I was unable to reproduce the issue on my side with the default generated repositories. I had to change the AddNew() method, so instead AttachCopy() method to use the Add() method to reproduce the same error. 

    Did you make any changes in the repositories methods?
    Do you call any other methods or do you perform any actions on the entities?

    Answering these questions and if you can share more information about your scenario or if you can provide a small sample demonstrating the problem will help us greatly to understand the issue and help you in the best possible way. 

    The detached object has information about the context from which it is detached for performance optimization.

    I am looking forward to hearing from you.

    Boris Georgiev
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  3. DevCraft banner
  4. Xaver
    Xaver avatar
    6 posts
    Member since:
    Jan 2015

    Posted 15 Jul Link to this post

    Hi Boris,

    i have similar problems. If i try to add something in a many-to-many collection of a detached entity i get the same Error as John.

    When i try to remove something in this collection, it works but its not saving after attaching it again (but context.HasChanges is true).

    Did i understand something wrong?


    static void Main(string[] args)
        UserGroup detachedGroup;
        ModulPermission detachedPermission;
        using (var context = new DbContext())
            var groupToDetach = context.Groups.FirstOrDefault();
            detachedGroup = context.CreateDetachedCopy(groupToDetach, x => x.ModulePermissions);
            var permToDetach = context.ModulePermissions.SingleOrDefault(p => p.ModulePermissionId == 1062);
            detachedPermission = context.CreateDetachedCopy(permToDetach);
        detachedGroup.ModulePermissions.Add(detachedPermission); //Throws Exception
        using (var context = new DbContext())
            var attachedGroup = context.AttachCopy(detachedGroup);
            var test = context.HasChanges; //is true
            context.SaveChanges(); //Does not save the deleted Relation


    Thanks in advance,



  5. Xaver
    Xaver avatar
    6 posts
    Member since:
    Jan 2015

    Posted 18 Jul Link to this post

    Hi again,


    I got it to work now (but not perfect). It depends on the mapping. i have this (before without comments, now with comments):


        .HasAssociation<ModulPermission>(x => x.ModulePermissions)
        .WithOpposite(e => e.UserGroups)
            (group, permission) => new
                USER_GROUP_ID = group.UserGroupId,
                MODULE_PERMISSION_ID = permission.ModulePermissionId
    //    .HasAssociation<UserGroup>(x => x.UserGroups)
    //    .WithOpposite(x => x.ModulePermissions)
    //    .IsManaged()
    //    .MapJoinTable("T_USER_GROUP_REF_MODULE_PERM",
    //        (permission, group) => new
    //        {
    //            USER_GROUP_ID = group.UserGroupId,
    //            MODULE_PERMISSION_ID = permission.ModulePermissionId
    //        });


    If I try using IsManaged anywhere, the exeption is thrown. And if i try to use the association also on permissionMapping nothing is saved. The only problem i have now, thats the example in the post above is not working in the other way, using


    instead of



    can you please help me to understand, why its throwing an exeption using ismanaged and why nothing happens if i use association mappings on both sides and what i can do now, to get it work in both directions?


    Thanks in advance,


  6. Boris Georgiev
    Boris Georgiev avatar
    190 posts

    Posted 21 Jul Link to this post

    Hi Xaver,

    To define many-to-many association you need only one mapping, you don't need to add it twice for both ends. You should also have in mind that even it is many-to-many with join table, the association is represented in the OpenAccessContext as Parent-Child and it depends on how you are defining the association.

    Unfortunately IsManaged doesn't work with Attach/Detech and this is a known behavior. Each of the detached entities contains in itself a small copy of the context and when you add one entity to the other there is a conflict which context should manage the association and in which orders the entities should be added to the database.

    If you want to use Attach/Detach you should not use IsManaged and you should synchronize the association ends manually.

    I hope that helps.

    Boris Georgiev
    Telerik by Progress
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top
DevCraft banner