<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>duncanMgunn.com - Database</title>
    <link>http://www.duncangunn.me.uk/dasblog/</link>
    <description />
    <language>en-us</language>
    <copyright>Duncan M Gunn</copyright>
    <lastBuildDate>Tue, 29 Jun 2010 13:26:15 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>dasblog@example.com</managingEditor>
    <webMaster>dasblog@example.com</webMaster>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=3aad13b9-1d61-40e2-a9aa-800e1432d053</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,3aad13b9-1d61-40e2-a9aa-800e1432d053.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,3aad13b9-1d61-40e2-a9aa-800e1432d053.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=3aad13b9-1d61-40e2-a9aa-800e1432d053</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">If we have a varchar column as a Primary
Key (don't ask - we also are not able to alter the schema) then we can still use Max
to generate IDs programmatically.<br /><br />
However, because the column is char, then SQL Server will determine that '9' is actually
greater than '1','0' [10] so we need to be a little bit smarter.<br /><br />
Use this:<br /><br /><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial; font-size: 12px; line-height: 20px;">1.
DECLARE @Length INT<br />
2. SET @Length=(SELECT MAX(LEN(ID)) from test)<br />
3. SELECT TOP 1 ID FROM Test WHERE LEN(ID)=@Length ORDER BY ID DESC<br /><br />
Thanks to </span></span><a href="http://www.dotnetspider.com/forum/192995-Can-we-apply-Max-function-SQL-Query-for.aspx">http://www.dotnetspider.com/forum/192995-Can-we-apply-Max-function-SQL-Query-for.aspx</a><br /><br />
And if you want to call this using LINQ to SQL (or variant thereof), you could try:<br /><br /><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 11px;">CREATE
PROCEDURE web_GetMaxCampaignCode 
<br />
(@maxcode int output)<br />
AS<br />
BEGIN<br />
    -- SET NOCOUNT ON added to prevent extra result sets from<br />
    -- interfering with SELECT statements.<br />
    SET NOCOUNT ON;<br /><br />
  DECLARE @Length INT<br />
  SET @Length=(SELECT MAX(LEN(campaign_code)) from ref_campaign_code)<br />
  SET @maxcode = (SELECT TOP 1 campaign_code FROM ref_campaign_code WHERE LEN(campaign_code)=@Length
ORDER BY campaign_code DESC)<br />
END<br />
GO<br /><br /><br />
Calling Code:<br /><br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> GetFirstName(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> id)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">using</span> (MyDataContext
dataContext <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">new</span> MyDataContext())
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> FirstName;
var query <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> dataContext.GetFirstName(id, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">ref</span> FirstName); <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">return</span> FirstName;
} }</span></pre>
Then just drag your sproc onto the dbml and call it like above.<br /></span></span><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=3aad13b9-1d61-40e2-a9aa-800e1432d053" /></body>
      <title>Useful SQL to get max from varchar column</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,3aad13b9-1d61-40e2-a9aa-800e1432d053.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2010/06/29/UsefulSQLToGetMaxFromVarcharColumn.aspx</link>
      <pubDate>Tue, 29 Jun 2010 13:26:15 GMT</pubDate>
      <description>If we have a varchar column as a Primary Key (don't ask - we also are not able to alter the schema) then we can still use Max to generate IDs programmatically.&lt;br&gt;
&lt;br&gt;
However, because the column is char, then SQL Server will determine that '9' is actually
greater than '1','0' [10] so we need to be a little bit smarter.&lt;br&gt;
&lt;br&gt;
Use this:&lt;br&gt;
&lt;br&gt;
&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial; font-size: 12px; line-height: 20px;"&gt;1.
DECLARE @Length INT&lt;br&gt;
2. SET @Length=(SELECT MAX(LEN(ID)) from test)&lt;br&gt;
3. SELECT TOP 1 ID FROM Test WHERE LEN(ID)=@Length ORDER BY ID DESC&lt;br&gt;
&lt;br&gt;
Thanks to &lt;/span&gt;&lt;/span&gt;&lt;a href="http://www.dotnetspider.com/forum/192995-Can-we-apply-Max-function-SQL-Query-for.aspx"&gt;http://www.dotnetspider.com/forum/192995-Can-we-apply-Max-function-SQL-Query-for.aspx&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
And if you want to call this using LINQ to SQL (or variant thereof), you could try:&lt;br&gt;
&lt;br&gt;
&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 11px;"&gt;CREATE
PROCEDURE web_GetMaxCampaignCode 
&lt;br&gt;
(@maxcode int output)&lt;br&gt;
AS&lt;br&gt;
BEGIN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; -- SET NOCOUNT ON added to prevent extra result sets from&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; -- interfering with SELECT statements.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SET NOCOUNT ON;&lt;br&gt;
&lt;br&gt;
&amp;nbsp; DECLARE @Length INT&lt;br&gt;
&amp;nbsp; SET @Length=(SELECT MAX(LEN(campaign_code)) from ref_campaign_code)&lt;br&gt;
&amp;nbsp; SET @maxcode = (SELECT TOP 1 campaign_code FROM ref_campaign_code WHERE LEN(campaign_code)=@Length
ORDER BY campaign_code DESC)&lt;br&gt;
END&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Calling Code:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; GetFirstName(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; id)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; (MyDataContext
dataContext &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; MyDataContext())
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; FirstName;
var query &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; dataContext.GetFirstName(id, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;ref&lt;/span&gt; FirstName); &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; FirstName;
} }&lt;/span&gt;&lt;/pre&gt;
Then just drag your sproc onto the dbml and call it like above.&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=3aad13b9-1d61-40e2-a9aa-800e1432d053" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,3aad13b9-1d61-40e2-a9aa-800e1432d053.aspx</comments>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Interesting real-world example of what
goes on under the hood of SQL Server the other day.<br /><br />
We have a routine that locks rows (using REPEATABLE READ) in a transaction that can
take some time to run (we simulated 5 minutes).<br /><br />
The same tables are accessed from a couple of websites, however as long as they weren't
accessing THOSE rows they would be fine to proceed as normal.  I know we're mixing
and matching our locking models here, but I can't see how it would work (nor do I
have the time to investigate any further!) if we were to use an optimistic locking
strategy for our update routine.  (As an aside, perhaps breaking it into chunks
an committing rather than a 5-minute long transaction would help I'm sure)<br /><br />
Anyway, we tested in isolation and all was well, however as soon as we try it with
the actual system then it fails-  the system stops responding.<br /><br />
Looking at the query plan, we saw the problem straight away - one of the queries being
run by the website was actually doing a table scan.<br /><br />
But why?  These tables were all indexed where the join was happening.<br /><br />
Turns out that SQL Server is clever enough to know when a Table Scan is more efficient
than a random access via an index.<br /><br />
Because our test data only had a handful of rows, this meant that a Table Scan happened
every time.  Running the same query on a database with 100000s of records and
the table scan disappears, replaced with an Index seek.<br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e" /></body>
      <title>Check out the brain on SQL Server's Query Optimizer</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2010/02/11/CheckOutTheBrainOnSQLServersQueryOptimizer.aspx</link>
      <pubDate>Thu, 11 Feb 2010 11:58:48 GMT</pubDate>
      <description>Interesting real-world example of what goes on under the hood of SQL Server the other day.&lt;br&gt;
&lt;br&gt;
We have a routine that locks rows (using REPEATABLE READ) in a transaction that can
take some time to run (we simulated 5 minutes).&lt;br&gt;
&lt;br&gt;
The same tables are accessed from a couple of websites, however as long as they weren't
accessing THOSE rows they would be fine to proceed as normal.&amp;nbsp; I know we're mixing
and matching our locking models here, but I can't see how it would work (nor do I
have the time to investigate any further!) if we were to use an optimistic locking
strategy for our update routine.&amp;nbsp; (As an aside, perhaps breaking it into chunks
an committing rather than a 5-minute long transaction would help I'm sure)&lt;br&gt;
&lt;br&gt;
Anyway, we tested in isolation and all was well, however as soon as we try it with
the actual system then it fails-&amp;nbsp; the system stops responding.&lt;br&gt;
&lt;br&gt;
Looking at the query plan, we saw the problem straight away - one of the queries being
run by the website was actually doing a table scan.&lt;br&gt;
&lt;br&gt;
But why?&amp;nbsp; These tables were all indexed where the join was happening.&lt;br&gt;
&lt;br&gt;
Turns out that SQL Server is clever enough to know when a Table Scan is more efficient
than a random access via an index.&lt;br&gt;
&lt;br&gt;
Because our test data only had a handful of rows, this meant that a Table Scan happened
every time.&amp;nbsp; Running the same query on a database with 100000s of records and
the table scan disappears, replaced with an Index seek.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,a5f4be40-d3c7-4baa-a3b3-c46d1a79c80e.aspx</comments>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=aa990360-7271-44a3-b951-75191ead311f</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,aa990360-7271-44a3-b951-75191ead311f.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,aa990360-7271-44a3-b951-75191ead311f.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=aa990360-7271-44a3-b951-75191ead311f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">If you ever get the above error, you can
restore from the query command line, via use of the REPLACE command.<br /><br />
This is illustrated here - <a href="http://blog.sqlauthority.com/2007/09/27/sql-server-fix-error-3154-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-database/">http://blog.sqlauthority.com/2007/09/27/sql-server-fix-error-3154-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-database/</a><br /><br />
I've copied this fix for my own reference:<br /><br /><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: verdana,tahoma,arial,sans-serif; font-size: 12px; line-height: 19px; text-align: left;"><p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;"><strong style="margin: 0px; padding: 0px;">Fix/WorkAround/Solution:</strong></p><p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;">
1) Use WITH REPLACE while using the RESTORE command.<span class="Apple-converted-space"> </span></p><p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;">
2) Delete the older database which is conflicting and restore again using RESTORE
command.
</p><p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;">
I understand my solution is little different than BOL but I use it to fix my database
issue successfully.
</p><p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;">
3) Sample Example :<br style="margin: 0px; padding: 0px;" /><code style="margin: 0px; padding: 0px; font-size: 12px;"><span style="margin: 0px; padding: 0px; color: blue;">RESTORE
DATABASE<span class="Apple-converted-space"> </span></span><span style="margin: 0px; padding: 0px; color: black;">AdventureWorks<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: blue;">FROM DISK =<span class="Apple-converted-space"> </span></span><span style="margin: 0px; padding: 0px; color: red;">'C:\BackupAdventureworks.bak'<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: blue;">WITH<span class="Apple-converted-space"> </span></span><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 255);">REPLACE</span></code></p></span></span><br />
Also, to REPLACE AND MOVE, use the following:<br /><br />
RESTORE DATABASE mydb<br />
FROM DISK = 'C:\Temp\data\mybak.bak'<br />
WITH REPLACE,<br />
MOVE 'original_dat' to 'C:\work\data\data2.mdf',<br />
MOVE 'original_log' to 'C:\work\data\data2.ldf'<br /><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=aa990360-7271-44a3-b951-75191ead311f" /></body>
      <title>SQL Server Error 3154: The backup set holds a backup of a database other than the existing database</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,aa990360-7271-44a3-b951-75191ead311f.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/11/17/SQLServerError3154TheBackupSetHoldsABackupOfADatabaseOtherThanTheExistingDatabase.aspx</link>
      <pubDate>Tue, 17 Nov 2009 12:04:07 GMT</pubDate>
      <description>If you ever get the above error, you can restore from the query command line, via use of the REPLACE command.&lt;br&gt;
