Based on information from here .
I discovered how to remove orphans using the Entity Framework.
public void SaveChanges() { context.ReportCards .Local .Where(r => r.Student == null) .ToList() .ForEach(r => context.ReportCards.Remove(r)); context.SaveChanges(); }
I was wondering how to create a generic function for this part, because it can be used often:
context.ReportCards .Local .Where(r => r.Student == null) .ToList() .ForEach(r => context.ReportCards.Remove(r));
I thought of something like this:
public void SaveChanges() { RemoveOrphans(Student, ReportCards) context.SaveChanges(); } private void RemoveOrphans<T>(T sourceContext, T orphan) { context.orphan .Local .Where(r => r.sourceContext == null) .ToList() .ForEach(r => context.orphan .Remove(r)); }
But of course this will not work. Any tips?
Dipix source share