<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Static Void</title>
  <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/" />
  <link rel="self" href="http://martinwilley.com/blog/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2013-06-13T00:53:11.1062812-07:00</updated>
  <author>
    <name>Martin Willey</name>
  </author>
  <subtitle>What next?</subtitle>
  <id>http://martinwilley.com/blog/</id>
  <generator uri="http://dasblog.info/" version="2.3.9074.18820">DasBlog</generator>
  <entry>
    <title>Nuget vs jQuery 2 </title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2013/06/13/NugetVsJQuery2.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,cf74c266-4c1e-40f2-8698-515124710e86.aspx</id>
    <published>2013-06-13T00:53:11.1062812-07:00</published>
    <updated>2013-06-13T00:53:11.1062812-07:00</updated>
    <category term="VS2012" label="VS2012" scheme="http://martinwilley.com/blog/CategoryView,category,VS2012.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">The <a href="http://jquery.com/download/">jQuery
2</a> branch does not support IE 6,7 or 8. Unless the site is exclusively targeted
at mobile, or you have a very small and up-to-date audience, everyone should still
use the jQuery 1.9+ branch.<br /><br />
Nuget insists that you want to update to jQuery 2.x<br /><br />
Doh.<br /><br />
The package should not have been updated from 1.x to 2.x. There should have been a
separate package for jQuery 2, so .net websites continue to update on the 1.x branch.<br /><br />
There is a workaround.<br /><br />
You must manually change the packages.config in the project. Add a range of allowed
versions:<br />
 <br /><pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">package</span><span style="color:blue;"> </span><span style="color:red;">id</span><span style="color:blue;">=</span>"<span style="color:blue;">jQuery</span>"<span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>"<span style="color:blue;">1.10.1</span>"<span style="color:blue;"> </span><span style="color:red;">targetFramework</span><span style="color:blue;">=</span>"<span style="color:blue;">net45</span>"<span style="color:blue;"> </span><span style="color:red;">allowedVersions</span><span style="color:blue;">=</span>"<span style="color:blue;">[1.7.1,2)</span>"<span style="color:blue;"> /&gt;</span><br /><br /></pre><p>
Square bracket "[" is "greater or equal to". So versions greater than 1.7.1 here...<br /></p><p>
Closing round bracket ")" is less than (not inclusive). So versions up to but not
including 2.<br /></p><p><br /></p><pre style="font-family:Consolas;font-size:13;color:black;background:white;"></pre><p></p><img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=cf74c266-4c1e-40f2-8698-515124710e86" /></div>
    </content>
  </entry>
  <entry>
    <title>WebAPI updated 30 May 2013</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2013/05/31/WebAPIUpdated30May2013.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,732d4906-ff06-4681-9c23-90c30b3ba8b9.aspx</id>
    <published>2013-05-31T00:04:42.8218713-07:00</published>
    <updated>2013-05-31T00:04:42.8218713-07:00</updated>
    <category term=".net 4" label=".net 4" scheme="http://martinwilley.com/blog/CategoryView,category,net4.aspx" />
    <category term="VS2012" label="VS2012" scheme="http://martinwilley.com/blog/CategoryView,category,VS2012.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">WebAPI got updated 30 May 2013.<br /><br />
