Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found the approach in the "final edit" works for me as well. However, it's REALLY annoying. It's not just for testing, but any time you want to use TransactionScope with Entity Framework and SQL CE. I want to code once and have my app support both SQL Server and SQL CE, but anywhere I use transactions I have to do this. Surely the Entity Framework team should have handled this for us!</p> <p>In the meantime, I took it one step farther to make it a little cleaner in my code. Add this block to your data context (whatever class you derive from DbContext):</p> <pre><code>public MyDataContext() { this.Connection.Open(); } protected override void Dispose(bool disposing) { if (this.Connection.State == ConnectionState.Open) this.Connection.Close(); base.Dispose(disposing); } private DbConnection Connection { get { var objectContextAdapter = (IObjectContextAdapter) this; return objectContextAdapter.ObjectContext.Connection; } } </code></pre> <p>This makes it a lot cleaner when you actually use it:</p> <pre><code>using (var db = new MyDataContext()) { using (var ts = new TransactionScope()) { // whatever you need to do db.SaveChanges(); ts.Complete(); } } </code></pre> <p>Although I suppose that if you design your app such that all changes are committed in a single call to SaveChanges(), then the implicit transaction would be good enough. For the testing scenario, we want to roll everything back instead of calling ts.Complete(), so it's certainly required there. I'm sure there are other scenarios where we need the transaction scope available. It's a shame it isn't supported directly by EF/SQLCE.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload