<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>system.data.objects dev guy : D3</title><link>http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx</link><description>Tags: D3</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>D3: Rides Again – Now on VS2010 Beta 2</title><link>http://blogs.msdn.com/dsimmons/archive/2009/12/06/d3-rides-again-now-on-vs2010-beta-2.aspx</link><pubDate>Sun, 06 Dec 2009 01:19:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9933056</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9933056.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9933056</wfw:commentRss><description>&lt;p&gt;And you thought I might never post on D3 again.&amp;#160; I know.&amp;#160; I know.&amp;#160; Jedi knights don’t wait 6 months for a follow-up blog post.&lt;a href="#_b47d84a174ea44beaf1ed22cba91d1e1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;sup&gt;&amp;#160; &lt;/sup&gt;Well, the wait is finally over.&amp;#160; I’ve put a &lt;a href="http://code.msdn.microsoft.com/d3/Release/ProjectReleases.aspx?ReleaseId=3648" target="_blank"&gt;new release of D3&lt;/a&gt; up on code gallery.&amp;#160; In this release the code has been updated to work with VS2010/.Net 4 beta 2.&amp;#160; The release also includes a reorganization of project names and some new functionality, but I’ll talk about those in a future post or two.&amp;#160; For this post, let’s look at the three broad steps involved in upgrading the project since they illuminate EF features and changes you might need to make to move some other project from beta 1 to beta 2.&lt;/p&gt;  &lt;h3&gt;Step 1 : Basic Compilation&lt;/h3&gt;  &lt;p&gt;When I loaded the solution in beta 2 and tried to recompile, the first problems I encountered were issues with stylecop—there were a number of errors referring to a file called &amp;quot;.NETFramework,Version=v4.0.AssemblyAttributes.&amp;quot;&amp;#160; I found a solution in &lt;a href="http://code.msdn.microsoft.com/sourceanalysis/Thread/View.aspx?ThreadId=2512"&gt;this thread&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The next change needed was to react to the EF’s breaking change from beta 1 to beta 2 where the ContextOptions property DeferredLoadingEnabled was renamed to LazyLoadingEnabled.&amp;#160;&amp;#160;&amp;#160; The semantics of this property didn’t change—just it’s name, so this was a simple fix.&lt;/p&gt;  &lt;p&gt;Once compilation was passing, the next step was to start leveraging new EF features in beta 2.&lt;/p&gt;  &lt;h3&gt;Step 2 : Adding FKs to the Model&lt;/h3&gt;  &lt;p&gt;Beta 2 not only added runtime support for FKs in the model, but the designer is now smarter about them, and they are turned on by default. So I had a few options:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;I could do everything by hand:&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Manually add the FK properties.&lt;/li&gt;      &lt;li&gt;Go to each association and add the referential integrity constraint between the FK property on the dependent entity and the primary key on the principal entity.&lt;/li&gt;      &lt;li&gt;Remove the association set mappings for the associations since the mapping will now be handled by mapping the FK property on the entity just like any other entity.&lt;/li&gt;      &lt;li&gt;Finally, either manually add the mapping for that property or regenerate the database from the model.       &lt;br /&gt;&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;Since the model is not yet all that big, I could recreate it from scratch, requesting FK properties with each association.     &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Given that I already had a database with foreign key columns (just not exposed on the entities), I could reverse engineer the model from the database with the option turned on to generate FK properties on the entities.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;I choose option #3 for no reason except that it was interesting to come full circle from a model-first database to a DB first model and then back to model first when further changes are needed.&amp;#160; :-)&amp;#160; In the process I encountered one may difficulty which was that reverse engineering from the database uses a simplistic algorithm for naming things that works reasonably well when you only have one relationship between any two entities, but when you have more than one relationship between the same pair of entities things get a bit confusing.&amp;#160; In D3 I have two identical relationships between Room and Exit--one for leaving a room by way of an exit and the other for entering a room from an exit. The one tricky thing is determining which association was connected to which navigation and FK property.&amp;#160; First you need to right click on the navprop and choose the &amp;quot;select association” menu item.&amp;#160; Then you can double click on the association to see a dialog which describes the referential constraint including the FK property.&lt;/p&gt;  &lt;h3&gt;Step 3 : Updating Model First Workflow &amp;amp; Templates&lt;/h3&gt;  &lt;p&gt;The final step was to update the D3 model first workflow and templates both because the bug mentioned in &lt;a href="http://blogs.msdn.com/dsimmons/archive/2009/06/27/d3-fun-with-concurrency-and-model-first-part-1.aspx"&gt;this previous post&lt;/a&gt; was fixed and because of other changes and improvements made by the designer team in beta 2.&amp;#160; You can read the &lt;a href=" http://blogs.msdn.com/adonet/archive/2009/11/05/model-first-with-the-entity-framework-4.aspx"&gt;details of those improvements on the ado.net team blog&lt;/a&gt;, but the summary is that there are now two properties available in the EDM designer when you click on an empty place on the designer surface.&amp;#160; The “Database Generation Workflow” property allows you to specify which workflow xaml file will be used when you choose to generate the database from the model.&amp;#160; The default value picks up the file from one of the VS installation directories, but it can also point to a per user directory (where a new xaml file could be installed by a VSIX package for instance) or to a location relative to your project directory.&amp;#160; The “DDL Generation Template” property specifies the T4 template used in the step for generating the data definition language based on the SSDL.&lt;/p&gt;  &lt;p&gt;I was able to keep the overall strategy the same—for the most part just comparing the original versions of the files to the new versions and reacting to things like the renaming of the namespace Microsoft.Data.Entity.Design.Pipeline.EdmExtension to Microsoft.Data.Entity.Design.DatabaseGeneration.Activities, or the change in xml namespace for the StoreGeneratedAttribute in SSDL from the SSDL namespace to the annotations namespace.&amp;#160; &lt;/p&gt;  &lt;p&gt;In the case of the SsdlToCode template there were more extensive changes, so I took the new template from the system and reapplied the same customization I originally made.&amp;#160; Once the updates were complete, I diff’d the new template against the old one and the changes generally fall into the following buckets:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Updating assembly and namespace names&lt;/li&gt;    &lt;li&gt;Moving re-usable utility code out of the template and into a shared ttinclude file&lt;/li&gt;    &lt;li&gt;Supporting multiple sql server schemas in the same ssdl file&lt;/li&gt;    &lt;li&gt;Supporting SQLCE by skipping things it doesn't allow like specific schema names&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;While I was at it, I also applied a couple of improvements which a colleague suggested could be made to the default template including removing the “WITH NOCHECK” clauses when creating constraints and adding statements to create indexes for each foreign key.&lt;/p&gt;  &lt;h3&gt;Final Result&lt;/h3&gt;  &lt;p&gt;The final result of all this is that D3 is up and running on beta 2.&amp;#160; We’re back to the functionality we had last June with beta 1 plus we now have FK properties and a somewhat more efficient database.&amp;#160; This is just the foundation needed so that we can start filling in more of the overall application.&lt;/p&gt;  &lt;p&gt;- Danny&lt;/p&gt;  &lt;h3&gt;&lt;/h3&gt;  &lt;p&gt;&lt;span style="font-weight: bold; text-decoration: underline"&gt;Footnotes&lt;/span&gt;     &lt;br /&gt;&lt;a id="_b47d84a174ea44beaf1ed22cba91d1e1"&gt;&lt;/a&gt;1. For some of us increasingly old geezers, the cartoon &lt;a href="http://en.wikipedia.org/wiki/Bloom_County"&gt;Bloom County&lt;/a&gt; by Berke Breathed was a major part of our lives once upon a time.&amp;#160; So I can’t help occasionally throwing out obscure references to it.&amp;#160; You can see the specific strip I’m referring to &lt;a href="http://www.gocomics.com/bloomcounty/2009/08/20/"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9933056" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>D3: Fun with Concurrency and Model First – Part 2</title><link>http://blogs.msdn.com/dsimmons/archive/2009/06/27/d3-fun-with-concurrency-and-model-first-part-2.aspx</link><pubDate>Sat, 27 Jun 2009 11:14:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806444</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9806444.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9806444</wfw:commentRss><description>&lt;p&gt;In &lt;a href="http://blogs.msdn.com/dsimmons/archive/2009/06/27/d3-fun-with-concurrency-and-model-first-part-1.aspx"&gt;my last post&lt;/a&gt; I began the story of how I added concurrency checking capabilities to D3—we conquered a bug in the model first workflow activities in VS 2010 beta 1.&amp;#160; Next we need to look into ways to customize the SSDL generated so that the concurrency column will end up with the RowVersion type that SQL Server will automatically update when any part of the row changes.&lt;/p&gt;  &lt;p&gt;This lead to the next surprise.&amp;#160; At first I thought that I would add a new custom activity to the workflow which would fix-up the SSDL, but this turned out to have its own complexities.&amp;#160; As it turns out, the model first process runs the workflow within the visual studio process which means the DLLs containing activities for the workflow are loaded in the same way other VS &lt;a href="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirstPart2_57/DbGenxaml.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px; display: inline; border-top: 0px; border-right: 0px" title="DbGen-xaml" border="0" alt="DbGen-xaml" align="left" src="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirstPart2_57/DbGenxaml_thumb.jpg" width="316" height="389" /&gt;&lt;/a&gt;DLLs are loaded, namely from the VS install directories or from the GAC.&amp;#160; This also means if the DLL changes, you have to shutdown VS and restart it before model first will reflect the change.&amp;#160; Conclusion: If you want to create a general purpose activity that many folks will reuse, then this is a reasonable approach&amp;#160; You can carefully debug an activity and set something up to install it centrally.&amp;#160; But, if you need a one-off customization, this is just too hard.&amp;#160; &lt;/p&gt;  &lt;p&gt;So we’re looking into options to help here, but in the meantime I’ve discovered another approach which seems to work well.&amp;#160; The default workflow described in DbGen.xaml has two activities.&amp;#160; The first one takes the conceptual model and produces an MSL and SSDL string, while the second one takes the SSDL and produces something which will create the database (normally a SQL file, but after my first round of customizations for D3 it will produce a c-sharp file with a method that will create it).&amp;#160; &lt;/p&gt;  &lt;p&gt;It turns out, though, that the second activity under the covers is a general-purpose mechanism that can be configured to take in a string, run it through a T4 template and produce an output string.&amp;#160; The nice thing about using a template is that it allows you to write arbitrary code as a one-off in your solution and easily iterate on your design, modifying and rerunning without any need to restart VS, etc.&lt;/p&gt;  &lt;p&gt;Eureka!&amp;#160; The second activity can be used twice.&amp;#160; In between the two default activities, it can be inserted again and configured to take the SSDL in, transform it with a different template and send it on to the final activity.&amp;#160; In addition model first makes some useful bits of information like a path to the EDMX file available to the template, so we can access the CSDL in order to make better decisions about how to modify the SSDL.&lt;/p&gt;  &lt;p&gt;The relevant part of the resulting XAML looks like this:&lt;/p&gt;  &lt;div&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Sequence&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;edm1:SsdlToDdlActivity&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ExistingSsdlInput&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;[ExistingSsdl]&amp;quot;&lt;/span&gt;&lt;br /&gt;                            &lt;span style="color: #ff0000"&gt;SsdlInput&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;[Ssdl]&amp;quot;&lt;/span&gt;&lt;br /&gt;                            &lt;span style="color: #ff0000"&gt;DdlOutput&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;[Ssdl]&amp;quot;&lt;/span&gt;&lt;br /&gt;                            &lt;span style="color: #ff0000"&gt;TemplatePath&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;DbGen\SsdlUseRowVersion.tt&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Sequence&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;For D3, the fix-up we need is to find properties in the SSDL which correspond to properties in the CSDL with the following characteristics: type is binary, maxlength is 8, concurrency mode is fixed and store generation pattern is computed.&amp;#160; So we read the EDMX into an XElement tree in memory and then use LINQ to XML to gather the names of properties that need to be changed in the SSDL grouped by the name of the table in which they appear:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var query = from property &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; csdl.Elements(edm + &lt;span style="color: #006080"&gt;&amp;quot;EntityType&amp;quot;&lt;/span&gt;).SelectMany(t =&amp;gt; t.Elements(edm + &lt;span style="color: #006080"&gt;&amp;quot;Property&amp;quot;&lt;/span&gt;))&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)property.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;Type&amp;quot;&lt;/span&gt;).Value == &lt;span style="color: #006080"&gt;&amp;quot;Binary&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp;&lt;br /&gt;                  (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)property.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;MaxLength&amp;quot;&lt;/span&gt;).Value == &lt;span style="color: #006080"&gt;&amp;quot;8&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp;&lt;br /&gt;                  (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)property.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;ConcurrencyMode&amp;quot;&lt;/span&gt;).Value == &lt;span style="color: #006080"&gt;&amp;quot;Fixed&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp;&lt;br /&gt;                  (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)property.Attribute(store + &lt;span style="color: #006080"&gt;&amp;quot;StoreGeneratedPattern&amp;quot;&lt;/span&gt;).Value == &lt;span style="color: #006080"&gt;&amp;quot;Computed&amp;quot;&lt;/span&gt;&lt;br /&gt;            group (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)property.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value&lt;br /&gt;            by entitySetName(property.Parent, csdl, edm);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This query depends on knowledge of the algorithm used by the CsdlToSsdlAndMslActivity to map conceptual model properties to the database—namely that the column names are the same as the property names on the entities and (at least for the base types of entity sets) the name of the table is the same as the name of the entity set that contains the type.&amp;#160; So we query the csdl for all the elements that represent properties which have the required facets and extract the name of the property, then we group those property names by the name of the entity set containing the entity type to which the property belongs using these two functions:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; entitySetName(XElement entityType, XElement csdl, XNamespace edm)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (from set &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; csdl.Element(edm + &lt;span style="color: #006080"&gt;&amp;quot;EntityContainer&amp;quot;&lt;/span&gt;)&lt;br /&gt;                            .Elements(edm + &lt;span style="color: #006080"&gt;&amp;quot;EntitySet&amp;quot;&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; entityTypeName(set) == entityType.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value&lt;br /&gt;            select (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)set.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value).Single();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; entityTypeName(XElement entitySet)&lt;br /&gt;{&lt;br /&gt;    XElement schema = entitySet.Parent.Parent;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; nsPrefix = schema.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;Namespace&amp;quot;&lt;/span&gt;).Value + &lt;span style="color: #006080"&gt;&amp;quot;.&amp;quot;&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; aliasPrefix = schema.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;Alias&amp;quot;&lt;/span&gt;).Value + &lt;span style="color: #006080"&gt;&amp;quot;.&amp;quot;&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; typeName = ((&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)entitySet.Attribute(&lt;span style="color: #006080"&gt;&amp;quot;EntityType&amp;quot;&lt;/span&gt;).Value).Trim();&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (typeName.StartsWith(nsPrefix))&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; typeName.Substring(nsPrefix.Length);&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (typeName.StartsWith(aliasPrefix))&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; typeName.Substring(aliasPrefix.Length);&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #006080"&gt;&amp;quot;UNEXPECTED TYPENAME -- Where is the namespace?&amp;quot;&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The result of this query is an IGrouping&amp;lt;string, string&amp;gt; where the first string is the name of the entity set and the second string is a collection of property names from the base type within that set.&amp;#160; With that information it’s a simple matter to load the SSDL into memory, search for the relevant entity types (in the case of the SSDL, each table is represented by an entity type and each column within the table is a property of that type) and properties within them.&amp;#160; Then we swap out the attributes on the XElement for that property and set the name, the type to rowversion and the store generated pattern to computed.&amp;#160; Finally, the template serializes the XML for the SSDL to its output.&lt;/p&gt;

