149

It seems to me that I have to retrieve an object before I delete it with entity framework like below

var customer = context.Customers.First(c => c.Id == 1);

context.DeleteObject(customer);

context.Savechanges();

So I need to hit database twice. Is there a easier way?

0

13 Answers 13

151

In Entity Framework 6 the delete action is Remove. Here is an example

Customer customer = new Customer () { Id = id };
context.Customers.Attach(customer);
context.Customers.Remove(customer);
context.SaveChanges();
6
  • 29
    Why Attach? Why not just Remove and SaveChanges?
    – runeks
    Sep 25, 2018 at 12:45
  • 4
    You have to attach your entity in the context because if you don't do that, you will receive an error while removing. EF can remove entities in this context only
    – Pierre-Luc
    Jan 9, 2019 at 14:43
  • 6
    @runeks according to the manual the entity must exist in the context before the Remove operation can be performed. See here learn.microsoft.com/en-us/dotnet/api/…
    – dwkd
    Jan 28, 2019 at 23:51
  • 9
    i didn't use attach, and it woks fine Jan 15, 2020 at 15:22
  • 20
    People might confuse the usage of Remove between Entity Framework Core and Entity Framework. It is true that for Entity Framework, the Remove method needs to be called after the Attach method, as mentioned by @Pierre-Luc. However, for EFCore, the Remove method will begin tracking the entity as deleted.
    – Yannick R.
    Nov 15, 2021 at 21:18
63

The same as @Nix with a small change to be strongly typed:

If you don't want to query for it just create an entity, and then delete it.

Customer customer = new Customer () { Id = id };
context.Customers.Attach(customer);
context.Customers.DeleteObject(customer);
context.SaveChanges();
2
  • 7
    Not perfect as it throws an exception if the object is missing: "DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0)." I'd like it to ignore this, like a DELETE statement would.
    – Dunc
    Jan 22, 2015 at 15:34
  • sorry, this causes validation which is not needed and expected always! Jul 16, 2017 at 12:35
39

Similar question here.

With Entity Framework there is EntityFramework-Plus (extensions library).
Available on NuGet. Then you can write something like:

// DELETE all users which has been inactive for 2 years
ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2))
     .Delete();

It is also useful for bulk deletes.

6
  • 59
    It defies reason that this isn't part of the core EF library by now. Dec 4, 2013 at 6:18
  • 2
    @FerretallicA - agreed.
    – acarlon
    Dec 8, 2013 at 4:15
  • 3
    this method is obsolete use: context.Users.Where(user=> user.Id == id).Delete();
    – Manuel
    Feb 12, 2016 at 9:22
  • It doesn't work with Azure SQL DataWarehouse due to error "A FROM clause is currently not supported in a DELETE statement.". But the raw SQL as in Jonik's answer works. Sep 8, 2016 at 11:34
  • 3
    Is context.SaveChanges() needed? Nov 22, 2018 at 8:42
23

If you dont want to query for it just create an entity, and then delete it.

Customer customer  = new Customer() {  Id = 1   } ; 
context.AttachTo("Customers", customer);
context.DeleteObject(customer);
context.Savechanges();
15

Since Entity Framework Core 7 you can use this:

await context.Customers.Where(c => c.Id == 1).ExecuteDeleteAsync();
1
  • However, be aware that the ExecuteDeleteAsync() doesn't respect the Change Tracker and transactional SaveChanges(). Thus, you have to create a facade transaction manually and handle the commit/rollback.
    – Rzassar
    Dec 1 at 7:35
11

I am using the following code in one of my projects:

    using (var _context = new DBContext(new DbContextOptions<DBContext>()))
    {
        try
        {
            _context.MyItems.Remove(new MyItem() { MyItemId = id });
            await _context.SaveChangesAsync();
        }
        catch (Exception ex)
        {
            if (!_context.MyItems.Any(i => i.MyItemId == id))
            {
                return NotFound();
            }
            else
            {
                throw ex;
            }
        }
    }

This way, it will query the database twice only if an exception occurs when trying to remove the item with the specified ID. Then if the item is not found, it returns a meaningful message; otherwise, it just throws the exception back (you can handle this in a way more fit to your case using different catch blocks for different exception types, add more custom checks using if blocks etc.).