&lt;br&gt;
This is illustrated here - &lt;a href="http://blog.sqlauthority.com/2007/09/27/sql-server-fix-error-3154-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-database/"&gt;http://blog.sqlauthority.com/2007/09/27/sql-server-fix-error-3154-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-database/&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
I've copied this fix for my own reference:&lt;br&gt;
&lt;br&gt;
&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: verdana,tahoma,arial,sans-serif; font-size: 12px; line-height: 19px; text-align: left;"&gt;
&lt;p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;"&gt;
&lt;strong style="margin: 0px; padding: 0px;"&gt;Fix/WorkAround/Solution:&lt;/strong&gt;
&lt;/p&gt;
&lt;p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;"&gt;
1) Use WITH REPLACE while using the RESTORE command.&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;"&gt;
2) Delete the older database which is conflicting and restore again using RESTORE
command.
&lt;/p&gt;
&lt;p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;"&gt;
I understand my solution is little different than BOL but I use it to fix my database
issue successfully.
&lt;/p&gt;
&lt;p style="margin: 0.7em 0px; padding: 0px; line-height: 1.6em; text-align: justify;"&gt;
3) Sample Example :&lt;br style="margin: 0px; padding: 0px;"&gt;
&lt;code style="margin: 0px; padding: 0px; font-size: 12px;"&gt;&lt;span style="margin: 0px; padding: 0px; color: blue;"&gt;RESTORE
DATABASE&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="margin: 0px; padding: 0px; color: black;"&gt;AdventureWorks&lt;br style="margin: 0px; padding: 0px;"&gt;
&lt;/span&gt;&lt;span style="margin: 0px; padding: 0px; color: blue;"&gt;FROM DISK =&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="margin: 0px; padding: 0px; color: red;"&gt;'C:\BackupAdventureworks.bak'&lt;br style="margin: 0px; padding: 0px;"&gt;
&lt;/span&gt;&lt;span style="margin: 0px; padding: 0px; color: blue;"&gt;WITH&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="margin: 0px; padding: 0px; color: rgb(255, 0, 255);"&gt;REPLACE&lt;/span&gt;&lt;/code&gt;
&lt;/p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;br&gt;
Also, to REPLACE AND MOVE, use the following:&lt;br&gt;
&lt;br&gt;
RESTORE DATABASE mydb&lt;br&gt;
FROM DISK = 'C:\Temp\data\mybak.bak'&lt;br&gt;
WITH REPLACE,&lt;br&gt;
MOVE 'original_dat' to 'C:\work\data\data2.mdf',&lt;br&gt;
MOVE 'original_log' to 'C:\work\data\data2.ldf'&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=aa990360-7271-44a3-b951-75191ead311f" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,aa990360-7271-44a3-b951-75191ead311f.aspx</comments>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=6c5414cb-afaf-4117-89fe-d66e2a0c8f76</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,6c5414cb-afaf-4117-89fe-d66e2a0c8f76.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,6c5414cb-afaf-4117-89fe-d66e2a0c8f76.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=6c5414cb-afaf-4117-89fe-d66e2a0c8f76</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">When you're creating an app that uses Linq
and you want to see the SQL that is generated, you can attach to the Log property
of the DataContext.<br /><br />
To get this to stream to the DebugWindow, use the following bit of code (credits to
Kris Vandermotten - <a href="http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11">http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11</a>)<br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">using</span> System.Diagnostics; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">using</span> System.Globalization; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">using</span> System.IO; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">using</span> System.Text; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">namespace</span> Vandermotten.Diagnostics
{ <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
Implements a &lt;see cref="TextWriter"/&gt; for writing information to the debugger
log.</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;/summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;seealso cref="Debugger.Log"/&gt;</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">class</span> DebuggerWriter
: TextWriter { <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">bool</span> isOpen; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">static</span> UnicodeEncoding
encoding; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">readonly</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> level; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">readonly</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> category; <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
Initializes a new instance of the &lt;see cref="DebuggerWriter"/&gt; class.</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;/summary&gt;</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span> DebuggerWriter()
: <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">this</span>(0,
Debugger.DefaultCategory) { } <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
Initializes a new instance of the &lt;see cref="DebuggerWriter"/&gt; class with the
specified level and category.</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;/summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;param name="level"&gt;A description of the importance of the messages.&lt;/param&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;param name="category"&gt;The category of the messages.&lt;/param&gt;</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span> DebuggerWriter(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> level, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> category)
: <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">this</span>(level,
category, CultureInfo.CurrentCulture) { } <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
Initializes a new instance of the &lt;see cref="DebuggerWriter"/&gt; class with the
specified level, category and format provider.</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;/summary&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;param name="level"&gt;A description of the importance of the messages.&lt;/param&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;param name="category"&gt;The category of the messages.&lt;/param&gt;</span><span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">///
&lt;param name="formatProvider"&gt;An &lt;see cref="IFormatProvider"/&gt; object that
controls formatting.&lt;/param&gt;</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span> DebuggerWriter(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> level, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> category,
IFormatProvider formatProvider) : <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">base</span>(formatProvider)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">this</span>.level <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> level; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">this</span>.category <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> category; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">this</span>.isOpen <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">true</span>;
} <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">void</span> Dispose(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">bool</span> disposing)
{ isOpen <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">false</span>; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">base</span>.Dispose(disposing);
} <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">void</span> Write(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">char</span> value)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">if</span> (!isOpen)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">throw</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">new</span> ObjectDisposedException(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">null</span>);
} Debugger.Log(level, category, value.ToString()); } <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">void</span> Write(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> value)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">if</span> (!isOpen)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">throw</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">new</span> ObjectDisposedException(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">null</span>);
} <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">if</span> (value
!<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">null</span>)
{ Debugger.Log(level, category, value); } } <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">void</span> Write(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">char</span>[]
buffer, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> index, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> count)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">if</span> (!isOpen)
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">throw</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">new</span> ObjectDisposedException(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">null</span>);
} <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">if</span> (buffer
== <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">null</span> ||
index &lt; 0 || count &lt; 0 || buffer.Length <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">-</span> index
&lt; count) { <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">base</span>.Write(buffer,
index, count); <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">//
delegate throw exception to base class</span> } Debugger.Log(level, category, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">new</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span>(buffer,
index, count)); } <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">override</span> Encoding
Encoding { get { <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">if</span> (encoding
== <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">null</span>)
{ encoding <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">new</span> UnicodeEncoding(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">false</span>, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">false</span>);
} <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">return</span> encoding;
} } <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">int</span> Level
{ get { <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">return</span> level;
} } <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">string</span> Category
{ get { <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">return</span> category;
} } } }</span></pre><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=6c5414cb-afaf-4117-89fe-d66e2a0c8f76" /></body>
      <title>Linq logs to Debug Window</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,6c5414cb-afaf-4117-89fe-d66e2a0c8f76.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/10/02/LinqLogsToDebugWindow.aspx</link>
      <pubDate>Fri, 02 Oct 2009 10:17:53 GMT</pubDate>
      <description>When you're creating an app that uses Linq and you want to see the SQL that is generated, you can attach to the Log property of the DataContext.&lt;br&gt;