&lt;h3&gt;&lt;/h3&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h3&gt;But what about derived types?&lt;/h3&gt;

&lt;div&gt;The above algorithm is all well and good for a model with no inheritance—every entity’s corresponding SSDL can be fixed up properly, but what if I want to put a concurrency property on an inherited type?&amp;#160; As it turns out the EF doesn’t allow that; concurrency properties can only be present on the base type for an entity set.&amp;#160; While I generally don’t like limitations of any kind, I can’t really think of any scenario where I’d want a concurrency property only on a derived type, and if I do have them on a base type, then I don’t need a different or additional property on any of the derived types.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;In fact, for standard TPH and TPC scenarios all of the properties for a particular entity instance appear in a single row of a single table.&amp;#160; With TPH all of the instances are in a single table, and in TPC there are multiple tables involved but each type has its own table with all of the properties both from the base type and added in the derived type in that table.&amp;#160; So the concurrency property will appear on each table and a rowversion will work just fine because any property changing in that row will cause the concurrency property to change.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;I got to thinking about TPT, though, and became a little worried.&amp;#160; First off, TPT is the default inheritance strategy used by model first.&amp;#160; In this strategy the properties from a base type all live in one table which has a row for every single entity in the set, while extra properties added&amp;#160; by derived types are placed in their own tables with just those properties and the primary key.&amp;#160; So for example, if I have entity type A with an id, scalar properties a and b, and a version, and I have type B which inherits from A and adds scalar property c, then the database would have table A with columns for id, a, b and version PLUS table B with columns for id and c.&amp;#160; (A similar sort of case exists with entity splitting or mixed mode mapping, but it’s easier to think about the pure TPH, TPC and TPT scenarios.)&amp;#160; In this kind of mapping, what happens if I have an instance of type B and only property c is modified?&amp;#160; Then only table B needs an update, and the rowversion would not be updated since SQL Server only maintains it for changes to the row of the table where it resides.&amp;#160; Doh!&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;After banging my head against the wall for a little while, I decided to check with some folks on the team to see what might be done to solve this problem.&amp;#160; Could we relax the restriction on concurrency properties only being allowed in the base type (in which case maybe for TPT scenarios we could add a second concurrency property on each derived type which would track changes on the extra tables)?&amp;#160; As it turns out Colin Meek, one of the brightest guys I know and a key member of the team, was way ahead of me.&amp;#160; He was able to tell me that the design for the update code that handles concurrency checks already takes this condition into account, and whenever any part of an entity which has a concurrency token is modified, the update system of the EF guarantees that the table with the concurrency value is “touched”.&amp;#160; In cases where there isn’t a real modification being made, a fake update statement is issued which doesn’t really change anything but does force the rowversion to update.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;So, in the end there was nothing to worry about.&amp;#160; The SSDL modification algorithm described above works just fine, and once the updated workflow XAML and template were put into place in the project, model first sales through cleanly and produces code which creates a database with rowversion properties for each entity type.&amp;#160; And the whole thing is verified by the successful completion of the unit test described in the first part of the article.&lt;/div&gt;

