none
Saving a single entity instead of the entire context

    Question

  • I’m looking for a way to have fine grained control over what is saved using Entity Framework, rather than the whole ObjectContext.SaveChanges(). My scenario is pretty straight forward, and I’m quite amazed not catered for in EF – pretty basic in NHibernate and all other data access paradigms I’ve seen. I’m generating a bunch of data (in a WPF UI) and allowing the user to fine tune what is proposed and choose what is actually committed to the database. For the proposed entities I’m:

    1. getting a bunch of reference entities (eg languages) via my objectcontext,
    2. creating the proposed entities and assigning these reference entities to them (as navigation properties), so by virtue of their relationship to the reference entities they’re implicitly added to the objectconext
    3. Trying to create & save individual entites based on the proposed entities.

    I figure this should be really simple & trivial but everything I’ve tried I’ve hit a brick wall, either I set up another objectcontext & add just the entity I need (it then tries to add the whole graph and fails as it’s on another objectcontext). I’ve tried MergeOptions = NoTracking on my reference entities to try to get the Attach/AddObject not to navigate through these to create a graph, no avail. I've removed the navigation properties from the reference entities. I've tried AcceptAllChanges, that works but pretty useless in practice as I do still want to track & save other entities. In a simple test, I can create 2 of my proposed entities, AddObject the one I want to save and then Detach the one I dont then call SaveChanges, this works but again not great in practice. Following are a few links to some of the nifty ideas which in the end don’t help in the end but illustrate the complexity of EF for something so simple. I’m really looking for a SaveSingle/SaveAtomic method, and think it’s a pretty reasonable & basic ask for any DAL, letalone a cutting edge ORM.

    Sunday, November 29, 2009 8:43 PM

Answers

  • Hi Lingzhi,

    Thanks for the quick response. I'm really looking for an easy solution as EF is only a spike at the moment (I've introduced it to try & get my company onto ORM as I've seen the power of Nibernate but have chosen EF as they're too used to having a linq-2-sql design surface for mapping).

    I'm keen to jump on to 2010 ASAP, and if this had been an out-of-the-box thing in EF4 it would have been the clincher, however what your describing sounds like a significant time in research & code generation as a divergence from the simplicity of the designer.

    I was really hoping that someone else might have come across this situation and found an easy workaround. We're working ahead now on creating new poco objects for our 'proposed' entities and mapping to the EntityObject version once we're ready to commit. Some extra coding which violates DRY, but a lot less and a lot quicker than any alternatives I've seen (already spent 2 days on this now so need to bite the bullet)

    FWIW, I've added a suggestion here.
    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=515967

    Cheers,
    Andy


    Monday, November 30, 2009 11:10 AM
  • Hello znite,

     

    Welcome to MSDN Forums!

     

    From your description, you have done a deep research on how to manually update single entities in EF v1 (.NET 3.5 SP1).  In the current version of EF, it is really hard to accomplish your target to save singe entities and meanwhile track all the relationships.   Fortunately, in EF 4 (.NET 4.0), we have the new feature, self tracking entities, to make your application simpler.  Given this, we can manually manage the object tracking instead of letting the ObjectStateManager automatically manages the tracking for us.  

     

    Here are some really helpful product team blogs and MSDN magazine articles about the self tracking entities:

    http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2009/03/24/self-tracking-entities-in-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx

    http://msdn.microsoft.com/en-us/magazine/dd876635.aspx

     

    To make it simpler, we can use the T4 templates to generate self tracking entities.  Here are new APIs to manage the self tracking entities: ChangeObjectState (manage entity’s state), ChangeRelationshipSate (manage the entity’s relationship state), StartTracking/StopTracking (manage the entity tracking), and ApplyChanges (attach an entity graph and interpret the change tracking information stored in each entity)

     

    However, if only single entity is needed, we need to manage the relationships carefully.  The self tracking is generally designed for n-tier applications.  For your scenario, I think a method SaveSingle/SaveAtomic as you said would be helpful.  But we need to make the relationships of the single entity as some state like pending to save.  The situation could be really complicated. 

     

    If such a method (SaveSingle/SaveAtomic) is really important, I strongly recommend you to send a suggestion to the product team via Microsoft Connect (VS 2010).   I am really appreciate if you can share the link of your suggestion here to benefit other community members.  

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 30, 2009 2:09 AM
    Moderator

All replies

  • Hello znite,

     

    Welcome to MSDN Forums!

     

    From your description, you have done a deep research on how to manually update single entities in EF v1 (.NET 3.5 SP1).  In the current version of EF, it is really hard to accomplish your target to save singe entities and meanwhile track all the relationships.   Fortunately, in EF 4 (.NET 4.0), we have the new feature, self tracking entities, to make your application simpler.  Given this, we can manually manage the object tracking instead of letting the ObjectStateManager automatically manages the tracking for us.  

     

    Here are some really helpful product team blogs and MSDN magazine articles about the self tracking entities:

    http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2009/03/24/self-tracking-entities-in-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx

    http://msdn.microsoft.com/en-us/magazine/dd876635.aspx

     

    To make it simpler, we can use the T4 templates to generate self tracking entities.  Here are new APIs to manage the self tracking entities: ChangeObjectState (manage entity’s state), ChangeRelationshipSate (manage the entity’s relationship state), StartTracking/StopTracking (manage the entity tracking), and ApplyChanges (attach an entity graph and interpret the change tracking information stored in each entity)

     

    However, if only single entity is needed, we need to manage the relationships carefully.  The self tracking is generally designed for n-tier applications.  For your scenario, I think a method SaveSingle/SaveAtomic as you said would be helpful.  But we need to make the relationships of the single entity as some state like pending to save.  The situation could be really complicated. 

     

    If such a method (SaveSingle/SaveAtomic) is really important, I strongly recommend you to send a suggestion to the product team via Microsoft Connect (VS 2010).   I am really appreciate if you can share the link of your suggestion here to benefit other community members.  

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 30, 2009 2:09 AM
    Moderator
  • Hi Lingzhi,

    Thanks for the quick response. I'm really looking for an easy solution as EF is only a spike at the moment (I've introduced it to try & get my company onto ORM as I've seen the power of Nibernate but have chosen EF as they're too used to having a linq-2-sql design surface for mapping).

    I'm keen to jump on to 2010 ASAP, and if this had been an out-of-the-box thing in EF4 it would have been the clincher, however what your describing sounds like a significant time in research & code generation as a divergence from the simplicity of the designer.

    I was really hoping that someone else might have come across this situation and found an easy workaround. We're working ahead now on creating new poco objects for our 'proposed' entities and mapping to the EntityObject version once we're ready to commit. Some extra coding which violates DRY, but a lot less and a lot quicker than any alternatives I've seen (already spent 2 days on this now so need to bite the bullet)

    FWIW, I've added a suggestion here.
    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=515967

    Cheers,
    Andy


    Monday, November 30, 2009 11:10 AM
  • Hi Andy,

     

    Thank you very much for sharing the suggestion link here. 

     

    For the EF 4, I am really looking forward to it as well.  It is really improved a lot comparing with the version one.  I have read many related blogs about EF 4.  Additionally, I have some samples about EF 4 on All-In-One Code Framework (an open-source project owned by MSDN Forum Support Team), CSEFForeignKeyAssociation, VBFForeignKeyAssociation, CSEFComplexType, VBEFComplexType, CSEFModelFirst, and VBEFModelFirst.  We are planning to write more EF 4 samples recently. 

     

    If any community members have some ideas on this thread, I am also looking forward to hear your voice. 

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, December 01, 2009 5:06 AM
    Moderator