locked
.Net Entityframework SaveChanges Delete Rows RRS feed

  • Question

  • Hi Experts,

    i am developing an application that uses entityframework to store the data. I have some columns with not too many data. Therefore i load all tables at Startup and work on the Cache.
    When i call savechanges all updates and inserts are stored correctly in my database. My Problem is deleting the rows. When i call savechanges the rows still exist, the Framework just removes the refrencial keys.
    Is there a way to let savechanges delete the rows from the database and not just dereference them?

    Kind Regards
    Manu

    • Moved by Mike Feng Thursday, May 23, 2013 8:38 AM
    Wednesday, May 22, 2013 11:37 AM

Answers

All replies

  • How are you deleting your rows?

    Just clearing property/collection on the parent object won't delete referenced/referencing rows, you should call context.DeleteObject() (or context.Table.Remove()) for each that row.

    • Marked as answer by blenet Thursday, May 23, 2013 7:33 AM
    • Unmarked as answer by blenet Thursday, May 23, 2013 8:08 AM
    Thursday, May 23, 2013 1:23 AM
  • Here is my example:

    public DbSet<Entry> dbsEntries { get; set; } dbsEntries.Load(); //Bind to Datagrid dgData.ItemsSource = dbsEntries.ToList();

    //On Application Exit

    context.SaveChanges();

    Now when i delete a row in the datagrid only the reference to the parentobject is deleted.

    Is there a way to get all deleted Entries of the objectcontext. I tried DataContext.ChangedTracker.Entries() but in this collection the deleted items does not Show up as deleted, they are marked as Unchanged?

     
    • Edited by blenet Thursday, May 23, 2013 8:16 AM
    Thursday, May 23, 2013 8:13 AM
  • Hi blenet;

    What kind of project are you working on, Windows Form, WPF, Web Project? For example a ItemsControl.ItemSource property from Microsoft documentation, "When the ItemsSource property is set, the Items collection is made read-only and fixed-size.", so the Context should not be updated.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, May 23, 2013 2:55 PM
  • Hi Fernando,

    i am working on a WPF Project. It seems that i found out what is going wrong, maybe have a look at my other Forum post:

    http://social.msdn.microsoft.com/Forums/de-DE/netfxbcl/thread/017fafc9-3d09-478f-9f4d-6e32f12ffcfb

    Kind Regards

    Manu

    • Marked as answer by Chester Hong Monday, May 27, 2013 3:09 AM
    Thursday, May 23, 2013 3:20 PM
  • Hi blenet,

    I'm glad to hear that you have found out the problem.

    Thanks for sharing the solution. Have a nice day!

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 27, 2013 3:09 AM