&lt;br&gt;
To get this to stream to the DebugWindow, use the following bit of code (credits to
Kris Vandermotten - &lt;a href="http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11"&gt;http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11&lt;/a&gt;)&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Diagnostics; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Globalization; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.IO; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Text; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; Vandermotten.Diagnostics
{ &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
Implements a &amp;lt;see cref="TextWriter"/&amp;gt; for writing information to the debugger
log.&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;seealso cref="Debugger.Log"/&amp;gt;&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; DebuggerWriter
: TextWriter { &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; isOpen; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; UnicodeEncoding
encoding; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;readonly&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; level; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;readonly&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; category; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
Initializes a new instance of the &amp;lt;see cref="DebuggerWriter"/&amp;gt; class.&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; DebuggerWriter()
: &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;(0,
Debugger.DefaultCategory) { } &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
Initializes a new instance of the &amp;lt;see cref="DebuggerWriter"/&amp;gt; class with the
specified level and category.&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;param name="level"&amp;gt;A description of the importance of the messages.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;param name="category"&amp;gt;The category of the messages.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; DebuggerWriter(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; level, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; category)
: &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;(level,
category, CultureInfo.CurrentCulture) { } &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
Initializes a new instance of the &amp;lt;see cref="DebuggerWriter"/&amp;gt; class with the
specified level, category and format provider.&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;param name="level"&amp;gt;A description of the importance of the messages.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;param name="category"&amp;gt;The category of the messages.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;param name="formatProvider"&amp;gt;An &amp;lt;see cref="IFormatProvider"/&amp;gt; object that
controls formatting.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; DebuggerWriter(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; level, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; category,
IFormatProvider formatProvider) : &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;base&lt;/span&gt;(formatProvider)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.level &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; level; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.category &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; category; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.isOpen &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;true&lt;/span&gt;;
} &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Dispose(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; disposing)
{ isOpen &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;base&lt;/span&gt;.Dispose(disposing);
} &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Write(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;char&lt;/span&gt; value)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (!isOpen)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;throw&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; ObjectDisposedException(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;);
} Debugger.Log(level, category, value.ToString()); } &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Write(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; value)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (!isOpen)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;throw&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; ObjectDisposedException(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;);
} &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (value
!&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;)
{ Debugger.Log(level, category, value); } } &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Write(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;char&lt;/span&gt;[]
buffer, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; index, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; count)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (!isOpen)
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;throw&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; ObjectDisposedException(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;);
} &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (buffer
== &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt; ||
index &amp;lt; 0 || count &amp;lt; 0 || buffer.Length &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;-&lt;/span&gt; index
&amp;lt; count) { &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;base&lt;/span&gt;.Write(buffer,
index, count); &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;//
delegate throw exception to base class&lt;/span&gt; } Debugger.Log(level, category, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;(buffer,
index, count)); } &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; Encoding
Encoding { get { &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (encoding
== &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;)
{ encoding &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; UnicodeEncoding(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;);
} &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; encoding;
} } &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; Level
{ get { &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; level;
} } &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; Category
{ get { &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; category;
} } } }&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=6c5414cb-afaf-4117-89fe-d66e2a0c8f76" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,6c5414cb-afaf-4117-89fe-d66e2a0c8f76.aspx</comments>
      <category>c#</category>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=9a9ec5ba-3351-4591-bdf8-698de6c73594</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,9a9ec5ba-3351-4591-bdf8-698de6c73594.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,9a9ec5ba-3351-4591-bdf8-698de6c73594.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9a9ec5ba-3351-4591-bdf8-698de6c73594</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">On my recent blog post, I demonstrated