if you do a NuGet update you'll probably not be able to build afterwards.<br /><br />
WebAPI depends on WebAPI.WebHost 
<br />
which depends on WebAPI.Core 
<br />
which depends on Web.Client<br />
which depends on Microsoft.Net.Http<br />
which now depends on Microsoft.Bcl and Microsoft.Bcl.Build.<br /><br /><a href="http://nuget.org/packages/Microsoft.Bcl/">Microsoft.Bcl </a>is a portability
library which allows .Net 4 etc to use .Net 4.5 types. Apparently it has no effect
on 4.5<br /><br />
But it (or at least the Bcl.Build) has <a href="http://stackoverflow.com/questions/16832256/the-ensurebindingredirects-task-failed-unexpectedly">an
ugly bug</a> when you try to build<br /><font color="#FF0000">Error    12    The "EnsureBindingRedirects"
task failed unexpectedly.<br />
System.NullReferenceException: Object reference not set to an instance of an object.<br />
   at Roxel.BuildTasks.EnsureBindingRedirects.MergeBindingRedirectsFromElements(IEnumerable`1
dependentAssemblies)<br />
   at Roxel.BuildTasks.EnsureBindingRedirects.Execute()<br />
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()<br />
   at Microsoft.Build.BackEnd.TaskBuilder.&lt;ExecuteInstantiatedTask&gt;d__20.MoveNext()   
Ems.WebApp</font><br /><br />
The fix is to add culture="neutral" to any binding redirects that are missing them.
In the default MVC template they are missing for some, and you almost certainly haven't
changed them.<br /><pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">  &lt;</span><span style="color:#a31515;">runtime</span><span style="color:blue;">&gt;</span><span style="color:blue;">    &lt;</span><span style="color:#a31515;">assemblyBinding</span><span style="color:blue;"> </span><span style="color:red;">xmlns</span><span style="color:blue;">=</span>"<span style="color:blue;">urn:schemas-microsoft-com:asm.v1</span>"<span style="color:blue;">&gt;</span><span style="color:blue;">      &lt;</span><span style="color:#a31515;">dependentAssembly</span><span style="color:blue;">&gt;</span><span style="color:blue;">        &lt;</span><span style="color:#a31515;">assemblyIdentity</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">System.Web.Helpers</span>"<span style="color:blue;"> </span><span style="color:red;">publicKeyToken</span><span style="color:blue;">=</span>"<span style="color:blue;">31bf3856ad364e35</span>"<span style="color:blue;"> </span><b><span style="color:red;">culture</span><span style="color:blue;">=</span>"<span style="color:blue;">neutral</span>"</b><span style="color:blue;"> /&gt;</span><span style="color:blue;">        &lt;</span><span style="color:#a31515;">bindingRedirect</span><span style="color:blue;"> </span><span style="color:red;">oldVersion</span><span style="color:blue;">=</span>"<span style="color:blue;">1.0.0.0-2.0.0.0</span>"<span style="color:blue;"> </span><span style="color:red;">newVersion</span><span style="color:blue;">=</span>"<span style="color:blue;">2.0.0.0</span>"<span style="color:blue;"> /&gt;</span><span style="color:blue;">      &lt;/</span><span style="color:#a31515;">dependentAssembly</span><span style="color:blue;">&gt;</span><span style="color:blue;">      &lt;</span><span style="color:#a31515;">dependentAssembly</span><span style="color:blue;">&gt;</span><span style="color:blue;">        &lt;</span><span style="color:#a31515;">assemblyIdentity</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">System.Web.Mvc</span>"<span style="color:blue;"> </span><span style="color:red;">publicKeyToken</span><span style="color:blue;">=</span>"<span style="color:blue;">31bf3856ad364e35</span>"<span style="color:blue;"> </span><b><span style="color:red;">culture</span><span style="color:blue;">=</span>"<span style="color:blue;">neutral</span>"</b><span style="color:blue;"> /&gt;</span><span style="color:blue;">        &lt;</span><span style="color:#a31515;">bindingRedirect</span><span style="color:blue;"> </span><span style="color:red;">oldVersion</span><span style="color:blue;">=</span>"<span style="color:blue;">0.0.0.0-4.0.0.0</span>"<span style="color:blue;"> </span><span style="color:red;">newVersion</span><span style="color:blue;">=</span>"<span style="color:blue;">4.0.0.0</span>"<span style="color:blue;"> /&gt;</span><span style="color:blue;">      &lt;/</span><span style="color:#a31515;">dependentAssembly</span><span style="color:blue;">&gt;</span></pre>Do
a Rebuild (rather than a build) to ensure everything's loaded.<br /><br />
Hopefully there will be a update pretty soon.<br /><br /><br /><br /><p></p><img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=732d4906-ff06-4681-9c23-90c30b3ba8b9" /></div>
    </content>
  </entry>
  <entry>
    <title>TF31003: Windows 8 vs Team Foundation Service</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/11/30/TF31003Windows8VsTeamFoundationService.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,8328acda-f445-47c5-9746-53010e1169d5.aspx</id>
    <published>2012-11-30T10:07:20.5169107-07:00</published>
    <updated>2012-11-30T10:07:20.5169107-07:00</updated>
    <category term="TFS" label="TFS" scheme="http://martinwilley.com/blog/CategoryView,category,TFS.aspx" />
    <category term="VS2012" label="VS2012" scheme="http://martinwilley.com/blog/CategoryView,category,VS2012.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
At work I can happily connect to my Azure-hosted Team Foundation Service. But I couldn't
do it from home. It says it is looking up identity providers, but the live.com logon
screen never shows up. I just see the dreaded TFS31003 error ("<em>Either you have
not entered the necessary credentials or your user account does not have permission
to connect to Team Foundation Server </em>").
</p>
        <p>
My home machines are Windows 8 and linked to my personal LiveIDs, not my work logon.
Windows 8 likes to connect to lots of Skydrive and lots of other services, storing
all those credentials. And Visual Studio picks those rather than allow me to add a
new one. Deleting entries in the Windows credentials store didn't work.
</p>
        <p>
How can I force Visual Studio to select the right logon?
</p>
        <p>
In Visual Studio, View&gt;Other Windows&gt;Web Browser
</p>
        <p>
In the browser, go to live.com, and log on. 
</p>
        <p>
On mine it automatically logged on with another of my logons, so I signed off, and
then signed back in with the correct one.
</p>
        <p>
Now when I tried to connect to the TFS service address, it works. 
</p>
        <img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=8328acda-f445-47c5-9746-53010e1169d5" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Visual Studio 2012 Unit Tests Changes</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/06/30/VisualStudio2012UnitTestsChanges.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,51995b8e-9640-4320-b81f-65de29610bf2.aspx</id>
    <published>2012-06-30T06:57:04.3107541-07:00</published>
    <updated>2012-06-30T06:57:04.3107541-07:00</updated>
    <category term="VS2012" label="VS2012" scheme="http://martinwilley.com/blog/CategoryView,category,VS2012.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <h3>MsTest, Nunit, MbUnit etc
</h3>
        <p>
Unit testing is now available in VS 2012 Express. In the paid-for SKUs, you can use
other unit test frameworks, not just MSTest. For instance, NUnit, MbUnit and so on.
</p>
        <p>
First, install an adapter via Extensions.
</p>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/NunitTestAdapter.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="NunitTestAdapter" border="0" alt="NunitTestAdapter" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/NunitTestAdapter_thumb.png" width="324" height="184" />
          </a>
        </p>
        <p>
Then install the framework via Nuget- and start writing tests!
</p>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/NunitTests.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="NunitTests" border="0" alt="NunitTests" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/NunitTests_thumb.png" width="324" height="275" />
          </a>
        </p>
        <h3>Test windows
</h3>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/TestExplorer.png">
            <img style="background-image: none; border-right-width: 0px; margin: 0px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="TestExplorer" border="0" alt="TestExplorer" align="left" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/TestExplorer_thumb.png" width="244" height="431" />
          </a>There's
now only one, the Test Explorer. Most people only ever used the Test Results before,
and Test Lists was unusable. It has a thin red or green bar (at last!), and simple
splitting into Failed and Passed tests (now with timings). 
</p>
        <p>
There's a search with filters ("FullName: Domain.Tests.MyTest"). For solutions with
large numbers of tests this might be awkward- I'd like to see more customization of
the result tree (by project/folder or namespace), and more filters. 
</p>
        <p>
The results at the bottom of the window are summarized, but you can still click through
to the test and stack.
</p>
        <p>
There's a button to make every build (Ctl-Shift-B) also run the tests - "Run Tests
After Build". Builds and test are run in the  background so it doesn't stop you
coding (well, my underpowered 32bit laptop is less responsive, but it's vastly better
than previous versions of VS). Not so good if you include some integration tests,
but nice nonetheless.
</p>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/RunTestsAfterBuild.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="RunTestsAfterBuild" border="0" alt="RunTestsAfterBuild" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/RunTestsAfterBuild_thumb.png" width="244" height="95" />
          </a>
        </p>
        <h3>Features 
</h3>
        <p>
Unit tests support async tests. And code coverage (in Premium and Ultimate only) is
much easier- no .testsettings, no having to select the dlls, just right click the
tests (or add /enablecodecoverage on the vstest.console.exe command line).
</p>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/CodeCoverage.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="CodeCoverage" border="0" alt="CodeCoverage" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/CodeCoverage_thumb.png" width="324" height="215" />
          </a>
        </p>
        <p>
You can't test private methods anymore (didn't use that anyway). You can't Generate
Unit Tests from a method either. In VS2010 I used this a fair amount- I got a test
project with the correct references, and the correct namespaces as well, even if the
stub test it created normally had to be deleted and rewritten straight away. I'll
miss that.
</p>
        <h3>Key Mappings
</h3>
        <p>
Of course, they broke something. I always use Ctl+R-T to run the current test. Well,
I hold Ctl and type R then T. Which just doesn't work in VS2012. You have to hold
Ctl and type R, then release Ctl and press T. The combination I used, which turns
out to be Ctl R,Ctl T, isn't mapped in VS2012. You can remap it manually. Very annoying.
</p>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/CtrlRT.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="CtrlRT" border="0" alt="CtrlRT" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/CtrlRT_thumb.png" width="324" height="103" />
          </a>
        </p>
        <h3>Microsoft.Fakes: Stubs and Shims
</h3>
        <p>
These are mocking classes, similar to Moq, NMock and RhinoMocks, and derived from
the Pex Moles project. It's simpler than Moq: there's no "Setup(" or "Verify". It's
also VS Ultimate only (not in Premium or Professional). Personally I much prefer to
do simple manual stubs (implement an interface in the test project) than full mocking.
Full mocks are powerful but you end up with loads of code setting up the tests (tight
coupling), and they make it easy to add too many dependencies (just because you can
test it doesn't mean you can forget all about the SRP).
</p>
        <p>
Microsoft.Fakes isn't going to replace mocking frameworks (there's no behaviour verification).
The shimming is very powerful (and dangerous), similar to TypeMock Isolator and other
expensive tools.
</p>
        <p>
To add Fakes, right click the references.
</p>
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/AddFakesAssembly.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="AddFakesAssembly" border="0" alt="AddFakesAssembly" src="http://www.martinwilley.com/blog/content/binary/Visual-Studio-2012-Test_C071/AddFakesAssembly_thumb.png" width="324" height="239" />
          </a>
        </p>
        <p>
You'll get a reference to Microsoft.QualityTools.Testing.Fakes and a project folder
called Fakes with an xml file in it. For example, in a web application, you'll probably
want to fake System.Web so you handle all the HttpContext/ Request stuff.
</p>
        <p>
Stubs are simple (<a href="http://msdn.microsoft.com/en-us/library/tfs/hh549174%28v=vs.110%29.aspx" target="_blank">MSDN</a>).
Let's stub our input object and initialize it's value. All properties are automatically
prepared to return the defaults (0s or nulls). The stub method (in the .Net framework
or a local assembly) has a "Stub" prefix.
</p>
        <pre class="csharpcode">        [Test]
        <span class="kwrd">public</span><span class="kwrd">void</span> Test20()
{ <span class="rem">//arrange</span> var entity = <span class="kwrd">new</span> ClassLibrary1.Tasks.Fakes.StubEntity();
entity.Value = 20; var processor = <span class="kwrd">new</span> Processor(); <span class="rem">//act</span> var
result = processor.Execute(entity); <span class="rem">//assert</span> Assert.That(result.Value,
Is.EqualTo(20)); }</pre>
        <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
        <p>
Shims are more difficult and powerful (<a href="http://msdn.microsoft.com/en-us/library/tfs/hh549176%28v=vs.110%29.aspx" target="_blank">MSDN</a>). 
</p>
        <p>
Here's a method we want to test:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> FileReader
{ <span class="kwrd">public</span><span class="kwrd">string</span> ReadAllText(<span class="kwrd">string</span> path)
{ <span class="kwrd">return</span> System.IO.File.ReadAllText(path); }</pre>
        <p>
          <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
Here's how we can test it. Here we have to fake the System reference (which includes
mscorlib). Note for shims, we have to have a ShimsContext. The prefix is "Shim" and
methods and properties are prepared with lambda functions.
</p>
        <pre class="csharpcode">        [Test]
        <span class="kwrd">public</span><span class="kwrd">void</span> ReadAllTextTest()
{ <span class="rem">//arrange</span><span class="kwrd">string</span> result; var
reader = <span class="kwrd">new</span> FileReader(); <span class="kwrd">using</span> (Microsoft.QualityTools.Testing.Fakes.
ShimsContext.Create()) { System.IO.Fakes.ShimFile.ReadAllTextString = (arg) =&gt; <span class="str">"x"</span>; <span class="rem">//act</span> result
= reader.ReadAllText(<span class="str">@"X:\doesnotexist\notThere.txt"</span>);
} <span class="rem">//assert</span> Assert.That(result, Is.EqualTo(<span class="str">"x"</span>));
}</pre>
        <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
        <img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=51995b8e-9640-4320-b81f-65de29610bf2" />
      </div>
    </content>
  </entry>
  <entry>
    <title>TechEd Europe 2012</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/06/29/TechEdEurope2012.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,f1abc9e2-959f-453f-9430-e5ac837c7137.aspx</id>
    <published>2012-06-29T13:16:38.8803132-07:00</published>
    <updated>2012-06-29T13:16:38.8803132-07:00</updated>
    <category term="TechEd" label="TechEd" scheme="http://martinwilley.com/blog/CategoryView,category,TechEd.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last time at TechEd 2010 in Berlin was a <a href="http://martinwilley.com/blog/2010/11/13/TechEdEurope2010WrapUp.aspx" target="_blank">little
disappointing</a>. After taking a year off, Microsoft moved this time to Amsterdam. 
</p>
        <p>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120628_430_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120628_430" border="0" alt="20120628_430" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120628_430_thumb.jpg" width="240" height="180" />
          </a>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_417_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120627_417" border="0" alt="20120627_417" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_417_thumb.jpg" width="240" height="180" />
          </a>
        </p>
        <p>
Ok, I know we're not supposed to be sightseeing, but central Amsterdam is fun and
easy to explore on foot. Another incidental but important point: Amsterdam in June
is a lot warmer than Berlin in November. The weather was warm and humid under grey
skies. The conference air conditioning struggled a little at times, so it was a little
uncomfortable.
</p>
        <p>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_389_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 10px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120627_389" border="0" alt="20120627_389" align="right" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_389_thumb.jpg" width="240" height="180" />
          </a>
        </p>
        <p>
The venue was a little outside Amsterdam centre, but easily accessible by Metro. Finding
rooms was, as always, a bit challenging, but by the middle of the week we had sort
of worked it out. 
</p>
        <p>
I bet 90% of attendees tried to press the big buttons on the check-in screens. Turns
out they weren't touch screens, you had to use the mouse. This at an event promoting
Windows 8's touch screen abilities. Even more lame was that Wi-Fi was down for all
Tuesday. 
</p>
        <p>
The bag was certainly nicer than 2010's. No T-shirt or other swag. This is the week
that Google's developer event gave every attendee a new phone, a new tablet, and their
new media streamer device. No Surface tablets or Nokia phones here. This is the first
place I've seen anyone else with a Windows Phone- there were quite a few around. Three
quarters of attendees had iPhones or Androids though. This is about as faithful an
audience that Microsoft can get, and Windows Phone is in a minority. That's pretty
bad for Microsoft and Nokia. 
</p>
        <p>
We'll get TechNet subscriptions, but they don't contain Visual Studio. For developers,
who generally feel like second-class citizens at these events, it disappointing. Give
us a cheap tablet with Windows 8 RC to play with, and we might be a little less sceptical
about Metro. If we can't get enthusiastic about it, no-one will be. Why not DVDs with
the RCs of Windows 8 and Visual Studio 2012 just to save us some bandwidth? 
</p>
        <p>
There were competitions to win Lumia 900s in the expo, but otherwise swag was disappointing
there too. 
</p>
        <p>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120626%20dinner1_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120626 dinner1" border="0" alt="20120626 dinner1" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120626%20dinner1_thumb.jpg" width="324" height="68" />
          </a> Dinners
were vast - the scale is always impressive. Good food, too. 
</p>
        <p>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_412_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 10px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120627_412" border="0" alt="20120627_412" align="right" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_412_thumb.jpg" width="240" height="178" />
          </a>The
delegate party was at the Amsterdam arena, and that was a pretty good venue. Plenty
of beer, cheese, and other nibbles, and huge screens to show the football. The music
was way too loud, though (we're old boring gits, not teenagers).
</p>
        <p>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_0915%20AmsterdamArena%20TechEd2012_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120627_0915 AmsterdamArena TechEd2012" border="0" alt="20120627_0915 AmsterdamArena TechEd2012" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120627_0915%20AmsterdamArena%20TechEd2012_thumb.jpg" width="324" height="89" />
          </a>
        </p>
        <p>
          <a href="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120626_387_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="20120626_387" border="0" alt="20120626_387" align="left" src="http://martinwilley.com/blog/content/binary/Windows-Live-Writer/TechEd-Europe-2012_100D8/20120626_387_thumb.jpg" width="240" height="180" />
          </a>The
keynotes on Tuesday and Wednesday heavily promoted Windows Server 2012 (Tues) and
Windows 8/ metro (Wed). The key message from the 2nd keynote seems to be that Windows
8 scales uniformly up from Phone to tablet to desktop. Which is a different story
to Apple, who have a clear distinction between iOS and the OS X versions (and MS's
own previous CE/ full Windows split). A few glitches when the Metro gestures didn't
work properly, which was amusing and disturbing. 
</p>
        <p>
My impression is that while it might work well on a small touchscreen, it's not obvious
or easily discoverable for a desktop. The demo applications look nice, but most internal
and third party business applications look like crap and there's nothing to make the
average developer into a decent UX designer. Even properly designed metro apps use
lots of whitespace, and have a low information density. Being chromeless actually
makes it more difficult to understand what you can do (the Windows Phone IE has tabs,
but I still haven't figured out how to switch between them- I think Windows 8 metro
IE is the same). The limited choice of full screen or side-by-side docking is just
inadequate for a lot of normal PC users.
</p>
        <p>
There were some good sessions. Honestly I'm not sure I learned a lot new, but it most
sessions were a good review and clarification of what's current (see my <a href="http://martinwilley.com/blog/2012/06/28/AzureWebRolesVsWebSites.aspx" target="_blank">Azure
posting</a>). All the information is already on the internet, so a few sessions turned
out to be quite boring. But it can be hard to keep up when searches bring up old blog
posts that are way out of date, and the sheer range of things that are going on. Best
session was Scott Gu's Azure introduction (some of the following Azure sessions were
repeating the same information, and were quite dull as a result). Mads Kristensen
from the asp.net team was great too, showing current Visual Studio work that only
existed on his computer. 
</p>
        <p>
The last one was disappointing because there wasn't any new stuff coming out. This
year there is, so overall it was worthwhile (thanks to my company for paying for it!).
I wonder if these conferences will still be relevant much longer. I'm not arguing
that we should get lots of free swag, but they do have to give a compelling reason
to physically attend. apart from all the free beer, of course.
</p>
        <img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=f1abc9e2-959f-453f-9430-e5ac837c7137" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Azure web roles vs web sites</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/06/28/AzureWebRolesVsWebSites.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,37215d3b-cdf0-4523-b926-6a21127ded60.aspx</id>
    <published>2012-06-28T14:24:41.0735619-07:00</published>
    <updated>2012-06-28T14:24:41.0735619-07:00</updated>
    <category term="Azure" label="Azure" scheme="http://martinwilley.com/blog/CategoryView,category,Azure.aspx" />
    <category term="TechEd" label="TechEd" scheme="http://martinwilley.com/blog/CategoryView,category,TechEd.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.martinwilley.com/blog/content/binary/Azure-web-roles-vs-web-sites_142CD/azurelogo.png">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="azurelogo" border="0" alt="azurelogo" align="right" src="http://www.martinwilley.com/blog/content/binary/Azure-web-roles-vs-web-sites_142CD/azurelogo_thumb.png" width="168" height="35" />
          </a>Websites
are new in Azure From conversations and talks at Teched:
</p>
        <table border="0" cellspacing="0" cellpadding="2" width="400">
          <tbody>
            <tr>
              <td valign="top" width="200">
Web Roles</td>
              <td valign="top" width="200">
Websites</td>
            </tr>
            <tr>
              <td valign="top" width="200">
The standard offering</td>
              <td valign="top" width="200">
New (summer 2012). In preview and discounted at time of writing.</td>
            </tr>
            <tr>
              <td valign="top" width="200">
For asp.net/php/html</td>
              <td valign="top" width="200">
same</td>
            </tr>
            <tr>
              <td valign="top" width="200">
Use Azure SQL/Blobs/Table storage. Can use the new Caching.</td>
              <td valign="top" width="200">
same</td>
            </tr>
            <tr>
              <td valign="top" width="200">
-</td>
              <td valign="top" width="200">
Has a persistent shared disk.</td>
            </tr>
            <tr>
              <td valign="top" width="200">
Production and staging. Instant switch for releases.</td>
              <td valign="top" width="200">
No. You upload to the live site, so a long release will mean the site is broken for
that time ( but see below- you can do diff releases)</td>
            </tr>
            <tr>
              <td valign="top" width="200">
Can install dependencies (3rd party installations etc, GAC)</td>
              <td valign="top" width="200">
Bin-deployment of dlls only - or one of a small number of packages like Umbracco.</td>
            </tr>
            <tr>
              <td valign="top" width="200">
Release as cspack. Spins up new VMs for each release.</td>
              <td valign="top" width="200">
Release by web deploy, git, TFS, FTP. 
</td>
            </tr>
            <tr>
              <td valign="top" width="200">
RDP access</td>
              <td valign="top" width="200">
No. Just FTP.</td>
            </tr>
            <tr>
              <td valign="top" width="200">
 </td>
              <td valign="top" width="200">
 </td>
            </tr>
          </tbody>
        </table>
        <h4>In conclusion:
</h4>
        <p>
Web sites simplify the story for simple web sites. But having no staging is a bit
limited.
</p>
        <p>
Web roles are better for more complex sites, with dependencies and/or worker roles. 
</p>
        <p>
There is no option to upgrade a website to a web role.
</p>
        <p>
All the above will probably be obsolete is 6 months as the features expand.
</p>
        <img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=37215d3b-cdf0-4523-b926-6a21127ded60" />
      </div>
    </content>
  </entry>
  <entry>
    <title>EF Code First - Add a DTO</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/03/21/EFCodeFirstAddADTO.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,884b5782-0923-43bd-b3b5-eb359c657758.aspx</id>
    <published>2012-03-20T23:18:25.8244417-07:00</published>
    <updated>2012-03-20T23:18:25.8244417-07:00</updated>
    <category term="Code First" label="Code First" scheme="http://martinwilley.com/blog/CategoryView,category,CodeFirst.aspx" />
    <category term="Entity Framework" label="Entity Framework" scheme="http://martinwilley.com/blog/CategoryView,category,EntityFramework.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">EF Code First has a neat method to merge
in the values of a DTO into an mapped entity.<br /><br />
context.Entry(entity).CurrentValues.SetValues(dataTransferObject);<br /><br />
The DTO will generally have a primary key property, and you can use that to determine
if it is a new record or a modification. Here's a method that does that:<br /><span style="color: blue;">public</span><span style="color: blue;">static</span> T
Merge&lt;T&gt;(<span style="color: blue;">this</span><span style="color: #2b91af;">DbContext</span> context, <span style="color: blue;">object</span> dataTransferObject)<br />
             <span style="color: blue;">where</span> T
: <span style="color: blue;">class</span><br />
        {<br />
            <span style="color: blue;">if</span> (context
== <span style="color: blue;">null</span>) <span style="color: blue;">throw</span><span style="color: blue;">new</span><span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"context"</span>);<br />
            <span style="color: blue;">if</span> (dataTransferObject
== <span style="color: blue;">null</span>) <span style="color: blue;">throw</span><span style="color: blue;">new</span><span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"dataTransferObject"</span>);<br />
 <br />
            <span style="color: blue;">var</span> property
= FindPrimaryKeyProperty&lt;T&gt;(context);<br />
            <span style="color: green;">//find
the id property of the dto</span><br />
            <span style="color: blue;">var</span> idProperty
= dataTransferObject.GetType().GetProperty(property.Name);<br />
            <span style="color: blue;">if</span> (idProperty
== <span style="color: blue;">null</span>)<br />
                <span style="color: blue;">throw</span><span style="color: blue;">new</span><span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Cannot
find an id on the dataTransferObject"</span>);<br />
            <span style="color: blue;">var</span> id
= idProperty.GetValue(dataTransferObject, <span style="color: blue;">null</span>);<br />
            <span style="color: green;">//has
the id been set (existing item) or not (transient)?</span><br />
            <span style="color: blue;">var</span> propertyType
= property.PropertyType;<br />
            <span style="color: blue;">var</span> transientValue
= propertyType.IsValueType ?<br />
                <span style="color: #2b91af;">Activator</span>.CreateInstance(propertyType)
: <span style="color: blue;">null</span>;<br />
            <span style="color: blue;">var</span> isTransient
= Equals(id, transientValue);<br />
            T entity;<br />
            <span style="color: blue;">if</span> (isTransient)<br />
            {<br />
                <span style="color: green;">//it's
transient, just create a dummy</span><br />
                entity
= CreateEntity&lt;T&gt;(id, property);<br />
                <span style="color: green;">//if
DatabaseGeneratedOption(DatabaseGeneratedOption.None) and no id, this errors</span><br />
                context.Set&lt;T&gt;().Attach(entity);<br />
            }<br />
            <span style="color: blue;">else</span><br />
            {<br />
                <span style="color: green;">//try
to load from identity map or database</span><br />
                entity
= context.Set&lt;T&gt;().Find(id);<br />
                <span style="color: blue;">if</span> (entity
== <span style="color: blue;">null</span>)<br />
                {<br />
                    <span style="color: green;">//could
not find entity, assume assigned primary key</span><br />
                   
entity = CreateEntity&lt;T&gt;(id, property);<br />
                   
context.Set&lt;T&gt;().Add(entity);<br />
                }<br />
            }<br />
            <span style="color: green;">//copy
the values from DTO onto the entry</span><br />
            context.Entry(entity).CurrentValues.SetValues(dataTransferObject);<br />
            <span style="color: blue;">return</span> entity;<br />
        }<br />
 <br />
 <br />
        <span style="color: blue;">private</span><span style="color: blue;">static</span><span style="color: #2b91af;">PropertyInfo</span> FindPrimaryKeyProperty&lt;T&gt;(<span style="color: #2b91af;">IObjectContextAdapter</span> context)<br />
            <span style="color: blue;">where</span> T
: <span style="color: blue;">class</span><br />
        {<br />
            <span style="color: green;">//find
the primary key</span><br />
            <span style="color: blue;">var</span> objectContext
= context.ObjectContext;<br />
            <span style="color: green;">//this
will error if it's not a mapped entity</span><br />
            <span style="color: blue;">var</span> objectSet
= objectContext.CreateObjectSet&lt;T&gt;();<br />
            <span style="color: blue;">var</span> elementType
= objectSet.EntitySet.ElementType;<br />
            <span style="color: blue;">var</span> pk
= elementType.KeyMembers.First();<br />
            <span style="color: green;">//look
it up on the entity</span><br />
            <span style="color: blue;">var</span> propertyInfo
= <span style="color: blue;">typeof</span>(T).GetProperty(pk.Name);<br />
            <span style="color: blue;">return</span> propertyInfo;<br />
        }<br />
 <br />
        <span style="color: blue;">private</span><span style="color: blue;">static</span> T
CreateEntity&lt;T&gt;(<span style="color: blue;">object</span> id, <span style="color: #2b91af;">PropertyInfo</span> property)<br />
            <span style="color: blue;">where</span> T
: <span style="color: blue;">class</span><br />
        {<br />
            <span style="color: green;">//
consider IoC here</span><br />
            <span style="color: blue;">var</span> entity
= (T)<span style="color: #2b91af;">Activator</span>.CreateInstance(<span style="color: blue;">typeof</span>(T));<br />
            <span style="color: green;">//set
the value of the primary key (may error if wrong type)</span><br />
            property.SetValue(entity,
id, <span style="color: blue;">null</span>);<br />
            <span style="color: blue;">return</span> entity;<br />
        }<br /><p></p><img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=884b5782-0923-43bd-b3b5-eb359c657758" /></div>
    </content>
  </entry>
  <entry>
    <title>EF Code First - is entity transient?</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/03/20/EFCodeFirstIsEntityTransient.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,5d9159b9-ec54-4c84-bba8-e2f1c0da0c31.aspx</id>
    <published>2012-03-19T21:29:09.1965697-07:00</published>
    <updated>2012-03-19T21:29:09.1965697-07:00</updated>
    <category term="Code First" label="Code First" scheme="http://martinwilley.com/blog/CategoryView,category,CodeFirst.aspx" />
    <category term="Entity Framework" label="Entity Framework" scheme="http://martinwilley.com/blog/CategoryView,category,EntityFramework.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">When the Code First project get a detached
entity from the UI, it may need to check if it is a new (transient) entity - which
can be added to the DbSet - or an existing entity that has been modified. You can
then implement an AddOrUpdate method.<br /><br />
To do that, it needs to know what the primary key of the entity is, and read the value.<br /><br />
The easiest way to do is generically is for all entities to have a standard interface
or abstract base. 
<br /><pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">var</span> id = ((<span style="color:#2b91af;">IEntity</span>) entity).Id; <span style="color:blue;">if</span> (id == <span style="color:blue;">default</span>(<span style="color:blue;">int</span>))
{     <span style="color:green;">//add</span> } <span style="color:blue;">else</span> {
    <span style="color:green;">//update</span> }</pre>If you use
the [Key] attribute you can also use that to discover the primary key of the entity,
whatever the type.<br /><br />
Finally,  you can use EF's internal metadata. 
<br /><pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsTransient&lt;T&gt;(<span style="color:#2b91af;">DbContext</span> context, T entity)
    <span style="color:blue;">where</span> T : <span style="color:blue;">class</span> {
    <span style="color:green;">//find the primary key</span>     <span style="color:blue;">var</span> objectContext = ((<span style="color:#2b91af;">IObjectContextAdapter</span>)context).ObjectContext;
    <span style="color:green;">//this will error if it's not a mapped entity</span>     <span style="color:blue;">var</span> objectSet = objectContext.CreateObjectSet&lt;T&gt;();
    <span style="color:blue;">var</span> elementType = objectSet.EntitySet.ElementType;
    <span style="color:blue;">var</span> pk = elementType.KeyMembers.First();
    <span style="color:green;">//look it up on the entity</span>     <span style="color:blue;">var</span> propertyInfo = <span style="color:blue;">typeof</span>(T).GetProperty(pk.Name);
    <span style="color:blue;">var</span> propertyType = propertyInfo.PropertyType;
    <span style="color:green;">//what's the default value for the type?</span>     <span style="color:blue;">var</span> transientValue = propertyType.IsValueType ? <span style="color:#2b91af;">Activator</span>.CreateInstance(propertyType) : <span style="color:blue;">null</span>;
    <span style="color:green;">//is the pk the same as the default value (int == 0, string == null ...)</span>     <span style="color:blue;">return</span> propertyInfo.GetValue(entity, <span style="color:blue;">null</span>) == transientValue;
}</pre><br /><p></p><img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=5d9159b9-ec54-4c84-bba8-e2f1c0da0c31" /></div>
    </content>
  </entry>
  <entry>
    <title>EF Code First -  Generically setting entity references to unchanged status</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/03/19/EFCodeFirstGenericallySettingEntityReferencesToUnchangedStatus.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,e2923ad4-daa5-4022-9b65-a41f4898e79f.aspx</id>
    <published>2012-03-19T10:08:43.547-07:00</published>
    <updated>2012-03-19T10:12:15.3346261-07:00</updated>
    <category term="Code First" label="Code First" scheme="http://martinwilley.com/blog/CategoryView,category,CodeFirst.aspx" />
    <category term="Entity Framework" label="Entity Framework" scheme="http://martinwilley.com/blog/CategoryView,category,EntityFramework.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <a href="http://martinwilley.com/blog/2012/03/18/EFCodeFirstUsingDummyReferences.aspx">Last
time</a> I was adding a new record with a reference to a dummy record. I marked the
reference as Unchanged so Code First wouldn't try to validate or save it.<br /><p><span style="font-family:Consolas;color:#000000;">var johnCarter = new </span><span style="font-family:Consolas;color:#2b91af;">Movie</span><span style="font-family:Consolas;color:#000000;">()
{ Title = </span><span style="font-family:Consolas;color:#a31515;">"John Carter"</span><span style="font-family:Consolas;color:#000000;"> };<br />
johnCarter.DirectorId = andrewStantonId;<br />
context.Movies.Add(johnCarter);<br />
//after it's added, change the status of the reference<br />
context.Entry(johnCarter.Director).State = </span><span style="font-family:Consolas;color:#2b91af;">EntityState</span><span style="font-family:Consolas;color:#000000;">.Unchanged;<br />
context.SaveChanges();</span></p>
Can you set all the references on any entity? 
<br /><p style="font-family:Consolas;color:#000000;">
var johnCarter = new <span style="font-family:Consolas;color:#2b91af;">Movie</span><span style="font-family:Consolas;color:#000000;">()
{ Title = </span><span style="font-family:Consolas;color:#a31515;">"John Carter"</span> };<br />
johnCarter.DirectorId = andrewStantonId;<br />
context.Movies.Add(johnCarter);<br />
//after it's added, change the status of the reference<br /><span style="background-color:#ff0">MarkNavigationPropertiesUnchanged(johnCarter)</span><span style="font-family:Consolas;color:#000000;">;<br />
context.SaveChanges();</span></p>
We have to look into the underlying EF model.<br /><br /><pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> MarkNavigationPropertiesUnchanged&lt;T&gt;(<span style="color:#2b91af;">DbContext</span> context, T entity)
    <span style="color:blue;">where</span> T : <span style="color:blue;">class</span> {
    <span style="color:blue;">var</span> objectContext = ((<span style="color:#2b91af;">IObjectContextAdapter</span>)context).ObjectContext;
    <span style="color:blue;">var</span> objectSet = objectContext.CreateObjectSet&lt;T&gt;();
    <span style="color:blue;">var</span> elementType = objectSet.EntitySet.ElementType;
    <span style="color:blue;">var</span> navigationProperties = elementType.NavigationProperties;
    <span style="color:green;">//the references</span>     <span style="color:blue;">var</span> references = <span style="color:blue;">from</span> navigationProperty <span style="color:blue;">in</span> navigationProperties
                        <span style="color:blue;">let</span> end = navigationProperty.ToEndMember
                        <span style="color:blue;">where</span> end.RelationshipMultiplicity == <span style="color:#2b91af;">RelationshipMultiplicity</span>.ZeroOrOne ||
                        end.RelationshipMultiplicity == <span style="color:#2b91af;">RelationshipMultiplicity</span>.One
                        <span style="color:blue;">select</span> navigationProperty.Name;
    <span style="color:green;">//NB: We don't check Collections. EF wants to handle the object graph.</span>     <span style="color:blue;">var</span> parentEntityState = context.Entry(entity).State;
    <span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> navigationProperty <span style="color:blue;">in</span> references)
    {         <span style="color:green;">//if it's modified but not loaded, don't need to touch it</span>         <span style="color:blue;">if</span> (parentEntityState == <span style="color:#2b91af;">EntityState</span>.Modified &amp;&amp;
            !context.Entry(entity).Reference(navigationProperty).IsLoaded)
            <span style="color:blue;">continue</span>;
        <span style="color:blue;">var</span> propertyInfo = <span style="color:blue;">typeof</span>(T).GetProperty(navigationProperty);
        <span style="color:blue;">var</span> value = propertyInfo.GetValue(entity, <span style="color:blue;">null</span>);
        context.Entry(value).State = <span style="color:#2b91af;">EntityState</span>.Unchanged;
    } }</pre><br />
This code only fixes the references to single entities (like movie.Director) - not
collections (like director.Movies). It's possible to discover and iterate the collections
to change their status, but you'll likely get exceptions from EF because its model
is broken.<br /><br /><p></p><img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=e2923ad4-daa5-4022-9b65-a41f4898e79f" /></div>
    </content>
  </entry>
  <entry>
    <title>EF Code First - Using dummy references</title>
    <link rel="alternate" type="text/html" href="http://martinwilley.com/blog/2012/03/18/EFCodeFirstUsingDummyReferences.aspx" />
    <id>http://martinwilley.com/blog/PermaLink,guid,8549925c-fb0f-4dad-8ed4-29cf8db8132b.aspx</id>
    <published>2012-03-18T00:04:08.1208926-07:00</published>
    <updated>2012-03-18T00:04:08.1208926-07:00</updated>
    <category term="Code First" label="Code First" scheme="http://martinwilley.com/blog/CategoryView,category,CodeFirst.aspx" />
    <category term="Entity Framework" label="Entity Framework" scheme="http://martinwilley.com/blog/CategoryView,category,EntityFramework.aspx" />
    <author>
      <name>Martin</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">You’re adding a new record, which has a
reference to an existing object.<br /><br /><span style="font-family:Consolas;color:#000000;">var johnCarter = new </span><span style="font-family:Consolas;color:#2b91af;">Movie</span><span style="font-family:Consolas;color:#000000;">()
{ Title = </span><span style="font-family:Consolas;color:#a31515;">"John Carter"</span><span style="font-family:Consolas;color:#000000;"> };<br />
johnCarter.Director = new </span><span style="font-family:Consolas;color:#2b91af;">Director</span><span style="font-family:Consolas;color:#000000;"> {
Id = andrewStantonId };<br />
context.Movies.Add(johnCarter);<br />
context.SaveChanges();</span><p>
SaveChanges() will save the new Movie- but it also saves a new Director record (which
gets a new Id, even though you set it manually). When you add an entity to a DbSet,
the entire object graph is marked as “Added”.
</p><p>
You could do a Find to load the Director record from the database, but it is a pointless
database access that you don’t need. It just needs to save the Movie record with a
known directorId. 
</p><p>
In NHibernate you can use session.Load&lt;Director&gt;(andrewStantonId) which will
create an empty proxy object without hitting the database. Only if you use one of
the proxy properties (like director.Name) will it hit the database to load the record.
EF Code First doesn’t have this feature.
</p><p>
One way round it to add a foreign key Id property to the Movie record:
</p><p><span style="font-family:Consolas;color:#000000;">        public
virtual </span><span style="font-family:Consolas;color:#2b91af;">Director</span><span style="font-family:Consolas;color:#000000;"> Director
{ get; set; }<br />
       public int? DirectorId { get; set; }</span></p><p>
You can then set the DirectorId directly. The two properties are not kept in step
automatically, so setting the DirectorId doesn’t cause Director to load from the database.
Foreign key Id properties are convenient, but your object model is “denormalized”.
</p><p>
The alternative is to mark the dummy record as unchanged. There are two ways.
</p><p>
One is to set the context.Entry state for the dummy reference AFTER the new record
has been added.
</p><p><span style="font-family:Consolas;color:#000000;">var johnCarter = new </span><span style="font-family:Consolas;color:#2b91af;">Movie</span><span style="font-family:Consolas;color:#000000;">()
{ Title = </span><span style="font-family:Consolas;color:#a31515;">"John Carter"</span><span style="font-family:Consolas;color:#000000;"> };<br />
johnCarter.DirectorId = andrewStantonId;<br />
context.Movies.Add(johnCarter);<br />
//after it's added, change the status of the reference<br />
context.Entry(johnCarter.Director).State = </span><span style="font-family:Consolas;color:#2b91af;">EntityState</span><span style="font-family:Consolas;color:#000000;">.Unchanged;<br />
context.SaveChanges();</span></p><p>
The second way is to create the dummy reference by Attaching it.
</p><p><span style="font-family:Consolas;color:#000000;">//attach the dummy director record<br />
var andrewStanton = new </span><span style="font-family:Consolas;color:#2b91af;">Director</span><span style="font-family:Consolas;color:#000000;"> {
Id = andrewStantonId};<br />
context.Directors.Attach(andrewStanton);<br />
//now we have an "unchanged" director record to attach<br />
var johnCarter = new </span><span style="font-family:Consolas;color:#2b91af;">Movie</span><span style="font-family:Consolas;color:#000000;">()
{ Title = </span><span style="font-family:Consolas;color:#a31515;">"John Carter"</span><span style="font-family:Consolas;color:#000000;"> };<br />
johnCarter.Director = andrewStanton;<br />
context.Movies.Add(johnCarter);<br />
context.SaveChanges();</span></p><p></p><img width="0" height="0" src="http://martinwilley.com/blog/aggbug.ashx?id=8549925c-fb0f-4dad-8ed4-29cf8db8132b" /></div>
    </content>
  </entry>
</feed>