1

I have a problem when using the Self-Traking Entities in compination of the WPF.

I have two entity set for exmaple: People and Numbers. Each person has many numbers and many numbers can have a person.

Here the sample code which I expect a number deletion:

using (var db = new Model.SampleEntities())
{
    list = db.People.Include("Numbers").ToList();
}

var samplePerson = list[0];
samplePerson.StartTracking();
var number = samplePerson.Numbers.First();
p.Numbers.Remove(number);

using (var db = new Model.SampleEntities())
{
    foreach (Model.Person person in list)
    {
        db.People.ApplyChanges(person);
    }
    db.SaveChanges();
}

It doesn't delete the number from DB.

When I change the TrackableCollection class (Which generated by the Self-Tracking T4 templates) by overriding the RemoveItem method and add the following code, it works as I expected:

protected override void RemoveItem(int index)
{
    var entity = ((IObjectWithChangeTracker)this[index]);
    base.RemoveItem(index);
    entity.MarkAsDeleted();
}

Is it correct to I put the MarkAsDeleted method here? So I should change the TT file to put this code in the TrackableCollection class. Is it a good approach?

Amir Karimi
  • 5,401
  • 4
  • 32
  • 51

1 Answers1

2

No it is not correct. You have many-to-many relation between person and number. You should strictly differ between removing relation (deleting record only from junction table) and between removing item itself and you should not make this automagically because removing number can affect many other persons which you don't work with at the moment (moreover without cascade deletes you will get exception in such case).

If you want to remove relation and delete item call MarkAsDeleted manually. Moreover without bigger effort your change will affect all entities in the model which is not what you want.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks for reply. It's not a many-to-many relation. It's a one to many (from person to numbers). My problem started when I bind the numbers of a person to a WPF datagrid. When a number delete from the datagrid it should be removed from DB. I dont want to call MarkAsDelete manually on each datagrid row delete event. How about finding out this situation (delete on a one to many navigation) and call the MarkAsDeleted then? – Amir Karimi Jun 30 '11 at 21:50
  • Well, I understood your description as many to many. The behavior is the same (bad) in the whole enity framework - it is not only problem of self tracking entities. Removing entity from the collection will only break the relation. The only exception is special type of [relation](http://stackoverflow.com/questions/4922228/entity-framework-4-delete-object-from-entity-collection/4925040#4925040) (but I'm not sure if it works in the same way with STEs). Placing `MarkAsDeleted` into template is dangerous because it will affect many-to-many relations as well. You should call `MarkAsDeleted` manually – Ladislav Mrnka Jun 30 '11 at 23:29
  • Got it! Thanks a lot. I will think about creating a customized DataGrid to handle this issue. – Amir Karimi Jul 01 '11 at 11:15