that I'd been able to submit the same LINQ query to both an in-memory repository and
a L2S Repo.<br /><br />
However, after discussing this on <a href="http://stackoverflow.com/questions/1052508/are-linq-to-sql-and-linq-to-objects-queries-the-same/1052593#1052593">StackOverflow</a>,
it seems that we must be careful.  While I have seen this technique demonstrated
in other articles, I've not found anything from an official perspective, so I will
probably err on the side of caution with this one.<br /><br />
While L2O and L2S may overlap on 90% of cases, that 10% is always going to be the
one to bite you when you don't have time to react.  So, it's back to a simpler
approach.<br /><br />
I'm still going to stick with the Repository pattern, but instead of exposing the
Repository as IQueryable&lt;T&gt; (even if the underlying source is IEnumerable&lt;T&gt;)
I'm going to encapsulate the actual queries within a Fetch(ICriteria) method instead. 
This worked well for me in a previous application, but it has the obvious drawback
that we have to define the queries differently for each Repository implementation.<br /><br />
However, it still allows us to abstract our DAL from our Application layer, as shown
in this simple diagram:<br /><br /><br /><p></p><img src="http://www.duncangunn.me.uk/dasblog/content/binary/Repo2.jpg" border="0" /><br /><br /><br />
So we can still use a Domain Driven approach to developing our app, which is great,
but we have to ensure that integration testing is thorough when we introduce our other
implemented repositories.<br /><br /><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=9a9ec5ba-3351-4591-bdf8-698de6c73594" /></body>
      <title>Linq To SQL Repositories - A word of Caution</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,9a9ec5ba-3351-4591-bdf8-698de6c73594.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/06/28/LinqToSQLRepositoriesAWordOfCaution.aspx</link>
      <pubDate>Sun, 28 Jun 2009 12:21:14 GMT</pubDate>
      <description>On my recent blog post, I demonstrated that I'd been able to submit the same LINQ query to both an in-memory repository and a L2S Repo.&lt;br&gt;
