0

I have a postgres database containing numberous tables. The project is implemented using Entity Framework.

I have 3 database classes lets say A, B & C. Class A contains list of B's objects. Class B contains list of C's objects. Class C is a simple class containing some fields.

Beginning of these classes look like this:

[Table("A")]
public class A: GUIDModelBase
{....
}

Data is filled as below:

Parent1 [A]
    -Child1 [B]
        --Grandchild1 [C]
        --Grandchild2 [C]
    -Child2 [B]
        --Grandchild3 [C]
        --Grandchild4 [C]

For some specific requirement, I retrieve the data of table into an object A using API as below:

A AObj = commonDbService.ARepository.GetAById(A.Id);

GetAById is implemented using dbset as below:

protected DbSet<TEntity> dbset = null;
public Project GetAById(Guid id)
{
    return dbset.Include(x => x.CoordinateSystem)
        .SingleOrDefault(p => p.Id == id);
}

I want to manipulate(Dont want to have Grandchild2 and Grandchild4) and store the data of AObj in another class object as below:

MigrationData md = new MigrationData();
md.ANewObj = Manipulate(AObj);  //Removes two grandchildren elements

When I do this, even AObj gets completely modified too. I need complete data intact in AObj and also in the database table(but temporarily until restart of my application). The table data is shown in one of the UI pages. As soon as its modified as in above lines, the UI page shows only remaining 2 grandchildren. But if i restart my application, all grandchildren are existing in database.

Because of this, I am not able to modify it separately without modifying the base database table. Can anyone help me to get this done ??

vinmm
  • 277
  • 1
  • 3
  • 14
  • Possible duplicate of [How do I detach objects in Entity Framework Code First?](http://stackoverflow.com/questions/5599147/how-do-i-detach-objects-in-entity-framework-code-first) – Gusman May 04 '17 at 16:24
  • @Gusman: I tried following both ways to detach my parent object as in the link you shared above. But as soon as I detach or AsNoTracking, all the child objects are completely removed. Only the parent and its fields are intact and children count goes to zero. I want all the data to be intact in the object and further changes to this object to not affect the database. Looks like i am closer to solution, but not able to get it working. Can you let me know more on how to do this exactly ? Thanks. – vinmm May 05 '17 at 04:50
  • Did you included the child elements on the query? If you don't include them by default EF will do a lazy load, and when the object is detached it's not possible to use it. I think if you explicitli Include the subelements they will be preserved. Else, try to first detach the child elements and then detach the root object. – Gusman May 05 '17 at 13:25
  • @Gusman: Yes I tried detaching the child elements first and then go to parent. But my tables refer back to parent have a list of parent objects too in some cases. I mean back references. So losing some data and not getting the complete working thing. As Alex told below, its pain when having back-references. No easy way to get this done my way I suppose. – vinmm May 08 '17 at 03:51

1 Answers1

1

Short answer is, it depends on what the manipulation of the object actually does. If you're modifying the object while it is attached to the context then yes, Entity Framework will see the object as modified and will update it if you call SaveChanges. Either detach the objects you want detached so you can work on them freely, or don't change them and map/clone them to new objects instead.

Alex Paven
  • 5,539
  • 2
  • 21
  • 35
  • As I mentioned above, manipulation involves stripping off few objects from the list in parent like removing grandchild2 and grandchild4. I tried using detach. Once i call detach on that object, all its child items and grandchild items are already stripped off. Only parent's fields & values are retained. How should I detach and use while having all the data inside parent object ?? Please let me know. – vinmm May 05 '17 at 04:46
  • Right, when detaching just a parent entity the children are not automatically detached. I'd have also suggested AsNoTracking and I'm puzzled by you saying it doesn't work. It should behave the same way as the regular version, unless you had the child objects in the context from a previous query. EF automatically fixes up relationships so maybe that's what happens. Do the query AsNoTracking including all children entities and I think it'll be fine. – Alex Paven May 05 '17 at 09:24
  • Just realized, there are some more nested list of objects of parent in child and list of child in grandchild objects and some more list of grandgrandchild class too. Gonna be very hard to individually detach everything everytime. :'(. Dont think there is any solution to my problem. – vinmm May 05 '17 at 11:58
  • Have you tried doing AsNoTracking correctly? Yes, having back-references can always be a pain, you need to manage that carefully. But if you load the graph with AsNoTracking, and Include() the elements that you need, you will have the detached entities available, I promise you. – Alex Paven May 05 '17 at 14:21