[I am using this code in a MVC .Net Core/.Net Core project with Entity Framework Core.]

1
  • This is the ideal solution.
    – MacK
    Jul 9, 2021 at 18:34
7

In EF Core, if you don't care if the object exists or not, and you just care that it will not be in the DB, the simplest would be:

context.Remove(new Customer(Id: id));  // adds the object in "Deleted" state
context.SaveChanges();                 // commits the removal

You don't really need Attach() - it adds the object to the change tracker in the Unchanged state and Remove() adds the object to the tracker in the Deleted state. The most important thing, however, is that you do only one roundtrip to the backend.

5

This answer is actually taken from Scott Allen's course titled ASP.NET MVC 5 Fundamentals. I thought I'd share because I think it is slightly simpler and more intuitive than any of the answers here already. Also note according to Scott Allen and other trainings I've done, find method is an optimized way to retrieve a resource from database that can use caching if it already has been retrieved. In this code, collection refers to a DBSet of objects. Object can be any generic object type.

        var object = context.collection.Find(id);  
        context.collection.Remove(object);
        context.SaveChanges();
3
  • 3
    object could be null. If it null, .Remove(object); will throw an exception.
    – Kappacake
    Jan 28, 2021 at 15:11
  • 3
    While this is the easiest syntactically, the OP specifically asked for a way to delete without hitting the database twice. This works but it performs a SELECT (Find) and then a DELETE (Remove/SaveChanges) on the database...and as @demonicdaron already mentioned, it fails if the Find returns a null. Jun 12, 2022 at 13:08
  • Yes, this answer complete misses the point. Aug 26, 2022 at 9:03
2

dwkd's answer mostly worked for me in Entity Framework core, except when I saw this exception:

InvalidOperationException: The instance of entity type 'Customer' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

To avoid the exception, I updated the code:

Customer customer = context.Customers.Local.First(c => c.Id == id);
if (customer == null) {
    customer = new Customer () { Id = id };
    context.Customers.Attach(customer);
}
context.Customers.Remove(customer);
context.SaveChanges();
1
  • 6
    I believe you want to use FirstOrDefault?
    – El Mac
    Jun 25, 2021 at 9:02
2

A smaller version (when compared to previous ones):

var customer = context.Find(id);
context.Delete(customer);
context.SaveChanges();
2
  • 2
    Please provide some context to this code snippet, and perhaps some explanation of what it does better than the other answers left in the past decade.
    – miken32
    Jan 20, 2020 at 20:04
  • 1
    No. The very point of the question is how to prevent this roundtrip. Aug 26, 2022 at 9:02
2

From official documentation (and the most efficient one I have found so far):

Student studentToDelete = new Student() { ID = id };
_context.Entry(studentToDelete).State = EntityState.Deleted;
await _context.SaveChangesAsync();
1
  • 1
    You need to link that "official document" that mentions it. Nov 24, 2022 at 3:43
1

Raw sql query is fastest way I suppose

public void DeleteCustomer(int id)
{
   using (var context = new Context())
   {
      const string query = "DELETE FROM [dbo].[Customers] WHERE [id]={0}";
      var rows = context.Database.ExecuteSqlCommand(query,id);
      // rows >= 1 - count of deleted rows,
      // rows = 0 - nothing to delete.
   }
}
7
  • 24
    This defeats the purpose of using strongly typed object funtionality in EF.
    – LawMan
    Mar 4, 2015 at 17:20
  • 4
    This compromises EF identity cash. After this EF will still return to you your deleted entity.
    – epox
    Aug 26, 2016 at 23:17
  • 1
    It works with Azure SQL DataWarehouse, when other solutions do not. Sep 8, 2016 at 11:37
  • 1
    If you're doing this, you might as well not use an ORM. I imagine that this would compromise the EF cache. Oct 6, 2018 at 11:31
  • 1
    This style is vulnerable to SQL Injection attacks. In this specific example you're protected because the variable is an integer, but never use this pattern with a string variable.
    – thelem
    Jun 29, 2020 at 11:23
0

Easier and more understandable version.

var customer = context.Find<Customer>(id);
context.Remove(customer);
context.SaveChanges();

Not the answer you're looking for? Browse other questions tagged or ask your own question.