none
db.entry(entity).State == EntityState.Deleted is not same as db.Set<T>().Remove(entity)

    Question

  • I noticed that marking an entity as deleted using the state option vs removing the entity from the context is not the same thing. 

    when i remove an entity, my cascade options work as advertised where as when i mark an entity as deleted using state entry, the cascade delete option does not work.

    Is this a bug or by design.


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Monday, July 18, 2011 7:07 PM

Answers

All replies

  • Hi zeeshan,

    Welcome!

    According to your description, I write a sample to repro your scenario, but it works, I will show the steps as follows:

    1. set the foreign key's Delete Rule to Cascade in database.

    2.

    using (var context= new EFTestContext())
          {
            var employee = context.Employees.Find(0);
            context.Employees.Remove(employee);
            // context.Entry(employee).State = System.Data.EntityState.Deleted;
            context.SaveChanges();
          }
    

    3. call SaveChanges() method, you can see the record through database, it works.

    I may miss something, would you please feel free to let me know your feedbacks?

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 19, 2011 9:08 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 26, 2011 5:57 AM
    Moderator
  • what i mean is this.

    if i set a cascade option on my model so that when customer is marked as deleted then orders that are loaded in the context are also marked for deletion. for instance

    var customer = db.Customers.Include(c => c.Orders).First(c => c.CustomerId == 1);

    db.Customers.Remove(customer); // at this point all orders in teh context are also marked as deleted.

     

    However when you do this

    db.Entry(customer).State = EntityState.Deleted;

    does not marked the orders as deleted.

     


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Thursday, July 28, 2011 2:05 PM
  • Hi Zeeshan,

    Thanks for your feedback.

    I got your point. But there are actually different things. 

    Base on my testing, When you Include the related entities and just set the Entry.State= deleted, it will throw an exception, otherwise you don't include the related entities, it works, the cascade action will be executed in database.

    The Move method will automatically call DetechChanges and set all related entities' states.

    http://blogs.msdn.com/b/adonet/archive/2011/02/06/using-dbcontext-in-ef-feature-ctp5-part-12-automatically-detecting-changes.aspx

    So there are not same thing, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 05, 2011 9:41 AM
    Moderator
  • Indeed they are not the something cuz one works at the entity level and other works at the entry level in the context. Its interesting that lot of people are using it interchangeably without being aware of these subtle differences.
    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Friday, August 05, 2011 5:09 PM
  • Hi zeeshan,

    Thanks for your feedback.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 09, 2011 12:21 PM
    Moderator