&lt;br&gt;
However, after discussing this on &lt;a href="http://stackoverflow.com/questions/1052508/are-linq-to-sql-and-linq-to-objects-queries-the-same/1052593#1052593"&gt;StackOverflow&lt;/a&gt;,
it seems that we must be careful.&amp;nbsp; While I have seen this technique demonstrated
in other articles, I've not found anything from an official perspective, so I will
probably err on the side of caution with this one.&lt;br&gt;
&lt;br&gt;
While L2O and L2S may overlap on 90% of cases, that 10% is always going to be the
one to bite you when you don't have time to react.&amp;nbsp; So, it's back to a simpler
approach.&lt;br&gt;
&lt;br&gt;
I'm still going to stick with the Repository pattern, but instead of exposing the
Repository as IQueryable&amp;lt;T&amp;gt; (even if the underlying source is IEnumerable&amp;lt;T&amp;gt;)
I'm going to encapsulate the actual queries within a Fetch(ICriteria) method instead.&amp;nbsp;
This worked well for me in a previous application, but it has the obvious drawback
that we have to define the queries differently for each Repository implementation.&lt;br&gt;
&lt;br&gt;
However, it still allows us to abstract our DAL from our Application layer, as shown
in this simple diagram:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.duncangunn.me.uk/dasblog/content/binary/Repo2.jpg" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
So we can still use a Domain Driven approach to developing our app, which is great,
but we have to ensure that integration testing is thorough when we introduce our other
implemented repositories.&lt;br&gt;
&lt;br&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=9a9ec5ba-3351-4591-bdf8-698de6c73594" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,9a9ec5ba-3351-4591-bdf8-698de6c73594.aspx</comments>
      <category>Database</category>
      <category>TDD</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=c1c5f6af-7dd8-45bd-af16-193e7d423465</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,c1c5f6af-7dd8-45bd-af16-193e7d423465.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,c1c5f6af-7dd8-45bd-af16-193e7d423465.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=c1c5f6af-7dd8-45bd-af16-193e7d423465</wfw:commentRss>
      <title>Mocking Linq To SQL repositories</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,c1c5f6af-7dd8-45bd-af16-193e7d423465.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/06/27/MockingLinqToSQLRepositories.aspx</link>
      <pubDate>Sat, 27 Jun 2009 09:59:11 GMT</pubDate>
      <description>I've started work on a new project recently and the first thing for me to sort out is the Data Access Layer.&amp;nbsp; Because the Repository pattern served me so well in the past, I'm going to start there.&lt;br&gt;
