Reputation: 897
I'm creating synchronization system for SQL Server database.
public void UpdateTable<T>(List<T> current, List<T> changed) where T : class
{
var deletedRecords = current.Except(changed);
foreach (T entity in deletedRecords)
{
_context.GetTable<T>().DeleteOnSubmit(entity);
}
_context.SubmitChanges();
}
But on DeleteOnSubmit
operation it fails with InvalidOperationException: Unable to remove an embedded object.
Database was designed with CASCADE
delete operation. What's wrong? Thank you!
Upvotes: 3
Views: 784
Reputation: 6252
Have you seen this post?. In this example, it creates a generic Interface with a type T which inherits from class. This interface exposes the method Delete(T Item) which does something similar than what you did. The problem could be that you need to perform a db.SubmitChanges() for each object but I'm not really sure. So, I suggest to call this method for each object in your deletedRecords list. Hope it helps!
// Deletes the data values in the LINQ to SQL generated class.
public virtual bool Delete(T Item)
{
using (TestDBContext db = new TestDBContext())
{
db.GetTable<T>().DeleteOnSubmit(Item);
db.SubmitChanges();
return true;
}
}
Upvotes: 1