views:

6508

answers:

5

I am quite sure I've seen the answer to this question somewhere, but as I couldn't find it with a couple of searches on SO or google, I ask it again anyway...

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

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

However, this approach requires the object to be loaded to, in this case, the Controller first, just to delete it. Is there a way to delete a business object referencing only for instance its ID?

If there is a smarter way using Linq or Lambda expressions, that is fine too. The main objective, though, is to avoid loading data just to delete it.

+5  A: 

I found this post, which states that there really is no better way to delete records. The explanation given was that all the foreign keys, relations etc that are unique for this record are also deleted, and so EF needs to have the correct information about the record. I am puzzled by why this couldn't be achieved without loading data back and forth, but as it's not going to happen very often I have decided (for now) that I won't bother.

If you do have a solution to this problem, feel free to let me know =)

Tomas Lycken
How about using a stored procedure?
Pure.Krome
+1  A: 

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

If you delete an object without ever reading it, then you can't know if another user has changed the object in between the time you confirmed that you wanted to delete the object and the actual delete. In "plain old SQL", this would be like doing:

DELETE FROM FOO
WHERE ID = 1234

Of course, most people don't actually do this. Instead, they do something like:

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

The point is that the delete should fail (do nothing) if another user has changed the record in the interim.

With this, better statement of the problem, there are two possible solutions when using the Entity Framework.

  1. In your Delete method, the existing instance, compare the expected values of the properties, and delete if they are the same. In this case, the Entity Framework will take care of writing a DELETE statement which includes the property values.

  2. Write a stored procedure which accepts both the IDE and the other property values, and execute that.

Craig Stuntz
+1  A: 

Look up Lazy Loading. You can delay the loading of the object until you access it, which you won't because you're just deleting it.

ajma
+3  A: 

There's a way to spoof load an entity by re-calculating it's EntityKey. It looks like a bit of a hack, but might be the only way to do this in EF.

Blog article on Deleting without Fetching

biozinc
+3  A: 

It is worth knowing that the Entiy Framework supports both to Linq to Entities and Entity SQL. If you find yourself wanting to do deletes or updates that could potentially affect many records you can use the equivalent of ExecuteNonQuery. In entity SQL this 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 takes an optional array of parameters.

John Wigger
This answer goes directly to the point of what the asker would like to do and should be marked as the accepted answer. Other solutions like constructing entity from nothing are less optimal. Also check this out http://msdn.microsoft.com/en-us/library/ee358769.aspx
mare