none
Entity Detaching problem RRS feed

  • Question

  • EF Detaching problem.

     

    I have a need to create some entities which, before saving need to detach. I do not want them to exist in the DB. The problem is to prevent them being saved into the database.

     

    The structure of the table / entity is:

    Name: WpPlanAggregated

    This has two foreign keys:

    Foreign key to WpPlan: called IDPlanParent  (not null)

    A second Foreign key to WpPlan: called IDPlanChild (not null)

    WpPlan also contains a number of ID’s with foreign keys to other objects.

     

    As a result each WpPlanAggregated has many objects hanging off it.

     

    To detach these entities I run

     

    var pocosInGraph = Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);

     foreach (var entityEntry in pocosInGraph)

     {

           Scope.Context.Detach(entityEntry.Entity);

     }

     Scope.Context.AcceptAllChanges();

     

    Examining pocosInGraph before I Detach them I can see the WpPlanAggregates that I need to detach. After detaching they apparently do not exist.

     

    I then call Context.SaveChanges() because I do need to update other entities that are unconnected with the Entities that need to be detached.

     

    The database is getting updated with the detached entities and  everything hanging off them :

    WpPlanAggregated – WpPlan plus all the foreign keyed parts attached to each WpPlan …

    Help Please !

    (in case this looks odd the reason for doing it is to re-use a lot of code where the entities would need to be saved, and then use the output from this to update other entities)

    Wednesday, September 28, 2011 10:31 AM

All replies

  • Hi,

    According with AcceptAllChanges() documentation, this method also call Detach():

    http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.acceptallchanges.aspx

    So, may be you should ser as Unchanged your entities before call AcceptAllChanges().

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Wednesday, September 28, 2011 11:13 AM
  • Many thanks. Sadly I still have the problem.

    I set the 'Added' entities to 'Unchanged' and then called 'AcceptAllchanges'.

    The consequence was that it still saved the newly created ones but didn't save the updated ones.

    I then removed 'AcceptAllchanges'. This time the modified ones did get updated, but the Added ones that were set to 'Unchanged' still got inserted ...

    Many thanks for your response though

    Ellie

    Wednesday, September 28, 2011 2:40 PM
  • Hi again,

    Well, may be despite change state to Unchanged these items still remains ObjectsAddedToCollectionProperties at collection. Try to perform something like this instead:

    entity,ChangeTracker.ObjectsAddedToCollectionProperties.Clear();
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Wednesday, September 28, 2011 3:25 PM
  • Unfortunately I am dealing with inherited code which doesn't enable ChangeTracker, so I'm a bit stuck here. I suspect my problem is because of issues connected with how EF has been implemented, and its use is so wide-spread in this project, its not something I dare embark on at the moment.

    Many thanks for your help. I do have a plan (b) - don't create entities in the first place, which is long winded, but probably the only way out at the moment.

    Regards

    Ellie


    Ellie
    Thursday, September 29, 2011 7:10 AM