I am using Linq to SQL and have a scenario in which I want to insert into a table whenever a specific record is updated.
So lets say I have Table1
and Table2
, whenever a record in Table1
is updated I want to insert a new record into Table2
.
Currently I have working code that allows me to update a couple of fields in Table1
whenever it is updated, this has been achieved by creating a partial method for the UpdateTable1
function, like so:
partial void UpdateTable1(Table1 instance)
{
//update some fields
this.ExecuteDynamicUpdate(instance);
}
This works great, and is so useful that I want to use it to ensure the new record is always created in Table2
when Table1
is updated. This is mostly for logging purposes by the way.
So this is what I tried to do next:
partial void UpdateTable1(Table1 instance)
{
//update some fields
this.ExecuteDynamicUpdate(instance);
this.ExecuteDynamicInsert(instance.ConvertToTable2());
}
(I am using an extension method here to create a Table2 record based on the Table1 instance)
The problem is I am getting the following error:
The operation cannot be performed for the entity because it is not being change tracked.
Any ideas how I can make this work?