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?