1

Using the amazing LinqPad. Is there a way to clear all pending database changes to the built-in LinqToSql DataContext (the UserQuery object which is this)?

Example LinqPad file, connection to a basic mssql database with tb_person table.

void Main()
{
   var newRow = new tb_person() {FirstName = "Bob"};
   // do lots of db updates, deletes, inserts....
   tb_persons.InsertOnSubmit(newRow);

   // how to do this, revert all pending Db changes?       
   //this.SomeMethodToDiscardChanges()       
}

I found 2 option, neither seems to be ideal, maybe there is a LinqPad specific way outside of LinqToSql?

  1. Create a new DataContext, how to do this in LinqPad?
  2. DataContext.Refresh() method passing in every added or changed entity, cumbersome!

References
How to clear the DataContext cache on Linq to Sql

Rax
  • 665
  • 8
  • 19
  • Does this answer your question? [How can I reject all changes in a Linq to SQL's DataContext?](https://stackoverflow.com/questions/259219/how-can-i-reject-all-changes-in-a-linq-to-sqls-datacontext) – Gert Arnold Jun 12 '22 at 17:19

1 Answers1

0

No guarantees, but you can try

this.Uncapsulate().ClearCache();

I've used this and it seems to work, but I've never used it on a production database.

Alternatively, you can create a new DataContext using

var dc = new TypedDataContext(this.Connection.ConnectionString);

but of course you can't assign to this which means that you need to be careful to prefix all your queries and references to use dc.

sgmoore
  • 15,694
  • 5
  • 43
  • 67