<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ian Mercer &#187; SQL</title>
	<atom:link href="http://blog.abodit.com/category/programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.abodit.com</link>
	<description>Living in the World&#039;s Smartest House</description>
	<lastBuildDate>Sat, 07 Jan 2012 19:50:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Semantic Web ontology / triple Store built on MongoDB</title>
		<link>http://blog.abodit.com/2011/01/a-semantic-web-ontology-triple-store-built-on-mongodb/</link>
		<comments>http://blog.abodit.com/2011/01/a-semantic-web-ontology-triple-store-built-on-mongodb/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 19:53:36 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[My News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[triple store]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=1351</guid>
		<description><![CDATA[In a previous blog post I discussed building a Semantic Triple Store using SQL Server. That approach works fine but I&#8217;m struck by how many joins are needed to get any results from the data and as I look to storing much larger ontologies containing billions of triples there are many potential scalability issues with <a href="http://blog.abodit.com/2011/01/a-semantic-web-ontology-triple-store-built-on-mongodb/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>In a previous blog post I discussed building a Semantic Triple Store using SQL Server.  That approach works fine but I&#8217;m struck by how many joins are needed to get any results from the data and as I look to storing much larger ontologies containing billions of triples there are many potential scalability issues with this approach.  So over the past few evenings I decided to try a different approach and so I created a semantic store based on MongoDB.  In the MongoDB version of my semantic store I take a different approach to storing the basic building blocks of semantic knowledge representation.  For starters I decided that typical ABox and TBox knowledge has really quite different storage requirements and that smashing all the complex TBox assertions into simple triples and stringing them together with meta fields only to immediately join then back up whenever needed just seemed like a bad idea from the NOSQL / document-database perspective.</p>
<p>TBox/ABox: In the ABox you typically find simple triples of the form <em>X-predicate-Y</em>.  These store simple assertions about individuals and classes.  In the TBox you typically find complex sequents, that&#8217;s to say complex logic statements having a head (or consequent) and a body (or antecedents).  The head is &#8216;entailed&#8217; by the body, which means that if you can satisfy all of the body statements then the head is true.  In a traditional store all the ABox assertions can be represented as triples and all the complex TBox assertions use quads with a meta field that is used solely to rebuild the sequent with a head and a body.  The ABox/TBox distinction is however arbitrary (see http://www.semanticoverflow.com/questions/1107/why-is-it-necessary-to-split-reasoning-into-t-box-and-a-box).</p>
<p>I also decided that I wanted to be use ObjectIds as the primary way of referring to any Entity in the store.  Using the full Uri for every Entity is of course possible and MongoDB couuld have used that as the index but I wanted to make this efficient and easily shardable across multiple MongoDB servers.  The MongoDB ObjectID is ideal for that purpose and will make queries and indexing more efficient.</p>
<p>The first step then was to create a collection that would hold Entities and would permit the mapping from Uri to ObjectId.   That was easy: an Entity type inheriting from a Resource type produces a simple document like the one shown below.  An index on Uri with a unique condition ensures that it&#8217;s easy to look up any Entity by Uri and that there can only ever be one mapping to an Id for any Uri.</p>
<pre class="brush: jscript; title: ; notranslate">

RESOURCES COLLECTION - SAMPLE DOCUMENT

{
  &quot;_id&quot;: &quot;4d243af69b1f26166cb7606b&quot;,
  &quot;_t&quot;: &quot;Entity&quot;,
  &quot;Uri&quot;: &quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#first&quot;
}
</pre>
<p>Although I should use a proper Uri for every Entity I also decided to allow arbitrary strings to be used here so if you are building a simple ontology that never needs to go beyond the bounds of this one system you can forgo namespaces and http:// prefixes and just put a string there, e.g. &#8220;SELLS&#8221;.  Since every Entity reference is immediately mapped to an Id and that Id is used throughout the rest of the system it really doesn&#8217;t matter much.</p>
<p>The next step was to represent simple ABox assertions.  Rather than storing each assertion as its own document I created a document that could hold several assertions all related to the same subject.  Of course, if there are too many assertions you&#8217;ll still need to split them up into separate documents but that&#8217;s easy to do.  This move was mainly a convenience for developing the system as it makes it easy to look at all the assertions made concerning a single Entity using MongoVue or the Mongo command line interface but I&#8217;m hoping it will also help performance as typical access patterns need to bring in all of the statements concerning a given Entity.</p>
<p>Where a statement requires a literal the literal is stored directly in the document and since literals don&#8217;t have Uris there is no entry in the resources collection.</p>
<p>To make searches for statements easy and fast I added an array field &#8220;SPO&#8221; which stores the set of all Ids mentioned anywhere in any of the statements in the document.  This array is indexed in MongoDB using the array indexing feature which makes it very efficient to find and fetch every document that mentions a particular Entity.  If the Entity only ever appears in the subject position in statements that search will result in possibly just one document coming back which contains all of the assertions about that Entity.  For example:</p>
<pre class="brush: jscript; title: ; notranslate">

STATEMENTGROUPS COLLECTION - SAMPLE DOCUMENT

{
  &quot;_id&quot;: &quot;4d243af99b1f26166cb760c6&quot;,
  &quot;SPO&quot;: [
    &quot;4d243af69b1f26166cb7606f&quot;,
    &quot;4d243af69b1f26166cb76079&quot;,
    &quot;4d243af69b1f26166cb7607c&quot;
  ],
  &quot;Statements&quot;: [
    {
      &quot;_id&quot;: &quot;4d243af99b1f26166cb760c5&quot;,
      &quot;Subject&quot;: {
        &quot;_t&quot;: &quot;Entity&quot;,
        &quot;_id&quot;: &quot;4d243af69b1f26166cb7606f&quot;,
        &quot;Uri&quot;: &quot;GROCERYSTORE&quot;
      },
      &quot;Predicate&quot;: {
        &quot;_t&quot;: &quot;Entity&quot;,
        &quot;_id&quot;: &quot;4d243af69b1f26166cb7607c&quot;,
        &quot;Uri&quot;: &quot;SELLS&quot;
      },
      &quot;Object&quot;: {
        &quot;_t&quot;: &quot;Entity&quot;,
        &quot;_id&quot;: &quot;4d243af69b1f26166cb76079&quot;,
        &quot;Uri&quot;: &quot;DAIRY&quot;
      }
    }
	... more statements here ...
  ]
}
</pre>
<p>The third and final collection I created is used to store TBox sequents consisting of a head (consequent) and a body (antecedents).  Once again I added an array which indexes all of the Entities mentioned anywhere in any of the statements used in the sequent.  Below that I have an array of Antecedent statements and then a single Consequent statement.  Although the statements don&#8217;t really need the full serialized version of an Entity (all they need is the _id) I include the Uri and type for each Entity for now.  Variables also have Id values but unlike Entities, variables are not stored in the Resources collection, they exist only in the Rule collection as part of consequent statements.  Variables have no meaning outside a consequent unless they are bound to some other value.</p>
<pre class="brush: jscript; title: ; notranslate">

RULE COLLECTION - SAMPLE DOCUMENT

{
  &quot;_id&quot;: &quot;4d243af99b1f26166cb76102&quot;,
  &quot;References&quot;: [
    &quot;4d243af69b1f26166cb7607d&quot;,
    &quot;4d243af99b1f26166cb760f8&quot;,
    &quot;4d243af99b1f26166cb760fa&quot;,
    &quot;4d243af99b1f26166cb760fc&quot;,
    &quot;4d243af99b1f26166cb760fe&quot;
  ],
  &quot;Antecedents&quot;: [
    {
      &quot;_id&quot;: &quot;4d243af99b1f26166cb760ff&quot;,
      &quot;Subject&quot;: {
        &quot;_t&quot;: &quot;Variable&quot;,
        &quot;_id&quot;: &quot;4d243af99b1f26166cb760f8&quot;,
        &quot;Uri&quot;: &quot;V3-Subclass8&quot;
      },
      &quot;Predicate&quot;: {
        &quot;_t&quot;: &quot;Entity&quot;,
        &quot;_id&quot;: &quot;4d243af69b1f26166cb7607d&quot;,
        &quot;Uri&quot;: &quot;rdfs:subClassOf&quot;
      },
      &quot;Object&quot;: {
        &quot;_t&quot;: &quot;Variable&quot;,
        &quot;_id&quot;: &quot;4d243af99b1f26166cb760fa&quot;,
        &quot;Uri&quot;: &quot;V3-Class9&quot;
      }
    },
    {
      &quot;_id&quot;: &quot;4d243af99b1f26166cb76100&quot;,
      &quot;Subject&quot;: {
        &quot;_t&quot;: &quot;Variable&quot;,
        &quot;_id&quot;: &quot;4d243af99b1f26166cb760fa&quot;,
        &quot;Uri&quot;: &quot;V3-Class9&quot;
      },
      &quot;Predicate&quot;: {
        &quot;_t&quot;: &quot;Variable&quot;,
        &quot;_id&quot;: &quot;4d243af99b1f26166cb760fc&quot;,
        &quot;Uri&quot;: &quot;V3-Predicate10&quot;
      },
      &quot;Object&quot;: {
        &quot;_t&quot;: &quot;Variable&quot;,
        &quot;_id&quot;: &quot;4d243af99b1f26166cb760fe&quot;,
        &quot;Uri&quot;: &quot;V3-Something11&quot;
      }
    }
  ],
  &quot;Consequent&quot;: {
    &quot;_id&quot;: &quot;4d243af99b1f26166cb76101&quot;,
    &quot;Subject&quot;: {
      &quot;_t&quot;: &quot;Variable&quot;,
      &quot;_id&quot;: &quot;4d243af99b1f26166cb760f8&quot;,
      &quot;Uri&quot;: &quot;V3-Subclass8&quot;
    },
    &quot;Predicate&quot;: {
      &quot;_t&quot;: &quot;Variable&quot;,
      &quot;_id&quot;: &quot;4d243af99b1f26166cb760fc&quot;,
      &quot;Uri&quot;: &quot;V3-Predicate10&quot;
    },
    &quot;Object&quot;: {
      &quot;_t&quot;: &quot;Variable&quot;,
      &quot;_id&quot;: &quot;4d243af99b1f26166cb760fe&quot;,
      &quot;Uri&quot;: &quot;V3-Something11&quot;
    }
  }
}
</pre>
<p>That is essentially the whole semantic store.  I connected it up to a reasoner and have successfully run a few test cases against it.  Next time I get a chance to experiment with this technology I plan to try loading a larger ontology and will rework the reasoner so that it can work directly against the database instead of taking in-memory copies of most queries that it performs.</p>
<p>At this point this is JUST AN EXPERIMENT but hopefully someone will find this blog entry useful.  I hope later to connect this up to the home automation system so that it can begin reasoning across an ontology of the house and a set of ABox assertions about its current and past state.</p>
<p>Since I&#8217;m still relatively new to the semantic web I&#8217;d welcome feedback on this approach to storing ontologies in NOSQL databases from any experienced semanticists.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2011/01/a-semantic-web-ontology-triple-store-built-on-mongodb/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An ontology triple (quad) store for RDF/OWL using Entity Framework 4</title>
		<link>http://blog.abodit.com/2010/05/an-ontology-triple-quad-store-for-rdfowl-using-entity-framework-4/</link>
		<comments>http://blog.abodit.com/2010/05/an-ontology-triple-quad-store-for-rdfowl-using-entity-framework-4/#comments</comments>
		<pubDate>Wed, 12 May 2010 22:34:21 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NLP]]></category>
		<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Ontology]]></category>
		<category><![CDATA[OWL]]></category>
		<category><![CDATA[RDF]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=859</guid>
		<description><![CDATA[This weeks side-project was the creation of an ontology store using Entity Framework 4. An ontology store stores axioms consisting of Subject, Predicate, Object which are usually serialized as RDF, OWL, N3, &#8230; Whereas there&#8217;s lots of details about these serialization formats, the actual mechanics of how to store and manipulate them was somewhat harder <a href="http://blog.abodit.com/2010/05/an-ontology-triple-quad-store-for-rdfowl-using-entity-framework-4/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>This weeks side-project was the creation of an ontology store using Entity Framework 4.  An ontology store stores axioms consisting of Subject, Predicate, Object which are usually serialized as RDF, OWL, N3, &#8230;  Whereas there&#8217;s lots of details about these serialization formats, the actual mechanics of how to store and manipulate them was somewhat harder to come by.  Nevertheless, after much experimentation I came up with an Entity Model that can store Quads (Subject, Predicate, Object and Meta) or Quins (Subject, Predicate, Object, Meta, Graph).  The addition of Meta allows one Axiom to reference another.  The addition of Graph allows the store to be segmented making it easy to import some N3 or RDF into a graph, then flush that graph if it is no longer needed or if a newer version becomes available.</p>
<p>The store is currently hooked up to an Euler reasoner that can reason against it, lazily fetching just the necessary records from the SQL database that backs the Entity Model.</p>
<p>Here&#8217;s the EDMX showing how I modeled the Ontology Store:</p>
<p><a href="http://blog.abodit.com/wp-content/uploads/2010/05/OntologyEDMX.png"><img src="http://blog.abodit.com/wp-content/uploads/2010/05/OntologyEDMX.png" alt="Ontology Store Entity Model" title="Ontology Store Entity Model" width="841" height="528" class="aligncenter size-full wp-image-858" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2010/05/an-ontology-triple-quad-store-for-rdfowl-using-entity-framework-4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SQL Server &#8211; error: 18456, severity: 14, state: 38 &#8211; Incorrect Login</title>
		<link>http://blog.abodit.com/2009/11/sql-server-error-18456-severity-14-state-38-incorrect-login/</link>
		<comments>http://blog.abodit.com/2009/11/sql-server-error-18456-severity-14-state-38-incorrect-login/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 23:57:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=48</guid>
		<description><![CDATA[Despite the error message this problem can be caused by something other than an Authorization failure.  In fact, simply misspelling the Initial Catalog can cause this message to appear.  I wish developers wouldn&#8217;t reuse error messages when the problem and solution is completely different.]]></description>
			<content:encoded><![CDATA[<p>Despite the error message this problem can be caused by something other than an Authorization failure.  In fact, simply misspelling the Initial Catalog can cause this message to appear.  I wish developers wouldn&#8217;t reuse error messages when the problem and solution is completely different.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2009/11/sql-server-error-18456-severity-14-state-38-incorrect-login/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generate a SQL Compact Database from your Entity Model (EDMX)</title>
		<link>http://blog.abodit.com/2009/05/generate-a-sql-compact-database-from-your-entity-model-edmx/</link>
		<comments>http://blog.abodit.com/2009/05/generate-a-sql-compact-database-from-your-entity-model-edmx/#comments</comments>
		<pubDate>Mon, 18 May 2009 16:55:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=30</guid>
		<description><![CDATA[I recently switched from SQL Lite to SQL Compact for some of the databases in a system I&#8217;m building. If you are using SQL Compact make sure you have the Hotfix from Microsoft that fixes the Where() clause. The release that shipped as SP1 is utterly useless &#8211; can&#8217;t even do a where clause on <a href="http://blog.abodit.com/2009/05/generate-a-sql-compact-database-from-your-entity-model-edmx/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<div>I recently switched from SQL Lite to SQL Compact for some of the databases in a system I&#8217;m building.  If you are using SQL Compact make sure you have the <a href="http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=958478&amp;kbln=en-us">Hotfix from Microsoft</a> that fixes the Where() clause.  The release that shipped as SP1 is utterly useless &#8211; can&#8217;t even do a where clause on an nvarchar()!  I don&#8217;t know why they didn&#8217;t recall it and replace it with the hotfix version!</div>
<div>Unfortunately SQL Server Management Studio doesn&#8217;t support generating scripts from a SQL Compact database, so if you design your database there you are out of luck when it comes to creating the scripts to auto-generate it when you deploy your code.</div>
<div><a href="http://blogs.msdn.com/stevelasker/archive/2007/03/31/creating-your-sql-server-compact-edition-database-and-schema-in-code.aspx">Steve Lasker&#8217;s Blog</a> has a great article explaing how to embed these scripts as resources but it still doesn&#8217;t solve the issue of how to generate the script in the first place.</div>
<div>There are a few articles that explain how to use T4 to generate stored procedures from the EDMX but I didn&#8217;t see one to generate the actual database itself.  So I built one and you can get it below.</div>
<div>To use this simply create a text file in your project called generate.tt, paste this text in.  It will run and generate another file below itself containing the script to create each table it finds in each EDMX file in your project.</div>
<div><span class="Apple-style-span" style="font-weight: bold;">Limitations:</span> Within the EDMX file there isn&#8217;t enough information to fully create a database.  While the code below can generate foreign keys and primary keys from Associations and Keys defined in your model it has no idea what indexes you might want.  So for now it just generates an index for every field that looks vaguely indexable and it leaves it up to you to delete the ones you don&#8217;t want.</div>
<div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">//</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">// Generate SQL Server CE database tables from EDMX definition &#8211; suitable for some simple databases only</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">// Creates tables, adds primary keys, adds an index for every conceivable field, adds foreign key relationships for all associations</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">//</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ template language=&#8221;C#v3.5&#8243; debug=&#8221;True&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ output extension=&#8221;.sql&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ assembly name=&#8221;System.Core&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ assembly name=&#8221;System.Data&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ assembly name=&#8221;System.Data.Entity&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ assembly name=&#8221;System.Xml&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ assembly name=&#8221;System.Xml.Linq&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Collections&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Collections.Generic&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Data.EntityClient&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Data.SqlClient&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Diagnostics&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.IO&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Linq&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Text&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Text.RegularExpressions&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#@ import namespace=&#8221;System.Xml.Linq&#8221; #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">// This will generate the code to build SQL COMPACT tables for every EDMX in the current directory</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">// Simply plug that code into SQL Server Management Studio or use it as a resource to build your database</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">// Get the current directory from the stack trace</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">string stackTraceFileName = new StackTrace(true).GetFrame(0).GetFileName();</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">string directoryName = Path.GetDirectoryName(stackTraceFileName);</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211; </span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211;     This code was generated by a tool.</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211;     Changes to this file may cause incorrect behavior and will be lost if</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211;     the code is regenerated.</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211; </span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">string[] entityFrameworkFiles = Directory.GetFiles(directoryName, &#8220;*.edmx&#8221;);</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">foreach (string fileName in entityFrameworkFiles)</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&#8211; Creating TSQL creation script &lt;#= fileName #&gt;.</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">DoExtractTables(fileName);</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">}</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+ </span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">XNamespace edmxns = &#8220;http://schemas.microsoft.com/ado/2007/06/edmx&#8221;;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">XNamespace edmns = &#8220;http://schemas.microsoft.com/ado/2006/04/edm&#8221;;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">XNamespace ssdlns = &#8220;http://schemas.microsoft.com/ado/2006/04/edm/ssdl&#8221;;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">private void DoExtractTables(string edmxFilePath)</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XDocument edmxDoc = XDocument.Load(edmxFilePath);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement edmxElement = edmxDoc.Element(edmxns + &#8220;Edmx&#8221;);</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">//  element</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement runtimeElement = edmxElement.Element(edmxns + &#8220;Runtime&#8221;);</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">//  element</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement storageModelsElement = runtimeElement.Element(edmxns + &#8220;StorageModels&#8221;);</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">//  element</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement ssdlSchemaElement = storageModelsElement.Element(ssdlns + &#8220;Schema&#8221;);</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string entityContainerName = runtimeElement</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">.Element(edmxns + &#8220;ConceptualModels&#8221;)</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">//  element</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">.Element(edmns + &#8220;Schema&#8221;)</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">//  element</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">.Element(edmns + &#8220;EntityContainer&#8221;)</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">//  element</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">.Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string ssdlNamespace = ssdlSchemaElement.Attribute(&#8220;Namespace&#8221;).Value;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Get a list of tables from the SSDL.</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement entityContainerElement = ssdlSchemaElement.Element(ssdlns + &#8220;EntityContainer&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">IEnumerable entitySets = entityContainerElement.Elements(ssdlns + &#8220;EntitySet&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">IEnumerable entityTypes = ssdlSchemaElement.Elements(ssdlns + &#8220;EntityType&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">IEnumerable associations = ssdlSchemaElement.Elements(ssdlns + &#8220;Association&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string defaultSchema = entityContainerElement.Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">foreach (XElement table in entitySets)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string tableName = table.Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">DROP Table &lt;#= tableName #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">GO</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">CREATE TABLE &lt;#= tableName #&gt; (</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement entityType = entityTypes.First(et =&gt; et.Attribute(&#8220;Name&#8221;).Value == tableName);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">IEnumerable properties = entityType.Elements(ssdlns + &#8220;Property&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">int i = 0;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">int count = properties.Count();</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// GET THE PRIMARY KEY INFORMATION</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement key = entityType.Element(ssdlns + &#8220;Key&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string pkstr = &#8220;&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (key != null)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">pkstr = string.Join(&#8220;,&#8221;, key.Elements(ssdlns + &#8220;PropertyRef&#8221;).Select(pk =&gt; pk.Attribute(&#8220;Name&#8221;).Value).ToArray());</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">count++;</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// To add a comma on last field definition</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">//</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Accumulate a list of indexes to add as we go along examining the properties</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">//</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">List indexesToAdd = new List();</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">foreach (XElement property in properties)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">bool last = (i == count-1); </span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string propertyName = property.Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string propertyType = property.Attribute(&#8220;Type&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string nullField = &#8220;&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (property.Attribute(&#8220;Nullable&#8221;) != null &amp;&amp; property.Attribute(&#8220;Nullable&#8221;).Value == &#8220;false&#8221;) </span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">nullField = &#8221; NOT NULL&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string maxLength = &#8220;&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (property.Attribute(&#8220;MaxLength&#8221;) != null)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;"> maxLength = &#8220;(&#8221; + property.Attribute(&#8220;MaxLength&#8221;).Value + &#8220;)&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string comma = last ? &#8220;&#8221; : &#8220;,&#8221;;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">&lt;#= propertyName #&gt; &lt;#= propertyType #&gt;&lt;#= maxLength #&gt;&lt;#= nullField #&gt;&lt;#= comma #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">i++;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// each property</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Was their a primary key?  If so, add it</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (pkstr != &#8220;&#8221;)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">CONSTRAINT &lt;#=&#8221;PK_&#8221;+tableName #&gt; PRIMARY KEY ( &lt;#= pkstr #&gt; )</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">)</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">GO </span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Now add all the indexes we might need &#8230; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">foreach (XElement property in properties)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string comment = &#8220;&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string propertyName = property.Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (pkstr.Contains(propertyName)) </span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">comment = &#8220;already in primary key, no need to index again&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">int maxLength = -1;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (property.Attribute(&#8220;MaxLength&#8221;) != null)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;"> int.TryParse(property.Attribute(&#8220;MaxLength&#8221;).Value, out maxLength);</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (maxLength &gt; 256) </span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">comment = &#8220;too long to meaningfully index&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string commentPrefix = comment != &#8220;&#8221; ? &#8220;&#8211; &#8221; : &#8220;&#8221;;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// TODO: More rules here about what to index and whether it&#8217;s unique or not &#8230;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// </span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">CREATE [UNIQUE] INDEX &#8230;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">if (comment != &#8220;&#8221;)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">&lt;#=commentPrefix #&gt;&lt;#=comment #&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">&lt;#=commentPrefix #&gt;CREATE INDEX IX_&lt;#=tableName #&gt;_&lt;#=propertyName #&gt; ON &lt;#=tableName #&gt; (&lt;#=propertyName #&gt;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// each property</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Each table</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Now add all the associations between tables &#8230; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">foreach (XElement association in associations)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string associationName = association.Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement referentialConstraint = association.Elements(ssdlns + &#8220;ReferentialConstraint&#8221;).First();</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement principalRole = referentialConstraint.Element(ssdlns + &#8220;Principal&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string thisTable = principalRole.Attribute(&#8220;Role&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string thisField = principalRole.Element(ssdlns + &#8220;PropertyRef&#8221;).Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">XElement dependentRole = referentialConstraint.Element(ssdlns + &#8220;Dependent&#8221;);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string otherTable = dependentRole.Attribute(&#8220;Role&#8221;).Value;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">string otherField = dependentRole.Element(ssdlns + &#8220;PropertyRef&#8221;).Attribute(&#8220;Name&#8221;).Value;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;"><br />
</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">ALTER TABLE &lt;#= otherTable #&gt;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">ADD CONSTRAINT &lt;#= associationName #&gt;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">FOREIGN KEY (&lt;#= otherField #&gt;)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">REFERENCES &lt;#= thisTable #&gt; (&lt;#= thisField #&gt;)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">ON UPDATE CASCADE</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">ON DELETE CASCADE</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">GO</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;"> </span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">&lt;#+</span></div>
<div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">}</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Each association</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">}</span><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-size: x-small;"> </span></span><span class="Apple-style-span" style="font-size: x-small;">// Class</span></div>
<div><span class="Apple-style-span" style="font-size: x-small;">#&gt;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2009/05/generate-a-sql-compact-database-from-your-entity-model-edmx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

