This is a migrated thread and some comments may be shown as answers.

Detached entity errors on AttachCopy()

4 Answers 85 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
John
Top achievements
Rank 1
John asked on 03 Jan 2014, 02:18 PM
Hi,

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?

Thanks,

John



4 Answers, 1 is accepted

Sort by
0
Boris Georgiev
Telerik team
answered on 08 Jan 2014, 01:03 PM
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.

Regards,
Boris Georgiev
Telerik
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!
0
Xaver
Top achievements
Rank 1
answered on 15 Jul 2016, 02:29 PM

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
    //detachedGroup.ModulePermissions.RemoveAt(2);
 
    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,

Xaver

 

0
Xaver
Top achievements
Rank 1
answered on 18 Jul 2016, 07:58 AM

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):

 

userGroupMapping
    .HasAssociation<ModulPermission>(x => x.ModulePermissions)
    .WithOpposite(e => e.UserGroups)
    //.IsManaged()
    .MapJoinTable("T_USER_GROUP_REF_MODULE_PERM",
        (group, permission) => new
        {
            USER_GROUP_ID = group.UserGroupId,
            MODULE_PERMISSION_ID = permission.ModulePermissionId
            });
 
//permissionMapping
//    .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

detachedPermission.UserGroups.Add(detachedGroup);

instead of

detachedGroup.ModulePermissions.Add(detachedPermission);

 

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,

Xaver

0
Boris Georgiev
Telerik team
answered on 21 Jul 2016, 05:03 PM
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.

Regards,
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.
Tags
Development (API, general questions)
Asked by
John
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Xaver
Top achievements
Rank 1
Share this question
or