&lt;br&gt;
However, the previous solution used a bespoke QueryBuilder which converted an ICriteria
into the appropriate SQL.&amp;nbsp; This SQL was then just passed through as CommandText
on an ADO.NET connection to the appropriate database, Oracle or SQL Server.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
What we did was to mock the Repository using LINQ to Objects, and this allowed us
to construct our application in a Domain-Driven fashion.&lt;br&gt;
&lt;br&gt;
Great!&lt;br&gt;
&lt;br&gt;
However, there was always room for improvement.&amp;nbsp; I've wanted to use LINQ as my
ORM for quite some time now, and I know the purists out there will say that LINQ isn't
an ORM, but for practical purposes in this project it's going to be.&lt;br&gt;
&lt;br&gt;
Also, I've decided to use L2S rather than the Entity Framework.&amp;nbsp; My mantra is
Design For Test, and right now L2E is just not persistent-ignorant in the slightest.&amp;nbsp;
There are hacks as I've outlined in previous blog posts, but building hacks into the
foundation of your application is a recipe for disaster.&lt;br&gt;
&lt;br&gt;
&lt;a href="http://blogs.msdn.com/adonet/archive/2008/10/31/clarifying-the-message-on-l2s-futures.aspx"&gt;While
L2S may not be supported in future by MS&lt;/a&gt;, who cares?!&amp;nbsp; It works, plenty of
good, solid apps have been built on it, AND IF WE GET THINGS RIGHT, there's no reason
why we can't switch to L2E v2.0 next year.&lt;br&gt;
&lt;br&gt;
We can do this because we are going to abstract out the actual DataContext, and access
our data using POCO and Repository pattern.&lt;br&gt;
&lt;br&gt;
In addition, if we can swap out the DataContext, then given that LINQ to Objects and
LINQ to SQL queries are the same (or at least I hope they are, more on this later)
then we can have a set of queries that we can run on both in-memory and database sources.&amp;nbsp;
Truly Datasource-Agnostic.&lt;br&gt;
&lt;br&gt;
I got the inspiration for this from &lt;a href="http://compiledexperience.com/Blog/post/Domain-Driven-Design-Repositories-in-LINQ-to-SQL.aspx"&gt;http://compiledexperience.com/Blog/post/Domain-Driven-Design-Repositories-in-LINQ-to-SQL.aspx&lt;/a&gt; -
and it works!&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.duncangunn.me.uk/dasblog/content/binary/ClassDiagram1.png" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
This diagram shows the LinqToSQL assembly, but we can also replace the SQLDataSource
and SQLUnitOfWork with a MemoryDataSource and MemoryUnitOfWork.&lt;br&gt;
&lt;br&gt;
For example, if we want to test an in-memory collection, we do the following:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt; MemoryUnitOfWork
context &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; MemoryUnitOfWork();
Repository&amp;lt;Customer&amp;gt; r &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Repository&amp;lt;Customer&amp;gt;(context);
r.Save(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Customer(1, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Joe
Bloggs"&lt;/span&gt;)); r.Save(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Customer(2, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Joanna
Bloggs"&lt;/span&gt;)); IEnumerable&amp;lt;Customer&amp;gt; customers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; from
c &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; r.Fetch(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;)
where c.ID == 1 select c; List&amp;lt;Customer&amp;gt; results &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; customers.ToList&amp;lt;Customer&amp;gt;();
Assert.AreEqual(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Joe
Bloggs"&lt;/span&gt;, results[0].Name);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
And if we want to test an actual connection to a database using L2S:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt; DataContext
db &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; DataContext(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Data
Source=(local);Initial Catalog=DMGTest;Integrated Security=SSPI"&lt;/span&gt;); SqlUnitOfWork
context &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; SqlUnitOfWork(db);
Repository&amp;lt;Customer&amp;gt; r &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Repository&amp;lt;Customer&amp;gt;(context);
IEnumerable&amp;lt;Customer&amp;gt; customers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; from
c &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; r.Fetch(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;)
where c.ID == 1 select c; List&amp;lt;Customer&amp;gt; results &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; customers.ToList&amp;lt;Customer&amp;gt;();
Assert.AreEqual(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Joe
Bloggs"&lt;/span&gt;, results[0].Name);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
Note the line&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IEnumerable&amp;lt;Customer&amp;gt;
customers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; from
c &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; r.Fetch(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;)
where c.ID == 1 select c;&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
which is identical in both versions.&lt;br&gt;
&lt;br&gt;
My next step is to encapsulate the queries using the Specification pattern, &lt;a href="http://www.codeinsanity.com/2008/08/implementing-repository-and.html"&gt;as
Ritesh shows here&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
So now we have completely isolated our domain from our DAL, and with the introduction
of the Specification pattern we can look at our queries from a domain perspective
also.&lt;br&gt;
&lt;br&gt;
The code is just bashed out at the moment, but if you want it then just drop me a
line and I'll send it over.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.duncangunn.me.uk%2fdasblog%2f2009%2f06%2f27%2fMockingLinqToSQLRepositories.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.duncangunn.me.uk%2fdasblog%2f2009%2f06%2f27%2fMockingLinqToSQLRepositories.aspx&amp;bgcolor=0099CC" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=c1c5f6af-7dd8-45bd-af16-193e7d423465" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,c1c5f6af-7dd8-45bd-af16-193e7d423465.aspx</comments>
      <category>c#</category>
      <category>Database</category>
      <category>Patterns</category>
      <category>TDD</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=f214097a-f916-4c30-a2c6-f2dbc342acd3</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,f214097a-f916-4c30-a2c6-f2dbc342acd3.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,f214097a-f916-4c30-a2c6-f2dbc342acd3.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=f214097a-f916-4c30-a2c6-f2dbc342acd3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">OK, so I'm starting to get to grips a little
more with the Repository pattern.  Still got a long way to go, but it makes sense
and in true XP style I'm going to run with it.<br /><br />
The namespace/deployment overview is as follows:<br /><br /><br /><p></p><img src="http://www.duncangunn.me.uk/dasblog/content/binary/RepoOverview.jpg" border="0" /><br /><br /><br /><br />
I've hidden some of the extra features, such as the DTO namespace.  The BusinessObjects
encapsulate the 'raw' DTOs, which are used primarily in serializing the data as JSON
via the WebService.<br /><br />
I've also not shown that the RepositoryFactory (and subsequent Repos) could be used
directly without having to go via our WebService. 
<br /><br />
Remember that you can't just go to a BusinessObject and ask it to fetch itself - ALL
data access is managed via the Repository.<br /><br />
I've also kept the interface deliberately simple - I don't want to be introducing
LINQ goodness at that stage.  Everything is POCO. 
<br /><br />
You can find a related SO post here - http://stackoverflow.com/questions/741490/repository-pattern-pocos-or-iqueryable/741558#741558<br /><br /><br /><br /><br /><br /><br /><br /><br /><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=f214097a-f916-4c30-a2c6-f2dbc342acd3" /></body>
      <title>Packaging up the Repository</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,f214097a-f916-4c30-a2c6-f2dbc342acd3.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/04/12/PackagingUpTheRepository.aspx</link>
      <pubDate>Sun, 12 Apr 2009 10:27:40 GMT</pubDate>
      <description>OK, so I'm starting to get to grips a little more with the Repository pattern.&amp;nbsp; Still got a long way to go, but it makes sense and in true XP style I'm going to run with it.&lt;br&gt;
&lt;br&gt;
The namespace/deployment overview is as follows:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.duncangunn.me.uk/dasblog/content/binary/RepoOverview.jpg" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I've hidden some of the extra features, such as the DTO namespace.&amp;nbsp; The BusinessObjects
encapsulate the 'raw' DTOs, which are used primarily in serializing the data as JSON
via the WebService.&lt;br&gt;
&lt;br&gt;
I've also not shown that the RepositoryFactory (and subsequent Repos) could be used
directly without having to go via our WebService. 
&lt;br&gt;
&lt;br&gt;
Remember that you can't just go to a BusinessObject and ask it to fetch itself - ALL
data access is managed via the Repository.&lt;br&gt;
&lt;br&gt;
I've also kept the interface deliberately simple - I don't want to be introducing
LINQ goodness at that stage.&amp;nbsp; Everything is POCO. 
&lt;br&gt;
&lt;br&gt;
You can find a related SO post here - http://stackoverflow.com/questions/741490/repository-pattern-pocos-or-iqueryable/741558#741558&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=f214097a-f916-4c30-a2c6-f2dbc342acd3" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,f214097a-f916-4c30-a2c6-f2dbc342acd3.aspx</comments>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=9f64744a-f384-420e-a6ee-9127a0d87e95</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,9f64744a-f384-420e-a6ee-9127a0d87e95.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,9f64744a-f384-420e-a6ee-9127a0d87e95.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9f64744a-f384-420e-a6ee-9127a0d87e95</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Starting a new database project, the most
important thing for me is to design it for test.  In order to do that effectively,
we have to keep the business objects and the data access separate.<br /><br />
This concept is nothing new, but there's a lot of different ways to do that these
days.<br /><br />
The first idea I had was to have the data access encapsulated within a DataProvider
layer which would be interfaced directly by the application.   This would
create and return basic DTO objects for the application to play with, as there is
little to no business logic in the classes at this point.<br /><br />
Following discussions with my team, this morphed slightly into having the DTOs converted
into fully-fledged Business Objects, which would have knowledge of the DAL and access
the database in this way.<br /><br />
It seemed okay.... but then I got thinking about how to return a collection of objects,
and how all that worked.  So that brought me to the <a href="http://martinfowler.com/eaaCatalog/repository.html">Repository
Pattern</a>.<br /><br />
Simply put, the Repository Pattern abstracts the Data Access part from the Business
Objects, which means that to fetch an Object, one has to go via the Repo.<br /><br />
There are a number of ways to implement this - doing a search will bring back a LOT
of Linq-related implementations.<br /><br />
Right now, I'm going to keep it more in line with the original specification of this
pattern.  My Repository is simply going to return the Business Objects themselves. 
At the moment it is read only so there is no need for CUD - because of the way we
have separated this we can easily come back to this at a later date.<br /><br /><br /><p></p><img src="http://www.duncangunn.me.uk/dasblog/content/binary/Repository.jpg" border="0" /><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=9f64744a-f384-420e-a6ee-9127a0d87e95" /></body>
      <title>The Repository Pattern</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,9f64744a-f384-420e-a6ee-9127a0d87e95.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/04/11/TheRepositoryPattern.aspx</link>
      <pubDate>Sat, 11 Apr 2009 11:06:37 GMT</pubDate>
      <description>Starting a new database project, the most important thing for me is to design it for test.&amp;nbsp; In order to do that effectively, we have to keep the business objects and the data access separate.&lt;br&gt;
&lt;br&gt;
This concept is nothing new, but there's a lot of different ways to do that these
days.&lt;br&gt;
&lt;br&gt;
The first idea I had was to have the data access encapsulated within a DataProvider
layer which would be interfaced directly by the application.&amp;nbsp;&amp;nbsp; This would
create and return basic DTO objects for the application to play with, as there is
little to no business logic in the classes at this point.&lt;br&gt;
&lt;br&gt;
Following discussions with my team, this morphed slightly into having the DTOs converted
into fully-fledged Business Objects, which would have knowledge of the DAL and access
the database in this way.&lt;br&gt;
&lt;br&gt;
It seemed okay.... but then I got thinking about how to return a collection of objects,
and how all that worked.&amp;nbsp; So that brought me to the &lt;a href="http://martinfowler.com/eaaCatalog/repository.html"&gt;Repository
Pattern&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
Simply put, the Repository Pattern abstracts the Data Access part from the Business
Objects, which means that to fetch an Object, one has to go via the Repo.&lt;br&gt;
&lt;br&gt;
There are a number of ways to implement this - doing a search will bring back a LOT
of Linq-related implementations.&lt;br&gt;
&lt;br&gt;
Right now, I'm going to keep it more in line with the original specification of this
pattern.&amp;nbsp; My Repository is simply going to return the Business Objects themselves.&amp;nbsp;
At the moment it is read only so there is no need for CUD - because of the way we
have separated this we can easily come back to this at a later date.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.duncangunn.me.uk/dasblog/content/binary/Repository.jpg" border="0"&gt;&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=9f64744a-f384-420e-a6ee-9127a0d87e95" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,9f64744a-f384-420e-a6ee-9127a0d87e95.aspx</comments>
      <category>Database</category>
      <category>Patterns</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=f3a00fd9-59ba-4b34-8f16-8506e4aca22b</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,f3a00fd9-59ba-4b34-8f16-8506e4aca22b.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,f3a00fd9-59ba-4b34-8f16-8506e4aca22b.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=f3a00fd9-59ba-4b34-8f16-8506e4aca22b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I've been looking at the EF lately and
while I've still not settled on how it can be tested (given the lack of persistence
agnosticism), I do feel that MS has received enough <i>stick </i>from the community
that this will be a feature of v2.<br /><br />
The following article by <a href="http://johnpapa.net/">John Papa</a> provides some
good ideas as to how one would use EF as part of an n-tier architecture - <a href="http://msdn.microsoft.com/en-us/magazine/cc700340.aspx">http://msdn.microsoft.com/en-us/magazine/cc700340.aspx</a><br /><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=f3a00fd9-59ba-4b34-8f16-8506e4aca22b" /></body>
      <title>Entity Framework n-tier architecture</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,f3a00fd9-59ba-4b34-8f16-8506e4aca22b.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/02/04/EntityFrameworkNtierArchitecture.aspx</link>
      <pubDate>Wed, 04 Feb 2009 19:53:45 GMT</pubDate>
      <description>I've been looking at the EF lately and while I've still not settled on how it can be tested (given the lack of persistence agnosticism), I do feel that MS has received enough &lt;i&gt;stick &lt;/i&gt;from
the community that this will be a feature of v2.&lt;br&gt;
&lt;br&gt;
The following article by &lt;a href="http://johnpapa.net/"&gt;John Papa&lt;/a&gt; provides some
good ideas as to how one would use EF as part of an n-tier architecture - &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc700340.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/cc700340.aspx&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=f3a00fd9-59ba-4b34-8f16-8506e4aca22b" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,f3a00fd9-59ba-4b34-8f16-8506e4aca22b.aspx</comments>
      <category>c#</category>
      <category>Database</category>
      <category>Patterns</category>
      <category>Entity Framework</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=98c57b8f-a96d-457a-9ddd-404715cb9456</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,98c57b8f-a96d-457a-9ddd-404715cb9456.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,98c57b8f-a96d-457a-9ddd-404715cb9456.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=98c57b8f-a96d-457a-9ddd-404715cb9456</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have to give a special mention to this tool as it helps me generate scripts for
my Unit Tests really quickly.  It's free at the moment, although each version
has an expiration date, but it's well worth a look if you need something to quickly
generate scripts.
</p>
        <p>
          <a href="http://www.sqlscripter.com/">http://www.sqlscripter.com/</a>
        </p>
        <p>
It's also an example of <strong>REALLY GOOD USER INTERFACE DESIGN</strong>. 
The author has taken the time to predict what you're most likely to do and wrapped
up any repetitive tasks (for example logging in to the same database with the same
credentials, or scripting a certain subset of tables with the same criteria). 
You can tell it's a bit rough round the edges, but then it's not a commercial product
yet.
</p>
        <img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=98c57b8f-a96d-457a-9ddd-404715cb9456" />
      </body>
      <title>SQL Scripter</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,98c57b8f-a96d-457a-9ddd-404715cb9456.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2008/03/09/SQLScripter.aspx</link>
      <pubDate>Sun, 09 Mar 2008 20:51:02 GMT</pubDate>
      <description>&lt;p&gt;
I have to give a special mention to this tool as it helps me generate scripts for
my Unit Tests really quickly.&amp;nbsp; It's free at the moment, although each version
has an expiration date, but it's well worth a look if you need something to quickly
generate scripts.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.sqlscripter.com/"&gt;http://www.sqlscripter.com/&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
It's also an example of &lt;strong&gt;REALLY GOOD USER INTERFACE DESIGN&lt;/strong&gt;.&amp;nbsp;
The author has taken the time to predict what you're most likely to do and wrapped
up any repetitive tasks (for example logging in to the same database with the same
credentials, or scripting a certain subset of tables with the same criteria).&amp;nbsp;
You can tell it's a bit rough round the edges, but then it's not a commercial product
yet.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=98c57b8f-a96d-457a-9ddd-404715cb9456" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,98c57b8f-a96d-457a-9ddd-404715cb9456.aspx</comments>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=132a2f7f-3497-4517-b77a-88975798bee7</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,132a2f7f-3497-4517-b77a-88975798bee7.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,132a2f7f-3497-4517-b77a-88975798bee7.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=132a2f7f-3497-4517-b77a-88975798bee7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Unit Testing of database applications is a large topic, so I'll concentrate on a specific
scenario.
</p>
        <p>
If you have the following setup:
</p>
        <p>
          <strong>TestFixture</strong>
        </p>
        <ul>
          <li>
Init - populate database with default data 
</li>
          <li>
Teardown - run delete script to restore the database to it's previous state.</li>
        </ul>
        <p>
This will be sufficient if none of our tests modify the data in between.  But
we'll need to to test Create, Update and Delete methods of our business objects.
</p>
        <p>
So, if one Test updates the database, but another Test is run after it which is expecting
the database to be in it's original state (i.e. the state it's in straight after the
TestFixture.Init() method has been called)  then we have a problem.  But
we can't determine the order that the tests will run in, so what are our options?
</p>
        <p>
We could partition our tests into different TestFixtures, so avoiding the problem
altogether.  Or we could run the Populate and Delete scripts after EVERY test
- though this is likely to be slow and will probably become impractical as our test
library grows.
</p>
        <p>
A good solution would be to have each test run in isolation, where changes to the
database are rolled back after each test is run - this way the database can be updated
in one test but returned to it's original state before the next test runs.
</p>
        <p>
We can do this by using the <strong>TransactionScope</strong> object, like so:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">[Test] <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> TestSomething()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span>(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> TransactionScope())
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//Update
the database</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//Perform
Assertions</span> } }</span>
        </pre>
        <p>
If we need to do some common initialisation of the <strong>TransactionScope</strong> object,
for example setting <strong>IsolationLevel</strong> or <strong>Timeout</strong> properties,
then we can wrap this up in a class as follows:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> RollbackTransaction:
IDisposable     {         <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> TransactionScope
mScope;         <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> RollbackTransaction()
        {             TransactionOptions
tOptions <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> TransactionOptions();
            tOptions.IsolationLevel <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> IsolationLevel.RepeatableRead;
            tOptions.Timeout <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> TimeSpan(2,
0, 0);             mScope <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> TransactionScope(TransactionScopeOption.RequiresNew,
             tOptions);
        }         <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///&lt;summary&gt;</span>         <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///Performs
application-defined tasks associated with freeing, releasing, or resetting unmanaged
resources.</span>         <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///&lt;/summary&gt;</span>         <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///&lt;filterpriority&gt;2&lt;/filterpriority&gt;</span>         <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Dispose()
        {             mScope.Dispose();
        }     }</span>
        </pre>
        <p>
and instead of <strong>using(new TransactionScope()</strong>), we would instead have <strong>using(new
RollbackTransaction()).</strong></p>
        <p>
 
</p>
        <p>
 
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=132a2f7f-3497-4517-b77a-88975798bee7" />
      </body>
      <title>Database Unit Tests</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,132a2f7f-3497-4517-b77a-88975798bee7.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2008/02/27/DatabaseUnitTests.aspx</link>
      <pubDate>Wed, 27 Feb 2008 20:00:59 GMT</pubDate>
      <description>&lt;p&gt;
Unit Testing of database applications is a large topic, so I'll concentrate on a specific
scenario.
&lt;/p&gt;
&lt;p&gt;
If you have the following setup:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;TestFixture&lt;/strong&gt; 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Init - populate database with default data 
&lt;li&gt;
Teardown - run delete script to restore the database to it's previous state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
This will be sufficient if none of our tests modify the data in between.&amp;nbsp; But
we'll need to to test Create, Update and Delete methods of our business objects.
&lt;/p&gt;
&lt;p&gt;
So, if one Test updates the database, but another Test is run after it which is expecting
the database to be in it's original state (i.e. the state it's in straight after the
TestFixture.Init() method has been called)&amp;nbsp; then we have a problem.&amp;nbsp; But
we can't determine the order that the tests will run in, so what are our options?
&lt;/p&gt;
&lt;p&gt;
We could partition our tests into different TestFixtures, so avoiding the problem
altogether.&amp;nbsp; Or we could run the Populate and Delete scripts after EVERY test
- though this is likely to be slow and will probably become impractical as our test
library grows.
&lt;/p&gt;
&lt;p&gt;
A good solution would be to have each test run in isolation, where changes to the
database are rolled back after each test is run - this way the database can be updated
in one test but returned to it's original state before the next test runs.
&lt;/p&gt;
&lt;p&gt;
We can do this by using the &lt;strong&gt;TransactionScope&lt;/strong&gt; object, like so:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[Test] &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; TestSomething()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; TransactionScope())
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//Update
the database&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//Perform
Assertions&lt;/span&gt; } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
If we need to do some common initialisation of the &lt;strong&gt;TransactionScope&lt;/strong&gt; object,
for example setting &lt;strong&gt;IsolationLevel&lt;/strong&gt; or &lt;strong&gt;Timeout&lt;/strong&gt; properties,
then we can wrap this up in a class as follows:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; RollbackTransaction:
IDisposable &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; TransactionScope
mScope; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; RollbackTransaction()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TransactionOptions
tOptions &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; TransactionOptions();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tOptions.IsolationLevel &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; IsolationLevel.RepeatableRead;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tOptions.Timeout &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; TimeSpan(2,
0, 0); &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mScope &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; TransactionScope(TransactionScopeOption.RequiresNew,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tOptions);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///&amp;lt;summary&amp;gt;&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///Performs
application-defined tasks associated with freeing, releasing, or resetting unmanaged
resources.&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///&amp;lt;/summary&amp;gt;&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///&amp;lt;filterpriority&amp;gt;2&amp;lt;/filterpriority&amp;gt;&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Dispose()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mScope.Dispose();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
and instead of &lt;strong&gt;using(new TransactionScope()&lt;/strong&gt;), we would instead have &lt;strong&gt;using(new
RollbackTransaction()).&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=132a2f7f-3497-4517-b77a-88975798bee7" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,132a2f7f-3497-4517-b77a-88975798bee7.aspx</comments>
      <category>TDD</category>
      <category>Database</category>
    </item>
  </channel>
</rss>