In LinqPad how to discard/clear all pending changes to database, LinqToSql DataContext does not have a method

Viewed 55

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

1 Answers

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.

Related