How to remove an object from the Entity Framework model without first loading it?

I am sure I saw the answer to this question somewhere, but since I could not find it with a few searches in SO or google, I still ask it again ...

In the Entity Framework, the only way to delete a data object is

MyEntityModel ent = new MyEntityModel(); ent.DeleteObject(theObjectToDelete); ent.SaveChanges(); 

However, this approach requires that the object is loaded, in this case, first into the controller, only to delete it. Is there a way to delete a business object that references only its identifier?

If there is a smarter way to use Linq or Lambda expressions, this is good too. The main goal, however, is to avoid downloading data only to delete it.

+39
asp.net-mvc linq-to-entities entity-framework
Feb 02 '09 at 10:38
source share
7 answers

It is worth knowing that the Entity Framework supports both Linq for Entities and Entity SQL. If you find that you want to delete or update files that may affect many records, you can use the equivalent of ExecuteNonQuery .

In Entity SQL, it might look like

  Using db As New HelloEfEntities Dim qStr = "Delete " & _ "FROM Employee" db.ExecuteStoreCommand(qStr) db.SaveChanges() End Using 

In this example, db is my ObjectContext . Also note that the ExecuteStoreCommand function accepts an optional array of parameters.

+19
Jun 13 '10 at 22:18
source share
β€” -

I found this post that says there really is no better way to delete entries. The explanation given was that all foreign keys, relationships, etc. that are unique to this record are also deleted, so EF must have the correct record information. I am puzzled why this cannot be achieved without loading the data back and forth, but since this does not happen very often, I decided (for now) that I would not worry.

If you have a solution to this problem, feel free to tell me =)

+8
Feb 02 '09 at 11:31
source share

I apologize in advance, but I have to question your goal.

If you delete an object without reading it, you cannot know if another user changed the object between the times when you confirmed that you want to delete the object and the actual deletion. In "plain old SQL", it would look like this:

 DELETE FROM FOO WHERE ID = 1234 

Of course, most people do not actually do this. Instead, they do something like:

 DELETE FROM FOO WHERE ID = 1234 AND NAME = ?ExpectedName AND... 

The fact is that the deletion should fail (do nothing) if another user changed the record in the interim period.

Thanks to this better description of the problem, there are two possible solutions to using the Entity Framework.

  • In the Delete method, an existing instance compares the expected property values ​​and deletes them if they are the same. In this case, the Entity Framework will take care of writing a DELETE statement that includes property values.

  • Write a stored procedure that accepts and executes both IDEs and other property values.

+4
02 Feb '09 at 14:02
source share

There is a way to trick object loading by recalculating EntityKey. This looks a bit hacky, but may be the only way to do it in EF.

Blog Article Unsampled Removal

+4
Feb 11 '09 at 11:44
source share

You can create an object with the same identifier and pass it to delete BUT this is useful for simple objects, if you have a complex relationship, you may need more than

  var user = new User { ID = 15 }; context.Entry(user).State = EntityState.Deleted; context.SaveChanges(); 
+4
Jun 14 '16 at 4:22
source share
 var toDelete = new MyEntityModel{ GUID = guid, //or ID = id, depending on the key }; Db.MyEntityModels.Attach(toDelete); Db.MyEntityModels.DeleteObject(toDelete); Db.SaveChanges(); 

If your key contains several columns, you need to specify all the values ​​(for example, GUID, columnX, columnY, etc.).

Look here for a general feature if you feel something fantastic.

+3
May 8 '13 at 2:32
source share

Entity Framework 5 has an Enhanced Entity Framework . Available on NuGet. Then you can write something like:

 context.Users.Delete(u => u.Id == id); 

It is also useful for bulk deletions.

+3
Jul 23 '13 at 7:07 on
source share



All Articles