2

I am using entity framework 4 and have the following setup and problem.

I have a table in MySql with metadata fields an a blob field. Using the Table Splitting technique described here i have divided the table into two entities (DataItem and DataItemDetails). This way I can load all the metadata without loading the blobs.

Problem is that when I try to delete a dataitem entity I get this exception:

exception: System.Data.UpdateException: Invalid data encountered. A required relationship is missing. Examine StateEntries to determine the source of the constraint violation.

If I turn of Lazy Loading or load the DataItemDetail part i can delete the DataItem.

This is OK, but I don't want to load the data just to delete it.

if (!D.DataItemDetailReference.IsLoaded)
     D.DataItemDetailReference.Load();
_db.DataItems.DeleteObject(d);
_db.SaveChanges();


_db is the ObjectContext dereived class and D is an instance of the DataItem class.
Markus
  • 61
  • 1
  • 1
  • 5

3 Answers3

6

If you don't want to load the property you must trick EF so it thinks that the related DataItemDetail is loaded.

var detailItem = new DataItemDetail { Id = d.Id }; 
_db.DataItemDetails.Attach(detailItem);
_db.DataItems.DeleteObject(d);
_db.SaveChanges();

The problem here is that table splitting uses 1:1 relation and EF knows that if it deletes one end of the relation it should also delete other end but because you didn't load other end it can't do it.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thank you, that was exactly what I was looking for. `if (!d.DataItemDetailReference.IsLoaded) { var Temp = new Models.DataItemDetails() { Id = d.Id }; _db.DataItemDetails.Attach(Temp); } this._db.DataItems.DeleteObject(d);` – Markus Apr 11 '11 at 08:43
  • @Ladislav: Waitaminute! This sure does look like a bug to me: since EF already knows it's a split table, it can just assume that the other end will disappear automatically, so that it doesn't have to delete it. Am I not right? – Fyodor Soikin Jan 23 '12 at 16:56
  • @Fyodor: The problem probably is that SQL generator doesn't have access to mapping details - it makes simple `DELETE`s for entities marked to be deleted. It doesn't explore any dependencies. So if the entity is not loaded and marked to be deleted it will not be deleted and you will get exception. – Ladislav Mrnka Jan 23 '12 at 17:00
  • @LadislavMrnka: Regardless of which particular deficiencies in the EF internal design cause this bug, it is still a bug, don't you agree? – Fyodor Soikin Jan 23 '12 at 17:10
1

Thanks Ladislav and Markus! This is exactly what I needed. To do this in Entity Framework 4.1 with Database First, I had to change it thusly:

If _db.Entry(d).Reference(Function(e) e.DataItemDetails).IsLoaded() = False Then
     Dim detailItem = New DataItemDetails With { .ID = d.ID }
     _db.DataItemDetails.Attach(detailItem)
End If
_db.DataItems.Remove(d)
_db.SaveChanges()
Eric
  • 11
  • 1
0

Is there any reason you can't make a call to a stored proc? MEF should handle those okay, and you'd be fine & fast to pass an ID to a stored proc where you can do what you like with the data.

Sorry...edit to say, I know this doesn't answer the question "how to with EF4", but if you're stuck this is a fairly viable work around (and easy to implement, too). Cheers.

MisterJames
  • 3,306
  • 1
  • 30
  • 48
  • Well, maybe using a stored procedure is the most effective way to do it. It would be nice to do everything from the EF4 framework, but loading the blob just to delete it seems a little awkward. Is it possible to add the stored proc to the model and just call a function? – Markus Apr 09 '11 at 09:28