Posted by Sébastien Lachance with Comments (0)
When using LinqToSql and unit testing in your application, you can benefit from two handy method calls that will make your life a lot easier.
var db = new DatabaseDataContext(); db.DeleteDatabase(); db.CreateDatabase();
You won't need to worry about putting the database in a correct state before each of your test. I call them in my setup part of each unit tests that make use of the database.
If you need to insert some test data, so I used the ExecuteCommand method to execute a sql script.
db.ExecuteCommand(File.ReadAllText(@"..\..\..\..\db\referencedata.sql"));
var db = new DatabaseDataContext(); db.DeleteDatabase(); db.CreateDatabase(); db.ExecuteCommand(File.ReadAllText(@"..\..\..\..\db\referencedata.sql")); db.Connection.Close();
Related posts