<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Jarek Kowalski</title>
	
	<link>http://jkowalski.com</link>
	<description>c#:\dev\blog</description>
	<lastBuildDate>Sun, 19 Jun 2011 03:06:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/jkowalski" /><feedburner:info uri="jkowalski" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>NLog 2.0 Release Candidate is out!</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/ETAEaVU4p4Y/</link>
		<comments>http://jkowalski.com/2011/06/18/nlog-2-0-release-candidate-is-out/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 15:36:59 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NLog]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=68</guid>
		<description><![CDATA[It is my pleasure to announce that the Release Candidate (RC) of NLog 2.0 is now available for download. This is the last milestone before NLog 2.0 final release. Binary downloads are available on CodePlex Source code on GitHub Full release notes can be found here. Please download the release and report any problems you may find. Assuming [...]]]></description>
				<content:encoded><![CDATA[<p>It is my pleasure to announce that the Release Candidate (RC) of NLog 2.0 is now available for download. This is the last milestone before NLog 2.0 final release.</p>
<ul>
<li>Binary downloads are available on <a href="http://nlog.codeplex.com/releases/view/68535">CodePlex</a></li>
<li>Source code on <a href="http://github.com/jkowalski/NLog">GitHub</a></li>
<li>Full release notes can be found <a href="http://nlog-project.org/nlog-2-rc-release-notes">here</a>.</li>
</ul>
<p>Please download the release and report any problems you may find. Assuming no critical issues are found, NLog 2.0 will be released within a month from the RC release.</p>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/ETAEaVU4p4Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2011/06/18/nlog-2-0-release-candidate-is-out/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2011/06/18/nlog-2-0-release-candidate-is-out/</feedburner:origLink></item>
		<item>
		<title>NLog 2.0 Beta1 has been released</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/rLwoi-p6bGA/</link>
		<comments>http://jkowalski.com/2010/09/26/nlog-2-0-beta1-has-been-released/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 04:00:06 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=64</guid>
		<description><![CDATA[It is my pleasure to announce that NLog 2.0 Beta 1 has been released. NLog 2.0 release is focused on adding support for new platforms (Silverlight, .NET Framework 4), improving logging architecture and manageability and addressing most frequently reported user issues. The following platforms are supported in this release: .NET Framework 2.0 SP1 and above, [...]]]></description>
				<content:encoded><![CDATA[<p>It is my pleasure to announce that NLog 2.0 Beta 1 has been released. </p>
<p>NLog 2.0 release is focused on adding support for new platforms (Silverlight, .NET Framework 4), improving logging architecture and manageability and addressing most frequently reported user issues. </p>
<p>The following platforms are supported in this release:</p>
<ul>
<li>.NET Framework 2.0 SP1 and above, 3.5 (Client and Extended profiles), 4.0 (Client and Extended profiles) </li>
<li>Silverlight 2.0, 3.0, 4.0 </li>
<li>.NET Compact Framework 2.0, 3.5 </li>
<li>Mono 2.x profile </li>
</ul>
<p>Read full release notes:    <br /><a href="http://nlog-project.org/nlog2-beta1-release-notes">http://nlog-project.org/nlog2-beta1-release-notes</a></p>
<p>Download Beta 1 bits on CodePlex:    <br /><a href="http://nlog.codeplex.com/releases/view/52957">http://nlog.codeplex.com/releases/view/52957</a></p>
<p>Source code is available on GitHub:    <br /><a href="http://github.com/jkowalski/NLog/">http://github.com/jkowalski/NLog/</a></p>
<p>Bugs can be reported on CodePlex:    <br /><a href="http://nlog.codeplex.com/workitem/list/basic">http://nlog.codeplex.com/workitem/list/basic</a></p>
<p>Discuss on forum:    <br /><a href="http://nlog-project.org/forum">http://nlog-project.org/forum</a></p>
<p>Subscribe to NLog RSS feed:    <br /><a href="http://nlog-project.org/feed">http://nlog-project.org/feed</a></p>
<p>Follow @JarekKowalski on Twitter:   <br /><a href="http://twitter.com/JarekKowalski">http://twitter.com/JarekKowalski</a></p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/rLwoi-p6bGA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/09/26/nlog-2-0-beta1-has-been-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/09/26/nlog-2-0-beta1-has-been-released/</feedburner:origLink></item>
		<item>
		<title>EFProviderWrappers v1.02 is available</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/UWc3kI7XzzM/</link>
		<comments>http://jkowalski.com/2010/08/03/efproviderwrappers-v1-02-is-available/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 16:57:47 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=61</guid>
		<description><![CDATA[I&#8217;ve just updated EFProviderWrappers sample to v1.02. It&#8217;s a bugfix release, which fixes two bugs: NotImplementedException during SaveChanges() when saving more than 1 entity Non-query commands were sometimes cached which was causing update to fail The code can be downloaded from: http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve just updated EFProviderWrappers sample to v1.02. It&#8217;s a bugfix release, which fixes two bugs:</p>
<ul>
<li>NotImplementedException during SaveChanges() when saving more than 1 entity</li>
<li>Non-query commands were sometimes cached which was causing update to fail</li>
</ul>
<p>The code can be downloaded from:</p>
<p><a href="http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747">http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747</a></p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/UWc3kI7XzzM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/08/03/efproviderwrappers-v1-02-is-available/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/08/03/efproviderwrappers-v1-02-is-available/</feedburner:origLink></item>
		<item>
		<title>Sample Provider for Entity Framework 4.0 is available</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/WEcdG72lnp4/</link>
		<comments>http://jkowalski.com/2010/05/24/sample-provider-for-entity-framework-4-0-is-available/#comments</comments>
		<pubDate>Tue, 25 May 2010 02:21:11 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EFSampleProvider]]></category>
		<category><![CDATA[Providers]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=58</guid>
		<description><![CDATA[Today Entity Framework team released an update to Sample Provider for Entity Framework 4.0. The provider is intended to be an example for ADO.NET provider writers but can also be used and hacked upon by everyone interested in the inner workings of an EF provider. The sample can be downloaded from MSDN Code Gallery. It is licensed under [...]]]></description>
				<content:encoded><![CDATA[<p>Today Entity Framework team released an update to Sample Provider for Entity Framework 4.0. The provider is intended to be an example for ADO.NET provider writers but can also be used and hacked upon by everyone interested in the inner workings of an EF provider.</p>
<p>The sample can be downloaded from <a href="http://code.msdn.microsoft.com/EFSampleProvider/Release/ProjectReleases.aspx?ReleaseId=4406">MSDN Code Gallery</a>. It is licensed under Microsoft Public License (Ms-PL). <a href="http://code.msdn.com/EFQuerySamples">EFQuerySamples</a> includes additional examples of configuring and using the sample provider.</p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/WEcdG72lnp4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/05/24/sample-provider-for-entity-framework-4-0-is-available/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/05/24/sample-provider-for-entity-framework-4-0-is-available/</feedburner:origLink></item>
		<item>
		<title>Update to EFProviderWrappers is available</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/_Jq8vSmpr8k/</link>
		<comments>http://jkowalski.com/2010/05/24/update-to-efproviderwrappers-is-available/#comments</comments>
		<pubDate>Mon, 24 May 2010 16:57:57 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EFProviderWrappers]]></category>
		<category><![CDATA[Providers]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=57</guid>
		<description><![CDATA[I&#8217;ve just posted a tiny update to EFProviderWrappers sample. It fixes the issue which was reported to me by several users, where NotSupportedException was being thrown during SaveChanges() from caching provider code.   You can download updated bits from MSDN Code Gallery. If you find any other issues, please report them as comments here.]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve just posted a tiny update to <a href="http://code.msdn.microsoft.com/EFProviderWrappers">EFProviderWrappers sample</a>. It fixes the issue which was reported to me by several users, where NotSupportedException was being thrown during SaveChanges() from caching provider code.  </p>
<p>You can download updated bits from <a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=EFProviderWrappers&amp;ReleaseId=4405">MSDN Code Gallery</a>. If you find any other issues, please report them as comments here.</p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/_Jq8vSmpr8k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/05/24/update-to-efproviderwrappers-is-available/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/05/24/update-to-efproviderwrappers-is-available/</feedburner:origLink></item>
		<item>
		<title>Attaching LINQ expression trees to delegates</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/8LOxG8eEYwI/</link>
		<comments>http://jkowalski.com/2010/05/18/attaching-linq-expression-trees-to-delegates/#comments</comments>
		<pubDate>Wed, 19 May 2010 03:32:03 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=53</guid>
		<description><![CDATA[Inspired by discussion on Twitter today, I started exploring the possibility of attaching LINQ expression trees to compiled delegates. The idea is that you could pass around Func&#60;T&#62; instead of Expression&#60;Func&#60;T&#62;&#62; yet still be able to retrieve original expression tree used to compile the delegate. This makes some scenarios (testing, logging) much cleaner, because you [...]]]></description>
				<content:encoded><![CDATA[<p>Inspired by discussion on Twitter today, I started exploring the possibility of attaching LINQ expression trees to compiled delegates. The idea is that you could pass around Func&lt;T&gt; instead of Expression&lt;Func&lt;T&gt;&gt; yet still be able to retrieve original expression tree used to compile the delegate. This makes some scenarios (testing, logging) much cleaner, because you don’t need to deal with expressions as much.</p>
<p>But how do you attach anything to a delegate and Func&lt;T&gt; in particular? Aren’t delegates essentially function pointers?</p>
<p>Well – in .NET each delegate holds information about both the target object instance and the method to invoke on it. This makes it possible to create a tiny wrapper object that would have a function which behaves like Func&lt;T&gt; but holds additional state. Let’s look at a simple wrapper for Func&lt;T&gt; which stores expression:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FuncWithExpression&lt;TResult&gt;
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> Func&lt;TResult&gt; wrappedFunction;

    <span class="kwrd">public</span> FuncWithExpression(Expression&lt;Func&lt;TResult&gt;&gt; expression, Func&lt;TResult&gt; func)
    {
        <span class="kwrd">this</span>.Expression = expression;
        <span class="kwrd">this</span>.wrappedFunction = func;
    }

    <span class="kwrd">public</span> TResult Function()
    {
        <span class="kwrd">return</span> wrappedFunction();
    }

    <span class="kwrd">public</span> Expression&lt;Func&lt;TResult&gt;&gt; Expression { get; <span class="kwrd">private</span> set; }
}</pre>
<p>Let us also define two of extensions methods: one which compiles Expression&lt;Func&lt;T&gt;&gt; and wraps the result with FuncWithExpression&lt;T&gt; wrapper and the other one which extracts the expression from Func&lt;T&gt;:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> ExtensionMethods
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> Func&lt;T&gt; CompileAndWrap&lt;T&gt;(<span class="kwrd">this</span> Expression&lt;Func&lt;T&gt;&gt; expression)
    {
        Func&lt;T&gt; compiledExpression = expression.Compile();
        <span class="kwrd">return</span> <span class="kwrd">new</span> FuncWithExpression&lt;T&gt;(expression, compiledExpression).Function;
    }

    <span class="kwrd">public</span> <span class="kwrd">static</span> Expression&lt;Func&lt;T&gt;&gt; GetExpression&lt;T&gt;(<span class="kwrd">this</span> Func&lt;T&gt; func)
    {
        var exp = func.Target <span class="kwrd">as</span> FuncWithExpression&lt;T&gt;;
        <span class="kwrd">if</span> (exp != <span class="kwrd">null</span>)
        {
            <span class="kwrd">return</span> exp.Expression;
        }

        <span class="kwrd">return</span> <span class="kwrd">null</span>;
    }
}</pre>
<p>That’s it! (Of course, in real life you would need to provide overloads for more Func&lt;&gt; types, since functions without arguments are not typically very useful). You can now write the code using just Func&lt;T&gt; and still be able to retrieve original expression tree used to compile the delegate:</p>
<pre class="csharpcode"><span class="kwrd">class</span> Program
{
    <span class="kwrd">static</span> <span class="kwrd">void</span> PrintAndExecute(Func&lt;<span class="kwrd">int</span>&gt; func)
    {
        Expression originalExpression = func.GetExpression();
        <span class="kwrd">if</span> (originalExpression != <span class="kwrd">null</span>)
        {
            Console.WriteLine(<span class="str">"expression: {0}"</span>, originalExpression);
        }

        Console.WriteLine(<span class="str">"result: {0}"</span>, func());
    }

    <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
    {
        var body = Expression.Add(Expression.Constant(3), Expression.Constant(9));
        Expression&lt;Func&lt;<span class="kwrd">int</span>&gt;&gt; expr = Expression.Lambda&lt;Func&lt;<span class="kwrd">int</span>&gt;&gt;(body);
        Func&lt;<span class="kwrd">int</span>&gt; compiledExpression = expr.CompileAndWrap();

        PrintAndExecute(compiledExpression);
    }
}</pre>
<p>This will print:</p>
<p><a href="http://jkowalski.com/wp-content/uploads/2010/05/image.png"><img style="display: inline; border: 0px;" title="image" src="http://jkowalski.com/wp-content/uploads/2010/05/image_thumb.png" border="0" alt="image" width="575" height="335" /></a></p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/8LOxG8eEYwI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/05/18/attaching-linq-expression-trees-to-delegates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/05/18/attaching-linq-expression-trees-to-delegates/</feedburner:origLink></item>
		<item>
		<title>Improvements in generated SQL in EF v4.0</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/KKpT05FCzR4/</link>
		<comments>http://jkowalski.com/2010/05/11/improvements-in-generated-sql-in-ef-v4-0/#comments</comments>
		<pubDate>Tue, 11 May 2010 20:42:04 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Providers]]></category>

		<guid isPermaLink="false">http://jkowalski.com/?p=52</guid>
		<description><![CDATA[Kati from EntityFramework team has published a detailed post about improvments in generated SQL in Entity Framework v4.0. The improvements are really impressive and make generated SQL perform better and and look much closer to hand-written query code. The best thing is: almost all of the improvements are provider-agnostic, so you will see automatic improvement [...]]]></description>
				<content:encoded><![CDATA[<p>Kati from EntityFramework team has published a detailed post about improvments in generated SQL in Entity Framework v4.0. The improvements are really impressive and make generated SQL perform better and and look much closer to hand-written query code. The best thing is: almost all of the improvements are provider-agnostic, so you will see automatic improvement on queries running on 3rd-party providers. In some cases (LIKE optimization) you will need to use EF4-specific version of the provider.</p>
<p>Read more on <a href="http://blogs.msdn.com/adonet/archive/2010/05/10/improvements-to-generated-sql-in-net-4-0.aspx">http://blogs.msdn.com/adonet/archive/2010/05/10/improvements-to-generated-sql-in-net-4-0.aspx</a></p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/KKpT05FCzR4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/05/11/improvements-in-generated-sql-in-ef-v4-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/05/11/improvements-in-generated-sql-in-ef-v4-0/</feedburner:origLink></item>
		<item>
		<title>Logging SQL statements in Entity Framework/Code First</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/BStIQHeDbp4/</link>
		<comments>http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 05:47:06 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EFProviderWrappers]]></category>

		<guid isPermaLink="false">http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/</guid>
		<description><![CDATA[Entity Framework/Code First feature released as part of Feature CTP 3 can work with any EF-enabled data provider. In addition to regular providers which target databases, it is possible to use wrapping providers which can add interesting functionality, such as caching and tracing. In this post I’m going to explain how to use EFTracingProvider to [...]]]></description>
				<content:encoded><![CDATA[<p>Entity Framework/Code First feature released as part of <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&amp;displayLang=en">Feature CTP 3</a> can work with any EF-enabled data provider.</p>
<p>In addition to regular providers which target databases, it is possible to use <a href="http://code.msdn.com/EFProviderWrappers">wrapping providers</a> which can add interesting functionality, such as caching and tracing. In this post I’m going to explain how to use <a href="http://jkowalski.com/2009/06/11/tracing-and-caching-for-entity-framework-available-on-msdn-code-gallery/">EFTracingProvider</a> to produce diagnostic trace of all SQL commands executed by EF in Code First.</p>
<h3>Setting up EFTracingConnection</h3>
<p>In order to use the tracing provider, we need to create a wrapping DbConnection which adds logging every time the command has finished execution. The following helper will set up such connection. As you can see it can log commands to the console, log file or both. Plugging in additional logging mechanisms (such as <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx">System.Diagnostics</a>, <a href="http://nlog-project.org/">NLog</a> or <a href="http://logging.apache.org/log4net/">log4net</a>) should be trivial.</p>
<pre class="csharpcode"><span class="kwrd">private</span> EFTracingConnection CreateTracingConnection()
{
    var connection = <span class="kwrd">new</span> EFTracingConnection
    {
        ConnectionString = <span class="str">@&quot;wrappedProvider=&quot;</span> + <span class="kwrd">this</span>.ProviderInvariantName + <span class="str">&quot;;&quot;</span> + <span class="kwrd">this</span>.ConnectionString
    };

    <span class="rem">// set up an event which will be raised whenever command has finished executing</span>
    connection.CommandFinished += (sender, e) =&gt;
    {
        <span class="kwrd">try</span>
        {
            <span class="kwrd">if</span> (<span class="kwrd">this</span>.LogToConsole)
            {
                Console.WriteLine(e.ToTraceString());
            }

            <span class="kwrd">if</span> (!<span class="kwrd">string</span>.IsNullOrEmpty(<span class="kwrd">this</span>.LogFileName))
            {
                File.AppendAllText(<span class="kwrd">this</span>.LogFileName, e.ToTraceString() + Environment.NewLine + Environment.NewLine);
            }
        }
        <span class="kwrd">catch</span>
        {
            <span class="rem">// catch all exceptions so that we don't pass logging-related failures to user code</span>
        }
    };

    <span class="kwrd">return</span> connection;
}</pre>
<h3>Object Context Factory</h3>
<p>In order to efficiently manage tracing for the application we need to create a central factory class which will create ObjectContext instances for us. This is the place where we will create tracing provider connection and use it to instantiate ObjectContext.</p>
<p>Assuming our Object Context class is called MyContainer, the factory class will be called MyContainerFactory and will have a method called CreateContext, so the usage becomes:</p>
<pre class="csharpcode">MyContainerFactory factory = ...;

<span class="kwrd">using</span> (MyContainer context = factory.CreateContext())
{
    <span class="rem">// use context normally</span>
}</pre>
<p>The factory will typically be be long-lived, created at the program startup and stored in a global variable or application state. </p>
<p>Having CreateTracingConnection(), we can now define our factory method by passing instance of the tracing connection to ContextBuilder&lt;T&gt;.Create():</p>
<pre class="csharpcode"><span class="rem">/// &lt;summary&gt;</span>
<span class="rem">/// Creates instance of &lt;see cref=&quot;MyContainer&quot;/&gt; with tracing enabled.</span>
<span class="rem">/// &lt;/summary&gt;</span>
<span class="rem">/// &lt;returns&gt;Instance of &lt;see cref=&quot;MyContainer&quot;/&gt;.&lt;/returns&gt;</span>
<span class="kwrd">public</span> MyContainer CreateContext()
{
    <span class="kwrd">return</span> contextBuilder.Create(<span class="kwrd">this</span>.CreateTracingConnection());
}</pre>
<p>Simple, isn’t it?</p>
<h3>Sample Project</h3>
<p>I’ve created a tiny (100 lines of code) sample project which demonstrates this technique. <a href="http://jkowalski.com/public/CodeFirstTracingDemo.zip">Click here to download it</a></p>
<p>In the spirit of <strong>Code Only</strong> the project does not have any non-source artifacts (not even App.config file) and configures everything (model, mapping, tracing) through code:</p>
<p><a href="http://jkowalski.com/wp-content/uploads/2010/04/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jkowalski.com/wp-content/uploads/2010/04/image_thumb.png" width="306" height="194" /></a> </p>
<p>Here is the output you get when running the application – as you can see all statements are logged to the console – log file is also created with similar output.</p>
<p><a href="http://jkowalski.com/wp-content/uploads/2010/04/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jkowalski.com/wp-content/uploads/2010/04/image_thumb1.png" width="439" height="484" /></a> </p>
<h3>Limitations</h3>
<p>There is a known issue with this technique where creating databases is not supported on SqlClient provider. Other providers may or may not support this functionality depending on implementation. In general, because of that it is recommended to use unwrapped connections when using DDL APIs (CreateDatabase, DeleteDatabase, DatabaseExists()) as demonstrated in the sample.</p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/BStIQHeDbp4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/</feedburner:origLink></item>
		<item>
		<title>New version of EFProviderWrappers released</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/ImXAFUsbHjQ/</link>
		<comments>http://jkowalski.com/2010/04/15/new-version-of-efproviderwrappers-released/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 00:42:59 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EFProviderWrappers]]></category>
		<category><![CDATA[Providers]]></category>

		<guid isPermaLink="false">http://jkowalski.com/2010/04/15/new-version-of-efproviderwrappers-released/</guid>
		<description><![CDATA[EFProviderWrappers sample for Entity Framework has been updated to support new features in EFv4 and Visual Studio 2010: Changes in this release since 3.5SP1 version: Upgraded all projects to .NET 4.0 and retargetted assemblies to use .NET Framework 4 Client Profile Removed reflection-based code to invoke CreateCommandDefinition() Moved AspNetCache to a separate assembly (which depends [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://code.msdn.com/EFProviderWrappers">EFProviderWrappers</a> sample for Entity Framework has been updated to support new features in EFv4 and Visual Studio 2010:</p>
<p>Changes in this release since 3.5SP1 version:</p>
<ol>
<li>Upgraded all projects to .NET 4.0 and retargetted assemblies to use .NET Framework 4 Client Profile</li>
<li>Removed reflection-based code to invoke CreateCommandDefinition()</li>
<li>Moved AspNetCache to a separate assembly (which depends on .NET Framework 4 Extended Profile)</li>
<li>Added wrappers for new provider API surface of DbProviderManifest class:
<ul>
<li>bool SupportsEscapingLikeArgument(out char escapeCharacter)</li>
<li>string EscapeLikeArgument(string argument)</li>
</ul>
</li>
<li>Added wrappers for new provider API surface of DbProviderServices class:
<ul>
<li>void DbCreateDatabase(DbConnection connection, int? commandTimeout, StoreItemCollection storeItemCollection);</li>
<li>bool DbDatabaseExists(DbConnection connection, int? commandTimeout, StoreItemCollection storeItemCollection);</li>
<li>void DbDeleteDatabase(DbConnection connection, int? commandTimeout, StoreItemCollection storeItemCollection);</li>
<li>string DbCreateDatabaseScript(string providerManifestToken, StoreItemCollection storeItemCollection);</li>
</ul>
</li>
<li>Wrappers now work in partial trust &#8211; check out AspNetCachingDemo</li>
<li>Removed Velocity adapter and demo.</li>
<li>Recreated sample models using edmx (instead of explicit csdl/ssdl/msl)</li>
<li>Removed the need to explicitly install the database in SQL Server &#8211; instead using |DataDirectory| and mdf files local to the project.</li>
</ol>
<p>The sample can be downloaded from <a href="http://code.msdn.com/EFProviderWrappers">MSDN Code Gallery</a>.</p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/ImXAFUsbHjQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/04/15/new-version-of-efproviderwrappers-released/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/04/15/new-version-of-efproviderwrappers-released/</feedburner:origLink></item>
		<item>
		<title>Migrating from EF POCO Adapter to Entity Framework v4.0</title>
		<link>http://feedproxy.google.com/~r/jkowalski/~3/U11hw3tr1os/</link>
		<comments>http://jkowalski.com/2010/04/14/migrating-from-ef-poco-adapter-to-entity-framework-v4-0/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 16:13:07 +0000</pubDate>
		<dc:creator>Jarek Kowalski</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EFPocoAdapter]]></category>

		<guid isPermaLink="false">http://jkowalski.com/2010/04/14/migrating-from-ef-poco-adapter-to-entity-framework-v4-0/</guid>
		<description><![CDATA[Now that Visual Studio 2010 and .NET Framework 4.0 have been released, users of Entity Framework can use POCO objects with Entity Framework without the need for wrapper layers such as EFPocoAdapter. Since I know a number of people are using EFPocoAdapter in their production applications and it is not supported, I recommend they migrate [...]]]></description>
				<content:encoded><![CDATA[</p>
<p>Now that Visual Studio 2010 and .NET Framework 4.0 have been released, users of Entity Framework can use POCO objects with Entity Framework without the need for wrapper layers such as <a href="http://code.msdn.com/EFPocoAdapter">EFPocoAdapter</a>. Since I know a number of people are using EFPocoAdapter in their production applications and it is not supported, I recommend they migrate to using native POCO support in EF v4.0 which is the supported way of using POCOs from now on. </p>
<p>This post will highlight the differences between EF and EFPOCOAdapter, which you must keep in mind when migrating your code base. </p>
</p>
<p> <span id="more-42"></span>
</p>
<p><b>Note</b> that this list is not guaranteed to be exhaustive and does not imply that there are no other differences.</p>
<h3><strong>Change tracking differences</strong></h3>
<p>Entity Framework v4.0 supports 3 different &quot;flavors&quot; of POCO objects:</p>
<ul>
<li>Basic (plain) POCOs </li>
<li>Lazy loading POCO proxies (non-sealed class, with default constructor and all navigation properties marked as virtual and overrideable) </li>
<li>Change tracking POCO proxies (non-sealed class, with default constructor, all properties marked as virtual and overrideable and all collections using ICollection&lt;T&gt; types) </li>
</ul>
<p>EF will enable particular feature (such as lazy loading and change tracking) if all required conditions have been met and there is no mechanism to explicitly request a particular type of behavior for each property. If the conditions have not been met, EF will fall back to plain POCO behavior.</p>
<p>EFPocoAdapter provides more fine-grained approach &#8211; you can set a change tracking mode (Snapshot, Proxy, etc.) for each property individually in CSDL by using objectmapping:ChangeTracking attribute. This is not supported by EF &#8211; the user needs to choose between one of the three flavors of POCO and mark properties of the entity accordingly. This ability to explicitly specify a tracking mode for every property might be implemented in future versions of EF.</p>
<p>From the performance standpoint, using change tracking-enabled objects is recommended, but doing so is not always possible, especially when you have code that expects concrete POCO types to be in use (such as WCF serialization). To learn more about this issue and possible workarounds check out this blog post: <a href="http://blogs.msdn.com/adonet/archive/2010/01/05/poco-proxies-part-2-serializing-poco-proxies.aspx">http://blogs.msdn.com/adonet/archive/2010/01/05/poco-proxies-part-2-serializing-poco-proxies.aspx</a></p>
<h3><strong>Value objects</strong></h3>
<p>EFPocoAdapter supports objects which are immutable (can only be constructed, but not modified) and semi-mutable objects when certain properties are read-only and must be initialized in the constructor while some are read-write. Below is an example of an immutable value object:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Category   
{   
    <span class="kwrd">public</span> Category(<span class="kwrd">int</span> categoryID, <span class="kwrd">string</span> categoryName)   
    {   
        <span class="kwrd">this</span>.CategoryID = categoryID;   
        <span class="kwrd">this</span>.CategoryName = categoryName;   
    }   
  
    <span class="kwrd">public</span> <span class="kwrd">int</span> CategoryID { get; <span class="kwrd">private</span> set; }   
    <span class="kwrd">public</span> <span class="kwrd">string</span> CategoryName { get; <span class="kwrd">private</span> set; }   
}</pre>
<p>EF v4.0 does not allow this directly (it always requires a default constructor without parameters and property setters to be there), but you can persist value objects and read them back by having setters which are non-public (EF will still be able to access them in full trust, but your application will not). You just need to add a non-public default constructor, as in the following example:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Category
{
    <span class="kwrd">private</span> Category()
    {
        <span class="rem">// this constructor will be called by EF</span>
    }

    <span class="kwrd">public</span> Category(<span class="kwrd">int</span> categoryID, <span class="kwrd">string</span> categoryName)
    {
        <span class="kwrd">this</span>.CategoryID = categoryID;
        <span class="kwrd">this</span>.CategoryName = categoryName;
    }

    <span class="kwrd">public</span> <span class="kwrd">int</span> CategoryID { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> <span class="kwrd">string</span> CategoryName { get; <span class="kwrd">private</span> set; }
}</pre>
<p>This approach has some downsides, because there is no validation happening when object is hydrated from the database (as it is bypassing the construction logic), but you may argue that this is not so bad since the object must have been validated before it was persisted.</p>
<p>Also note that you will need to switch from structures (C# struct) as a representation for value objects to classes, because the former are not supported in EFv4.</p>
<h3><strong>Lazy Loading</strong></h3>
<p>There are differences in the way lazy loading is implemented. In EF v4.0, collection properties with lazy loading need to be virtual, in POCO adapter they do not but it they have to be ICollection&lt;T&gt; or IList&lt;T&gt;. EFv4 supports lazy loading of concrete collection types (such as List&lt;T&gt;) while POCO adapter does not.</p>
<p>This is an example of a class which has Products property which can be lazy-loaded in Entity Framework:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Category
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> CategoryID { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">string</span> CategoryName { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> List&lt;Product&gt; Products { get; set; }
}</pre>
<p>Lazy-loaded properties of type ICollection&lt;T&gt; need to be marked as virtual to get lazy loading</p>
<h4><strong>Shadow state</strong></h4>
<p>Because of the way POCO adapter is implemented, it is also possible do omit certain properties (such as auto-generated GUIDs, etc.) from the POCO model but still have them in CSDL model. This is not something that’s possible in EFv4, so you need to add the properties to your domain classes. As in the case of value objects, properties can be private, but EF needs to be able to see them in order to load your model.</p>
<h4><strong>API Differences</strong></h4>
<p>The following table lists EF POCO Adapter classes and their EF v4.0 equivalents:</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="200"><strong>POCO Adapter Type</strong></td>
<td valign="top" width="200"><strong>Entity Framework v4.0 Type</strong></td>
</tr>
<tr>
<td valign="top" width="200">EFPocoContext</td>
<td valign="top" width="200">ObjectContext</td>
</tr>
<tr>
<td valign="top" width="200">EFPocoQuery&lt;T&gt;</td>
<td valign="top" width="200">ObjectQuery&lt;T&gt;</td>
</tr>
<tr>
<td valign="top" width="200">IEntitySet&lt;T&gt;</td>
<td valign="top" width="200">IObjectSet&lt;T&gt;</td>
</tr>
<tr>
<td valign="top" width="200">PocoEntitySet&lt;T&gt;</td>
<td valign="top" width="200">ObjectSet&lt;T&gt;</td>
</tr>
<tr>
<td valign="top" width="200">EFCompiledQuery</td>
<td valign="top" width="200">CompiledQuery</td>
</tr>
</tbody>
</table>
<p>Method names of POCO adapter classes typically correspond to method names in Entity Framework v4.0, but since POCO adapter was created at the beginning of EFv4.0 there can be some differences you will need to fix during the migration. Since POCO adapter is generally a subset of EF you should be able to easily find corresponding methods and properties, except:</p>
<ul>
<li>EFPocoContext.EnableLazyLoading – use ContextOptions.LazyLoadingEnabled </li>
<li>EFPocoContext.ConvertProxiesToPoco() and EFPocoContext.ConvertPocoToProxies() </li>
<li>EFPocoContext.IsProxy() </li>
</ul>
<p>I’m hoping these hints will be helpful to folks porting their apps to use POCO support in Entity Framework 4.0. If you have any further questions, feel free to ask them here.</p>
<img src="http://feeds.feedburner.com/~r/jkowalski/~4/U11hw3tr1os" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jkowalski.com/2010/04/14/migrating-from-ef-poco-adapter-to-entity-framework-v4-0/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://jkowalski.com/2010/04/14/migrating-from-ef-poco-adapter-to-entity-framework-v4-0/</feedburner:origLink></item>
	</channel>
</rss>