&lt;h3&gt;Conclusions&lt;/h3&gt;

&lt;div&gt;AppWeek has come and gone, and in the end I spent the whole week getting my one test to run, but we have that much more of a stable foundation for D3 work to build on.&amp;#160; In addition at least one key bug was found and fixed, and some extensibility scenarios were explored and opportunities for improvement identified.&amp;#160; The really good news, though, is that the EF in general is starting to become flexible enough that even if it doesn’t work out of the box in just the way I want, I can customize it for my needs.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;- Danny&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9806444" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Designer/default.aspx">Entity Designer</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>D3: Fun with Concurrency and Model First – Part 1</title><link>http://blogs.msdn.com/dsimmons/archive/2009/06/27/d3-fun-with-concurrency-and-model-first-part-1.aspx</link><pubDate>Sat, 27 Jun 2009 03:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806127</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9806127.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9806127</wfw:commentRss><description>&lt;P&gt;The second D3 release is now up at: &lt;A title="direct link" href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=d3&amp;amp;DownloadId=6342" mce_href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=d3&amp;amp;DownloadId=6342"&gt;d3-0.0026.zip&lt;/A&gt;&amp;nbsp; It’s the fruit of my labors pretty much full-time this week because it has been “App Week” on the EF team this week.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/appweek.jpg" mce_href="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/appweek.jpg"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: block; FLOAT: none; MARGIN-LEFT: auto; BORDER-TOP: 0px; MARGIN-RIGHT: auto; BORDER-RIGHT: 0px" title=appweek border=0 alt=appweek src="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/appweek_thumb.jpg" width=495 height=484 mce_src="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/appweek_thumb.jpg"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;What a great time!&amp;nbsp; My kids got out of school for summer break&amp;nbsp;last Friday (which means I have the option of sleeping in rather than waking up early to drive the car pool), and this week I got to almost completely clear my calendar and spend time working on DPMud.&amp;nbsp; App week is a time we set aside periodically to devote as much of our attention as possible to using the EF and related technologies to build applications so that we can experience some of the joys (and pain) of our valued customers.&amp;nbsp; Most everyone looks forward to it, and we invariably find lots of great opportunities to improve the product.&lt;/P&gt;
&lt;P&gt;While most other folks have been building new applications from scratch, I devoted my week to concentrated time on my long-running favorite app and just picked the next task off my to-do list and went to work.&amp;nbsp; That task was to add a concurrency-token property to each of my entities and a simple test to verify that optimistic concurrency checks are working correctly.&amp;nbsp; Seems pretty simple right?&amp;nbsp; The model currently only has 4 entities, so how hard could it be?&amp;nbsp; Can you say “working all week on this one task”?&amp;nbsp; :-(&lt;/P&gt;
&lt;P&gt;I won’t claim to be a test-first kind of developer, and the test code below is something I wrote after I had things working rather than first, but I’ll show it to you first so you can capture the goal.&amp;nbsp; Then we’ll talk through the challenges I encountered and how I conquered them (in the hope that I can save you some pain).&amp;nbsp; If you remember from &lt;A href="http://blogs.msdn.com/dsimmons/archive/2009/06/11/d3-release-0-0010.aspx" mce_href="http://blogs.msdn.com/dsimmons/archive/2009/06/11/d3-release-0-0010.aspx"&gt;my post about the last release&lt;/A&gt;, each entity currently in the model has a similar set of scalar properties but different relationships.&amp;nbsp; My idea was to just add a Version property which would be marked as a concurrency token and setup to be automatically updated in the database any time any property on the entity changes so that we would get automatic concurrency checks.&amp;nbsp; This property should actually have protected visibility because I don’t want to interact with it directly when using my entities—I just want the EF to use it and maintain it for me.&amp;nbsp; The result would enable this test helper method:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; VerifyConcurrency&amp;lt;T&amp;gt;(Func&amp;lt;T&amp;gt; create, Action&amp;lt;T&amp;gt; modify) &lt;SPAN style="COLOR: #0000ff"&gt;where&lt;/SPAN&gt; T : &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; (var ctx = Utilities.CreateTestContext())&lt;BR&gt;    {&lt;BR&gt;        var objectSet = ctx.CreateObjectSet&amp;lt;T&amp;gt;();&lt;BR&gt;        var obj = create();&lt;BR&gt;        objectSet.AddObject(obj);&lt;BR&gt;        ctx.SaveChanges();&lt;BR&gt;&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; (var ctx2 = Utilities.CreateTestContext())&lt;BR&gt;        {&lt;BR&gt;            var key = ctx.ObjectStateManager.GetObjectStateEntry(obj).EntityKey;&lt;BR&gt;            var obj2 = (T)ctx2.GetObjectByKey(key);&lt;BR&gt;            modify(obj2);&lt;BR&gt;            ctx2.SaveChanges();&lt;BR&gt;        }&lt;BR&gt;&lt;BR&gt;        modify(obj);&lt;BR&gt;        ctx.SaveChanges();&lt;BR&gt;    }&lt;BR&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;I made the method generic and gave it two delegate parameters so that I can easily re-use it for each entity type I currently have plus extend it as I add more types over time.&amp;nbsp; I also designed the code so that it will work with POCO types since I anticipate switching D3 to use POCO classes in the near future.&amp;nbsp; As a part of this release I also introduced a simple utility method which creates the D3Context with a connection string to my little test database—this way it’s quick and easy to type since intellisense finds things for me.&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;The basic pattern in this method is that it uses one context to add an entity to the database.&amp;nbsp; I use the create delegate because different entities have different requirements as far as what must be set on them to make them persist successfully.&amp;nbsp; Each of our current entity types requires a non-null Name property, for instance, but while the Room entity doesn’t have any other required properties, an Actor must have a non-null Room reference, etc.&amp;nbsp; &lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Once the new entity is added, I then create another context instance (which means another connection to the DB, etc.) and on it retrieve another copy of that same entity.&amp;nbsp; I do this retrieval by having the state manager in the first context lookup the entity for me by reference (works for POCO) and then give me back the EntityKey corresponding to it (also works for POCO even though the entity itself doesn’t have an EntityKey property).&amp;nbsp; Then I can use that key with the second context to load another copy of the entity, and call the modify delegate which makes a unique update to some property on the entity and saves that update to the database.&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;At this point the first instance of the entity is being tracked by the first context, and it’s concurrency token (assuming everything is configured correctly) represents the state of the entity before the second context updated anything.&amp;nbsp; So we call the modify delegate on this entity and SaveChanges which throws an OptimisticConcurrencyException because the concurrency token in the database doesn’t match what’s in this context.&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;In the actual test, I create a list of Action types which call this method specifying the appropriate type and passing in lambdas for the create and modify delegates.&amp;nbsp; Then it just loops over that list running the action and verifying that each call produces the appropriate exception.&amp;nbsp; You can look at the file Tests\Concurrency.cs in the release if you want the full details.&amp;nbsp; &lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;The point of all this, though, is that to make this test pass I needed my entities to have concurrency properties that were configured properly in the database.&amp;nbsp; With recent versions of SQL Server, the best way to accomplish that is with a &lt;A href="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/ConcurrencyProperty.jpg" mce_href="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/ConcurrencyProperty.jpg"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; MARGIN-LEFT: 0px; BORDER-TOP: 0px; MARGIN-RIGHT: 0px; BORDER-RIGHT: 0px" title=ConcurrencyProperty border=0 alt=ConcurrencyProperty align=right src="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/ConcurrencyProperty_thumb.jpg" width=280 height=392 mce_src="http://www.the-simmons.net/images/D3FunwithConcurrencyandModelFirst_E7DE/ConcurrencyProperty_thumb.jpg"&gt;&lt;/A&gt;RowVersion column (previously called a timestamp).&amp;nbsp; This special column type is stored as an 8 byte binary, and the server guarantees that it will have a unique value any time any part of a row in its table is modified.&amp;nbsp; The trick is that this special type only exists in the server—it’s not available in the CLR or the EDM conceptual model.&amp;nbsp; If I were reverse engineering a model from the database, that would be no big deal.&amp;nbsp; The tools would automatically map the RowVersion column in the database to an 8 byte binary property in my model.&amp;nbsp; In this case, though, I’m using model first to generate the database from my model, and when I create the property, model first has no way to know that it should create a RowVersion type rather than a binary type in the DB.&lt;/DIV&gt;
&lt;P&gt;So it was time to customize the model first process again, but before I got even that far I ran into a bug.&amp;nbsp; Ahhh…&amp;nbsp; The joy of AppWeek and of beta software.&amp;nbsp; The issue is that if you set Concurrency Mode to Fixed on any of your entities in VS 2010 beta 1, then when you choose Generate Database Script from Model, the tool will display an error rather than generating the script.&amp;nbsp; Considerable debugging and discussion with the developer who did most all of the work for model first later, we were able to track the problem down to a one line problem where model first was assuming that all property facets that could be set in the conceptual model were at least valid possibilities for the storage model, when in fact Concurrency Mode is the one property which can only be in the conceptual model and not the storage model.&amp;nbsp; So the workflow activity that generates the SSDL from the CSDL was triggering an exception from the metadata system.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;This bug will be fixed in beta 2, but that doesn’t help us much for D3 right now.&amp;nbsp; So my final solution was to extract a small piece of the model-first workflow into a separate DLL, make a fix to the code and then modify the DbGen.xaml file which describes the workflow to call out to that DLL rather than the core version that ships with the product.&amp;nbsp; The final result is part of this release, and it’s called modelfirst-bugfix.dll.&amp;nbsp; If you go the misc directory under the release and run the batch file install-bugfix.cmd it will copy the DLL to a directory in your VS installation (make sure you run this from a VS command prompt where the DevEnvDir environment variable is set).&amp;nbsp; Naturally this little hack is completely unsupported, but it works for me, and it might work for you too if you need to use model first with concurrency and beta 1 of EF4.&lt;/P&gt;
&lt;P&gt;Now at least I can run model first when I have concurrency configured, but I’ve still got the problem that my properties come through as binary rather than RowVersion type in the DB.&amp;nbsp; That’s where I’ll pick up the next post…&amp;nbsp; Until then, happy coding.&lt;/P&gt;
&lt;P&gt;- Danny&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9806127" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>D3: Release 0.0010</title><link>http://blogs.msdn.com/dsimmons/archive/2009/06/11/d3-release-0-0010.aspx</link><pubDate>Thu, 11 Jun 2009 02:22:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9725285</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9725285.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9725285</wfw:commentRss><description>&lt;p&gt;OK folks, after way too much delay, I’ve finally gotten all my ducks in a row and begun the process of rewriting DPMud.&amp;#160; Since my goal is to completely rebuild it from scratch, the process is going to take a while, and I intend to let you all look over my shoulder a bit as I go.&amp;#160; So don’t expect too much from this first release—all we have is a basic solution with a very simple model and some unit tests.&amp;#160; You can download the release from: &lt;a title="d3-0.0010" href="http://code.msdn.microsoft.com/d3/Release/ProjectReleases.aspx?ReleaseId=2815"&gt;d3-0.0010&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;First, let’s take a look at a few decisions that came up along the way, and then we’ll dig into some specific code areas.&lt;/p&gt;  &lt;h3&gt;&lt;/h3&gt;  &lt;h3&gt;Decisions&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Source Control and Licensing&lt;/strong&gt;: It is very important to me to make this project as relevant and useful as possible for folks trying to build applications using the EF.&amp;#160; So we decided to release the code via MS-PL which is a very permissive license.&amp;#160; I’m no lawyer, but my understanding is that this basically means you can copy the source and use any part or all of it in your own projects.&amp;#160; Originally it was my hope that I could just put the project on codeplex and use it for source control so that anyone interested could enlist and just sync down changes to their local machine as the project progresses, but there were various hang-ups with this approach, and in the end I decided to just use local source control and then periodically push releases up to code gallery which has a simple site system and where everything is automatically licensed with MS-PL.       &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Version Numbers&lt;/strong&gt;: One of those mundane but required decisions along the way is how we will create version numbers for the releases.&amp;#160; Here’s what I decided: Until we have the thing up and running, everything is version 0.something.&amp;#160; The last four digits of the version are in the form MMDD where MM is the number of months since I started releasing (so 00 is June ‘09), and DD is the day of the month for that release.&amp;#160; I haven’t done it yet, but that allows me to have a very simple algorithm to automate version stamping and such as I make subsequent releases.       &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;EF Versions and Dependencies&lt;/strong&gt;: One critical area of decisions was around what EF features do I take dependencies on—particularly when we’re in the state where EF4 is not yet complete.&amp;#160; In the end I decided that the main principle was to avoid taking dependencies on EF features that aren’t yet available outside the team so that you can stay up with me if you like.&amp;#160; This means, for instance, that I’m not going to take a dependency on foreign-key support even though I have access to an internal build where it’s working great.&amp;#160; When FKs are publically available, we’ll take a look at the project at that point and evaluate whether or not it’s worthwhile to refactor the app to take advantage of them (almost certainly we will do that refactoring).&amp;#160; Similarly, I’m not yet going to take a dependency on self-tracking entities because it isn’t yet available.&amp;#160; It will become available much sooner than FKs, though, so I will plan on taking advantage of it soon.&amp;#160; A harder question was code-only.&amp;#160; The first CTP of it will also be available soon (in the same download with self-tracking entities), but I already know of some areas where I’ll need more control over the model and database generation than code-only will support in its first CTP, so we’ll have to wait on that refactoring until a later CTP of code-only.&amp;#160; So the basic approach will be to use EF4 beta 1 with model-first to make it efficient to design the model and generate the database and code from that model. &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Initial Model&lt;/h3&gt;  &lt;p&gt;Like every other part of the system, we’re going to start with a simple model and expand.&amp;#160; Here’s a first look:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.the-simmons.net/images/D3Release0.0010_AFC1/D3ModelDiagram.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="D3ModelDiagram" border="0" alt="D3ModelDiagram" src="http://www.the-simmons.net/images/D3Release0.0010_AFC1/D3ModelDiagram_thumb.jpg" width="475" height="509" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;A few things to notice: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Each entity has an Id property which is a server-generated integer.&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;Each of these entities is a “real” thing which players will interact with in the virtual environment of the game, and as such we have given them all a few common properties including Name and Description.&amp;#160; Eventually we’ll define an interface for these common properties so that we can reason about the commonality of these objects.&amp;#160; We could have an abstract base class for each of these entities, but we want them each in their own entity sets, and the designer doesn’t support more than one entityset with the same base type, so using an interface gets the key aspects we want while allowing us to still use the designer (we don’t really need implementation inheritance here anyway).      &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;The two associations between Room and Exit have each had one of the navigation properties removed so that Room only has one collection of “Exits” (it doesn’t also have a collection of entrances, so to speak), and Exit has only the TargetRoom navigation property.&amp;#160; This makes our entities follow the intended model where exits go only one way.&amp;#160; If you want two rooms to be connected in a symmetrical fashion, you need two exits one going from room A to room B, and another one going back from room B to room A.&amp;#160; This is important because it means that we can easily give different names to the exits (from room A you go East to room B, but from room B you have to go West to get back to room A), and it means that you don’t always have symmetrical paths (when you jump off a cliff you can’t necessarily get back to the top of the cliff using the same way you got down, etc.).      &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;We don’t yet have version fields for concurrency checking.&amp;#160; That will come later, because it requires some additional customization to the database generation.      &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;Item has a relationship with Actor and one with Room, but logically an item can only be related to one of them at a time (and it must always be related to one or the other so that items have location).&amp;#160; These constraints we will represent in the database rather than the conceptual model.&amp;#160; They aren’t currently enforced, but they will be once we make additional customizations to database generation. &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Customizing “Model First”&lt;/h3&gt;  &lt;p&gt;The feature we call model first shows up in the entity designer (aka Escher) as the option “Generate Database Script from Model” on the context menu when you right click on an open part of the designer surface.&amp;#160; Out of the box this will generate a SQL DDL script that you can use to create a database for persisting the entities in your model.&amp;#160; It also automatically generates the SSDL and MSL which corresponds to that model.&amp;#160; This is great because it means that I can concentrate on the thing that is important to my program, the conceptual model.&amp;#160; If I make a change to the model, once that change is complete, I’m done.&amp;#160; I don’t have to also modify the database and the mapping to reflect that change.&amp;#160; Since I’m also generating entity classes from the model, I get a nice “DRY” (Don’t Repeat Yourself) development experience even though various parts of the system do have repetition, I don’t have to engage in that repetition myself.&amp;#160; Naturally we want to have an even more DRY experience, and code-first will lead us there for folks who want to do everything in code, but for the design-time experience this is not half bad.&lt;/p&gt;  &lt;p&gt;Naturally, though, there are times where I want more control over the database generation process, which is why there are customization hooks built-in.&amp;#160; The process of generating the database is orchestrated by a customizable workflow which has a couple of activities that manage generating an MSL and SSDL for the CSDL and then generating SQL from that SSDL.&amp;#160; This second step (SSDL-&amp;gt;SQL) is further customizable because it is accomplished with a T4 template.&amp;#160;&amp;#160; Eventually we will take advantage of these customization mechanisms to allow us to automatically generate rowversion columns for concurrency, constraints that keep item relationships straight and other things.&lt;/p&gt;  &lt;p&gt;All of this seemed somewhat academic, though, for the first few phases of the project until I started thinking through my testing strategy.&amp;#160; Eventually we’ll test parts of the system using fakes and mocks and the like, but we also need integration tests that send data all the way down to the database and back, and I wanted to start writing those tests right away and deliver them with my initial stab at the model.&amp;#160; To make that work I really wanted APIs for creating and dropping the database somewhat like we can get from L2S so that my tests can create a temp version of the database (separate storage but same schema) and test pushing data in and out of it.&amp;#160; Eventually this will also be useful for deployment, import/export of data, version-to-version upgrades and the like.&lt;/p&gt;  &lt;p&gt;So, I decided to customize the db generation process now by modifying the SSDL-&amp;gt;SQL template to generate a C# method for creating the database schema rather than a SQL script.&amp;#160; The process to set things up for customization looks like this:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Copy the workflow xaml file from %ProgramFiles\Microsoft Visual Studio 10.0\Extensions\EntityFrameworkTools\Workflows\DbGen.xaml and the SQL gen template from %ProgramFiles\Microsoft Visual Studio 10.0\Extensions\EntityFrameworkTools\Templates\SsdlToSql10.tt to my project.&amp;#160; &lt;/li&gt;    &lt;li&gt;I renamed the template to SsdlToCode.tt and modified the xaml file’s TemplatePath to just have that name rather than the full path to the original template.&amp;#160; &lt;/li&gt;    &lt;li&gt;Both files I added to my project in VS, but because they aren’t actually used at compile time (they are just used by the designer when I choose the menu option to generate the database) I made sure that the Custom Tool property for each of them is blank and the Build Action property is set to None. &lt;/li&gt;    &lt;li&gt;I clicked on a blank part of the Entity Designer surface and then set the “Generate Database Script Workflow” property to just DbGen.xaml (just the file name because the xaml file is in the same project and directory with the EDMX). &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Now when I choose the option to generate the database it runs the workflow as specified in my project which then uses the template from my project rather than the default versions.&lt;/p&gt;  &lt;p&gt;Next, I modified the template file to output C# code rather than SQL.&amp;#160; You can find the template file in the release and do your own diff against the default template, but the basic approach was to wrap each batch of SQL statements up to a “GO” in an ado.net command with a C# @”” string and execute it.&amp;#160; I eliminated a chunk of the template that would drop existing tables because my approach is to drop the whole database and recreate it, and I put all of these execute statements into a method on an internal partial class that I added to my model DLL.&amp;#160; That way most of that class is defined outside the template but this one method can be defined by the code generated from the template.&amp;#160; Then I further created a partial class file for my generated EF context and added methods to check for existence of the database, drop the database, and create it (using the generated method).&lt;/p&gt;  &lt;p&gt;One current limitation in the designer is that the generate database command always by default creates a file with a name based on the name of the EDMX file and with SQL for the extension.&amp;#160; I’ve already started discussions with the designer folks about adding some customization hooks here, but for now you just have to remember that whenever you generate the database you should rename the file.&lt;/p&gt;  &lt;p&gt;All of this enabled me to write the following simple test:&lt;/p&gt;  &lt;div&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[TestMethod]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DropAndCreate()&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var ctx = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; D3Context(&lt;span style="color: #006080"&gt;&amp;quot;name=D3TestContext&amp;quot;&lt;/span&gt;))&lt;br /&gt;    {&lt;br /&gt;        ctx.DropDatabase();&lt;br /&gt;        Assert.IsFalse(ctx.DatabaseExists());&lt;br /&gt;        ctx.CreateDatabase();&lt;br /&gt;        Assert.IsTrue(ctx.DatabaseExists());&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;Not only is it simple to perform these operations, but also everything is based on the connection string given.&amp;#160; So I just created two different connection strings in my app.config file—one called D3Context (the default) and the D3TestContext which is the exact same as the other except that it has a different initial-catalog name for my test database.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;h3&gt;The SqlDb Internal Class&lt;/h3&gt;

&lt;div&gt;It’s probably also worthwhile to take a brief look at the internal class I called SqlDb which does the heavy lifting for these database interactions.&amp;#160; The first interesting piece of the puzzle is that in order to create or drop the database with SQL Server (or in this case Sql Express) we need to make sure that we don’t use a connection string with the database in question as the initial-catalog.&amp;#160; So, we extract the connection string from the StoreConnection property on the EntityClient connection instance created by the context and create a SqlConnectionBuilder from it.&amp;#160; SqlDb’s constructor takes a DbConnection parameter since that’s what ObjectContext exposes and creates the builder like this:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var builder = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SqlConnectionStringBuilder(((EntityConnection)entityConnection).StoreConnection.ConnectionString);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Then we store the initial-catalog property into a string field so we know the name of the database we should be working with, and then we replace the initial-catalog with “master” since that database is present on every SQL instance.&lt;/p&gt;

&lt;p&gt;The constructor then extracts the new connection string from the builder and uses it to create a new SqlConnection instance.&amp;#160; We use this connection to execute commands without directly connecting to the database.&amp;#160; The class also implements IDisposable so that its Dispose method can Dispose of the SqlConnection.&lt;/p&gt;

&lt;p&gt;The other interesting trick which came up was the fact that while my initial executions of the above test succeeded while stepping through in the debugger, running the whole test straight through failed because the first part of the DropDatabase method executes a command to check if the database exists, and after that the drop command would fail with an error saying that the database was in use.&amp;#160; Eventually I fixed this by adding a ClearAllPools method to the class which closes the connection, calls SqlClient.ClearAllPools() to make sure that there are no connections in the connection pool holding onto the database and then re-opens the connection.&amp;#160; This method is called after the existence check and before the command to drop the database.&lt;/p&gt;

&lt;h3&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;We’re finally off and running!&amp;#160; We have an initial model, a simple way to deploy the database and some initial tests.&amp;#160; (Those tests have already found a couple of issues I had in my first crack at the model, by the way.&amp;#160; So the testing is already paying off.&amp;#160; Yay!)&amp;#160; Next I’ll probably tackle some further customizations of the generated database.&amp;#160; Soon we’ll look at replacing the default code gen with a POCO template, and in the not distant future I hope to fill out more pieces of the architecture so we can get a small end-to-end slice going with a WCF service and Silverlight, but all in good time…&lt;/p&gt;

&lt;p&gt;- Danny 
  &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9725285" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>D3: Using T4 to Generate Entity and Context Classes</title><link>http://blogs.msdn.com/dsimmons/archive/2009/04/08/d3-using-t4-to-generate-entity-and-context-classes.aspx</link><pubDate>Wed, 08 Apr 2009 10:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9537585</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9537585.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9537585</wfw:commentRss><description>&lt;P&gt;Sorry for the DPMud hiatus.&amp;nbsp; Here’s another installment for those interested in the process.&amp;nbsp; I’m still trying to get a public source repository up.&amp;nbsp; Until that’s functional I’m not going to do a lot of development because I want to share the process with you.&amp;nbsp; In the meantime I’m still laying groundwork where I can, and one aspect of that is using T4 to generate classes for the entities and for the data access layer.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;EM&gt;Side Note: We’ve been trying hard to refer to EF versions using the corresponding .net version rather than “v1” and “v2” since talking in terms of .net versions makes it clear when and how the corresponding versions ship.&amp;nbsp; So don’t be surprised when I refer to EF4 or EF3.5.&lt;/EM&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The idea behind using T4 for code generation is that I’d like to create the DPMud model using the EF Designer and then generate both the classes and the database from the model (&lt;A href="http://blogs.msdn.com/efdesign/archive/2008/09/10/model-first.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/09/10/model-first.aspx"&gt;database generation has been discussed previously on the EF Design blog&lt;/A&gt;).&amp;nbsp; There are two key architectural requirements for these classes that we generate:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The entities should be completely &lt;STRONG&gt;persistence ignorant&lt;/STRONG&gt;.&amp;nbsp; That is, they should live in an assembly which doesn’t have any dependency on Entity Framework or ADO.Net DLLs.&amp;nbsp; One concrete reason for this is that I want to reuse the entity classes in Silverlight.&amp;nbsp; Happily, EF4 adds extensive support for POCO entity classes. &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;The DAL should be designed in a way that supports easy introduction of a fake implementation.&amp;nbsp; That is, it should be a “seam” in the terminology of design for testability.&amp;nbsp; Another feature of EF4 which supports this is the introduction of a new interface, IObjectSet&amp;lt;T&amp;gt; which is implemented by the ObjectSet&amp;lt;T&amp;gt; class which provides both a starting point for queries and a place to add and remove entities of that type.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;So I created a couple T4 templates.&amp;nbsp; They share some infrastructure for helping to read EF metadata, escaping identifiers, generating multiple files, one per class, etc.&amp;nbsp; We’re working on getting that infrastructure into the core product, so I’ll concentrate on the unique parts of these templates.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The entity template iterates over each entity type in the metadata and generates a partial class with each property marked virtual so that an automatic EF proxy can be generated if necessary.&amp;nbsp; For navigation properties, it generates properties that are simple CLR references or ICollection&amp;lt;T&amp;gt; instances with an implementation that does fixup.&amp;nbsp; The fixup implementation is generated so that it can be included in the entity DLL and not introduce any other requirements to the entity classes.&lt;/P&gt;
&lt;P&gt;The context template generates a class which inherits from ObjectContext and has properties that return ObjectSet&amp;lt;T&amp;gt; instances.&amp;nbsp; It also generates an interface for the seam with IObjectSet&amp;lt;T&amp;gt; properties and a fake context implementation which has fake object set instances which have a very simple wrapper around hashset&amp;lt;T&amp;gt;.&lt;/P&gt;
&lt;P&gt;This way I can put the entities into one DLL and the context, interface and fake context into a separate DLL for the DAL.&lt;/P&gt;
&lt;P&gt;- Danny&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9537585" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>D3: Modeling Part 1 – Real-World Relationships</title><link>http://blogs.msdn.com/dsimmons/archive/2009/02/18/d3-modeling-part-1-real-world-relationships.aspx</link><pubDate>Wed, 18 Feb 2009 11:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9430523</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9430523.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9430523</wfw:commentRss><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;While there are a great many scenarios for “standard” associations in the EF, there are cases where an application requires something a little more sophisticated.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;DPMud is no different.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It has two cases.&lt;/FONT&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT face=Cambria color=#4f81bd size=4&gt;Relationships with Payload&lt;/FONT&gt;&lt;/H2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;A key part of the D3 model is the map which consists of rooms which have a many-many relationship.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Each room can have exits which lead to other rooms.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This would be pretty straightforward except we need to attach properties to the relationship for the name of the exit, whether or not it is locked, etc.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In UML terms this would be called an association class.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Ideally the EF would be able to directly support this kind of relationship, but there are a number of interesting semantics, and in the end the properties on the relationship amount to an entity with all the same sorts of behaviors.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The main problem, though, is that you need both the ability to navigate directly across the relationship skipping the properties and the ability to access the entity “in the middle”.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;The only real option here is to replace two entities and one relationship plus payload with three entities and two relationships:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;IMG title="Rooms and Exits Diagram" style="WIDTH: 379px; HEIGHT: 292px" height=292 alt="Rooms and Exits Diagram" src="http://www.the-simmons.net/dpmud/d3-rooms-and-exits.jpg" width=379 mce_src="http://www.the-simmons.net/dpmud/d3-rooms-and-exits.jpg"&gt;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;You can easily enumerate the exits from a room, and if you want to transit from that room through an exit to an adjacent room, then you can navigate from the room to the exit and then to the destination room.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;(You might notice, by the way, that there’s only one navigation property on Room and only one on Exit even though each of these participates in two associations.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I accomplished this by manually editing the XML of the EDMX to get rid of the extra navigation properties.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I call them extra because they confuse the common scenarios—it’s interesting to enumerate the exits leading out from a room but not to list the entrances, and similarly it’s much more useful to look at the destination of an exit rather than the source.)&lt;/FONT&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT face=Cambria color=#4f81bd size=4&gt;Conditional Relationships&lt;/FONT&gt;&lt;/H2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;There are lots of kinds of conditions you might want to put on a relationship—maybe the customer entity only has a salesman if their purchases total more than $5000 over the last month or something.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In general the EDM doesn’t provide facilities for describing arbitrary conditions on associations, behaviors of that sort should be implemented in business logic on the entity classes.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;There is a pattern which comes up occasionally, including in D3, which is worth looking at: Sometimes an entity might participate in multiple relationships but where only one of the relationships can be present at any one time.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;In the case of D3, this shows up with Items.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;An Item can be in a Room, it can be carried by an Actor, or (at least in theory, we haven’t added this feature to DPMud yet) inside another Item like a backpack.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;While the EDM can’t directly capture this kind of constraint in the conceptual model, this can be accomplished fairly easily in business logic in the entities, and it’s quite easy to add constraints in the database itself that require that only one of the three FK properties on Item be non-null at any one time.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;You might say that we could create a base type for Items, Rooms and Actors, called “ItemContainer” or something like that and then a relationship between Item and ItemContainer, and that approach could certainly work.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;There are consequences, though.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;First of all, we would either have to put all three entity types in the same entity set which is less usable (default context pattern would have one ItemContainers set rather than three sets: Items, Rooms and Actors).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Also there could be some perf implications since deeper inheritance hierarchies with relationships affecting subtypes can cost more during view generation and verification and the default database schema would put all three entity types into a single table which probably doesn’t give the ideal locality of reference for common queries, etc.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If we try to address these concerns by creating separate entity sets for the three subtypes rather than one top-level entity set, then the EF runtime could handle it, but the EF designer could not, and we would be forced to do all subsequent modeling by editing the XML directly which is certainly less than ideal.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In the end, three separate relationships with a constraint just seems to be a smoother path, so that’s the approach we take in D3.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;I suppose that’s all for tonight.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Next time we’ll continue looking at D3 modeling decisions.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Hopefully I’ll be able to work through the issues so that I can set up a source repository and start sharing out code before too long.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;- Danny&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;P.S. As we walk through this process together, if a better approach occurs to you about one of these design decisions or if I gloss over something that needs more explanation, please don’t hesitate to say so.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It’s my hope that we can all learn from one another.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9430523" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>D3: Basic Architecture Decisions</title><link>http://blogs.msdn.com/dsimmons/archive/2009/02/15/d3-basic-architecture-decisions.aspx</link><pubDate>Mon, 16 Feb 2009 01:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9424923</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9424923.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9424923</wfw:commentRss><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;In order to decide on the basic architecture for DPMud&lt;SUP&gt;3&lt;/SUP&gt; (I’ll just call it D3 from here on out), I first needed to build an understanding of the domain, then consider the goals and synthesize a basic approach.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;The good news for me is that this is the third major version of this particular application, and in years past I spent way too many hours spending other MUDs and similar games, so I’m pretty well versed in the domain.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;To give a very quick overview: &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;The key parts are that we model a virtual world in terms of rooms, passages between those rooms, items and actors which can either be players or non-player characters controlled by the game.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Interaction happens in real-time via text which means that players express their intentions through simple text commands which must be parsed, and they learn about their environment through textual descriptions of things and events at their locale.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The system needs a way to maintain a shared state for all the players, to communicate events as they occur (initiated by players or by other parts of the system such as computer controlled actors) and a framework for creating the AI that brings the non-player actors to life.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;The first goal for this exercise is to build a real application not just a collection of samples artificially glued together but at the same time to try out new EF features such as POCO, model-first, mockable-interfaces, the n-tier APIs, T4 for code generation, etc.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Another key goal is to use good architectural principles and popular app building patterns which means carefully separating concerns, using Inversion of Control to facilitate mocking and test driven (or at least very carefully tested) development, to create an n-tier architecture with WCF-services and a Silverlight client, and to use a model-view-controller pattern to simplify the creation of multiple views and controllers which share a common model.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;The biggest challenge in synthesizing a design out of this domain and these goals is reconciling the multi-player real-time communication aspects with the goal to use and demonstrate EF (and to do so in a way that will be educational to folks building business apps rather than old-style multi-user games).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The DPMud solution from the beginning has been to ask that you suspend disbelief about one architectural decision: we are going to use transacted database updates as a real-time communication mechanism.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;While this is almost certainly not the most efficient, scalable way to implement real-time communication, it does mesh very well with the EF and with the rest of our goals.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It also has the potential to be a nice stress-test for the systems involved, and we do have the option to apply more hardware and more sophisticated build-outs to address any scalability problems we encounter.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri size=3&gt;Given that basic decision and the architectural pattern goals listed above, we end up with a 10,000 foot architecture diagram that looks something like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;IMG title="DPMud 3 Architecture Diagram" style="WIDTH: 732px; HEIGHT: 654px" height=654 alt="DPMud 3 Architecture Diagram" src="http://www.the-simmons.net/dpmud/d3-arch.jpg" width=732 mce_src="http://www.the-simmons.net/dpmud/d3-arch.jpg"&gt;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Next time…&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;We’ll build up the model digging into some of the particular modelling difficulties that come up along the way.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;FONT face=Calibri size=3&gt;- Danny&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9424923" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item><item><title>DPMud</title><link>http://blogs.msdn.com/dsimmons/archive/2009/02/13/dpmud.aspx</link><pubDate>Fri, 13 Feb 2009 11:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9417741</guid><dc:creator>dsimmons@microsoft.com</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/dsimmons/comments/9417741.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dsimmons/commentrss.aspx?PostID=9417741</wfw:commentRss><description>&lt;P&gt;Back when the Entity Framework first started coming together as a product—well before any sane person outside the product team would actually try to use it to build an end-to-end solution—we had an application building contest within the team which we called ADO.NET Idol.&amp;nbsp; The idea was to get a number of people to explore what it’s like to use the product in the way we intend for our customers to use it.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I'm always up for a challenge, and a couple friends of mine from elsewhere in Microsoft had been bugging me for a while to write a game together.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;So I dropped them an email and that was the beginning of a project we unimaginatively named DPMud.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;DP, of course, was for Data Programmability, and Mud was because the game we decided to build was a multi-user dungeon (see &lt;A href="http://en.wikipedia.org/wiki/MUD"&gt;this page&lt;/A&gt; on wikipedia for some background on MUDs).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;One of my friends, Josh, was working on AI in the FlightSim/TrainSim team, and he was excited to help out not only with general coding, but especially with adding some AI.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;My other buddy, Jay, is a pretty serious gamer and a really creative guy, so I knew that he would be a great resource for story-line and content for the game.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Jay got the process off to a good start by whipping up some concept art that has been part of DPMud ever since:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG title=Rar style="WIDTH: 284px; HEIGHT: 281px" height=281 alt=Rar src="http://www.the-simmons.net/dpmud/rar.jpg" width=284 mce_src="http://www.the-simmons.net/dpmud/rar.jpg"&gt;&lt;/P&gt;
&lt;P&gt;I believe the phrase "zombie chicks with shotguns" went with the diagram.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Thus, in early April 2006, began a project which has off and on occupied a lot of my spare time ever since.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The first version which had all of the basic elements working was completed on May 24, 2006.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;We continued to improve and expand the game for some time after that, and meanwhile the Entity Framework was expanded and revised to the point that a year after we started the effort, in April 2007, we began version 2, "DPMud Rarloaded" which was a pretty major rewrite to take advantage of new EF features.&lt;/P&gt;
&lt;P&gt;Now, after a hiatus of quite some time, the major features of the second release of the EF are coming together, which creates the opportunity for some significant architectural improvements. So I've decided it's time for version 3 which we're going to call DPMud3: The Zombies Search for Spock.&lt;/P&gt;
&lt;P&gt;So why am I telling you about all this?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This time my plan is to share the project with all of you.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In fact, I intend to build the app from scratch and journal the process as I go in the hope that some of you may find the journey educational.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I'm sure that I will learn a lot, and I trust that if you spot something I could do better, you will let me know.&lt;/P&gt;
&lt;P&gt;I guess that's all for tonight.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Next time I'll share some more details about project goals and architecture ideas, but before I can actually start sharing source code, I've got to engage in one of those tasks that tend to show up in the early stages of a great many projects: working out some details with legal.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;- Danny&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9417741" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dsimmons/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/dsimmons/archive/tags/D3/default.aspx">D3</category></item></channel></rss>