views:

209

answers:

1

My God, EF is so frustrating. I can't seem to be able to get my head around what I need to do so I can delete an object. I seem to be able to remove the object but not the related child objects. Can anyone tell me what is the rule of thumb when you want to delete all related child objects of a given object?

I've tried loading all related objects like this:

Entry entry = ModelContext.GetObjectByKey(new EntityKey("ModelContainer.EntrySet", "Id", id)) as Entry;
entry.ChildEnteries.Load();

if (entry != null)
{
    ModelContext.DeleteObject(entry);
    ModelContext.SaveChanges();
}

I get errors related to the relationships: A relationship is being added or deleted from an AssociationSet 'EntryEntry'. With cardinality constraints, a corresponding 'Entry1' must also be added or deleted.

Why can't I just load the object using modelcontext.GetObjectByKey and remove it along with its child objects?

My other question is can I delete an object using Entity command like so?

DELETE e from objectset as e where e.id = 12

I've tried few variations and all of them throw exceptions.

+3  A: 

If you are not using cascade deletes you will have to delete the referenced objects independently.

Then I think you want something like this:

ObjectSet os = ModelContext.ObjectSet.First(x => x.id == 12);
ModelContext.DeleteObject(os);      
ModelContext.SaveChanges();

You can do cascade deletes in the entity framework too, but make sure you set it up in your SQL and then make sure you update from data source from the EDMX file. In particular make sure CSDL part has the following:

<Association Name="FK_ItemChildren_Item">
 <End Type="Model.Item" Role="Item" Multiplicity="1">
  <OnDelete Action="Cascade"></OnDelete>
 </End>
 <End Type="Model.ItemChildren" Role="ItemChildren"
  Multiplicity="*">
 </End>
</Association>
Brian R. Bondy
Tried your solution before. It only deletes the object but not the related child objects.
Am
@Am: added more info
Brian R. Bondy
The problem is that I've manually defined the edmx file and mapped the fields of my enities to fields of tables in DB. All my tables in MySql are MyISAM and I have no foreign keys defined for them. Will adding cascade delete manually to the edmx file work even when there exist no actual foreign keys defined in the db?
Am
@Am: I'm not sure, you could try. See the link there are 2 sections you need to add it to.
Brian R. Bondy
@Brian R. Bondy: I'll give the suggestion in the link a go and let you know what happens
Am
@Am: Sounds good, looking forward to your response.
Brian R. Bondy
Brian R. Bondy: BTW, between these different ORM technologies (ie EF, NHibernate and etc) which one would you say is the most easy to use? I am really getting sick of ORM in .Net it really isn't that mature or straight forward.
Am
@Am: I haven't tried NHibernate so not sure, try posting as a new question maybe. By the way I'm hoping that entity framework has better support in VS 2010 but unsure if it will.
Brian R. Bondy
@Am: The main thing I don't like about entity framework is exceptions like LINQ to entities does not support X. Where X is a bunch of different things that you should be able to do.
Brian R. Bondy
@Am, regarding "not mature or staight foward"; compared to what? Your complaining about EF not doing cascading deletes automatically with a 1-* relationship. That would be pretty dangerous for EF to do by default. Insane even.
jfar
@Brian I gave up on EF and looked else where for a solution. I gave Castle ActiveRecord ago and I haven't looked back since. I really like it because I was able to actually do DDD and maintain a very simple code.
Am