LinqToSQL CancelUpdate

  • Thread starter Thread starter Bram
  • Start date Start date
B

Bram

I'm writing an application using LinqToSQL. Basically the application
works as follows:
- Read a file
- Add rows to the database (the rows belong to more than one table).
- Call DataContext.SubmitChanges() to presis those rows into the
database.

It might be possible that there are problems in the datal. In this
case, the rows don´t need to be inserted into the database. So I don´t
call SubmitChanges. However, this scenario causes serious issues.
Since there is no CancelUpdate, LINQ to SQL will attempt to add the
data from the earlier attempt, which probably is corrupt. The only way
to recover is to restart the application!

I really like the transaction idea of Linq: make a lot of changes and
Linq applies them in batch. However, as far as I know there is no way
to cancel these changes, which is a huge drawback. Am I overlooking
something? Is there some way to cancel the pending changes? Or do I
need to keep track of each and every object I either modify or insert
and revert the changes myself?

Any pointers would be greatly appreciated!
Kind regards,
Bram Fokke
 
If you don't call SubmitChanges() there is no update, so we are good on the
DB level.
To handle your linq objects, just dispose the old DC and create a new one is
the easiest way.
If this "error" is the exceptional case, this method should be fine.

W

I'm writing an application using LinqToSQL. Basically the application
works as follows:
- Read a file
- Add rows to the database (the rows belong to more than one table).
- Call DataContext.SubmitChanges() to presis those rows into the
database.

It might be possible that there are problems in the datal. In this
case, the rows don´t need to be inserted into the database. So I don´t
call SubmitChanges. However, this scenario causes serious issues.
Since there is no CancelUpdate, LINQ to SQL will attempt to add the
data from the earlier attempt, which probably is corrupt. The only way
to recover is to restart the application!

I really like the transaction idea of Linq: make a lot of changes and
Linq applies them in batch. However, as far as I know there is no way
to cancel these changes, which is a huge drawback. Am I overlooking
something? Is there some way to cancel the pending changes? Or do I
need to keep track of each and every object I either modify or insert
and revert the changes myself?

Any pointers would be greatly appreciated!
Kind regards,
Bram Fokke
 
Hello William,

Thanks for your reply. Disposing the old DataContext does work,
however in my opinion this is not a very elegant solution. AFAIK the
DC is also the object that keeps track of object identity. If I were
to retrieve an object I have used before disposing the DC, there would
be two objects with the same primary key present. This is clearly not
a desirable situation.

Linq keeps track of all changes to entities. Because of this, it
should be possible to revert all changes since the last SubmitChanges
or all changes made to a single object.

Kind regards,
Bram Fokke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top