Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use Sql CE 4 databases for functional tests
    primarykey
    data
    text
    <p>Due to the potential differences between Linq-to-Entities (EF4) and Linq-to-Objects, I need to use an actual database to make sure my query classes retrieve data from EF correctly. Sql CE 4 seems to be the perfect tool for this however I have run into a few hiccups. These tests are using MsTest.</p> <p>The problem I have is if the database doesn't get recreated (due to model changes), data keeps getting added to the database after each test with nothing getting rid of the data. This can potentially cause conflicts in tests, with more data being returned by queries than intended.</p> <p>My first idea was to initialize a <code>TransactionScope</code> in the <code>TestInitialize</code> method, and dispose the transaction in <code>TestCleanup</code>. Unfortunately, Sql CE4 does not support transactions.</p> <p>My next idea was to delete the database in <code>TestCleanup</code> via a <code>File.Delete()</code> call. Unfortunately, this seems to not work after the first test is run, as the first test's <code>TestCleanup</code> seems to delete the database, but every test after the first does not seem to re-create the database, and thus it gives an error that the database file is not found.</p> <p>I attempted to change <code>TestInitialize</code> and <code>TestCleanup</code> tags to <code>ClassInitialize</code> and <code>ClassCleanup</code> for my testing class, but that errored with a <code>NullReferenceException</code> due to the test running prior to <code>ClassInitialize</code> (or so it appears. <code>ClassInitialize</code> is in the base class so maybe that's causing it).</p> <p>I have run out of ways to effectively use Sql CE4 for testing. Does anyone have any better ideas?</p> <p><hr> <b>Edit:</b> I ended up figuring out a solution. In my EF unit test base class I initiate a new instance of my data context and then call <code>context.Database.Delete()</code> and <code>context.Database.Create()</code>. The unit tests run a tad slower, but now I can unit test effectively using a real database</p> <p><hr> <b>Final Edit:</b> After some emails back and forth with Microsoft, it turns out that <code>TransactionScope</code>s are now allowed in SqlCE with the latest release of SqlCE. However, if you are using EF4 there are some limitations in that you must explicitly open the database connection prior to starting the transaction. The following code shows a sample on how to successfully use Sql CE for unit/functional testing:</p> <pre><code> [TestMethod] public void My_SqlCeScenario () { using (var context = new MySQLCeModelContext()) //ß derived from DbContext { ObjectContext objctx = ((IObjectContextAdapter)context).ObjectContext; objctx.Connection.Open(); //ß Open your connection explicitly using (TransactionScope tx = new TransactionScope()) { var product = new Product() { Name = "Vegemite" }; context.Products.Add(product); context.SaveChanges(); } objctx.Connection.Close(); //ß close it when done! } } </code></pre>
    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.
 

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