<?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>Gary Pretty's Blog &#187; .net 4</title>
	<atom:link href="http://blog.garypretty.co.uk/index.php/tag/net-4/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.garypretty.co.uk</link>
	<description>.Net Development &#38; General Tech Related News</description>
	<lastBuildDate>Sat, 04 Feb 2012 00:32:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>TUTORIAL: Entity Framework v2.0 &#8211; Model First using Visual Studio 2010 and .Net 4.0</title>
		<link>http://blog.garypretty.co.uk/index.php/2009/05/20/tutorial-entity-framework-v20-model-first-using-visual-studio-2010-and-net-40/</link>
		<comments>http://blog.garypretty.co.uk/index.php/2009/05/20/tutorial-entity-framework-v20-model-first-using-visual-studio-2010-and-net-40/#comments</comments>
		<pubDate>Wed, 20 May 2009 14:36:56 +0000</pubDate>
		<dc:creator>Gary Pretty</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[.net 4]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[visual studio 2010]]></category>

		<guid isPermaLink="false">http://blog.garypretty.co.uk/index.php/2009/05/20/tutorial-entity-framework-v20-model-first-using-visual-studio-2010-and-net-40/</guid>
		<description><![CDATA[Following up on my recent post about the improvements to the Entity Framework in .Net 4.0 this is the first in a series of posts examining some of the new features and showing you how to put some of them into practice. In this post I am going to look at one of the most &#8230; </p><p><a class="more-link block-button" href="http://blog.garypretty.co.uk/index.php/2009/05/20/tutorial-entity-framework-v20-model-first-using-visual-studio-2010-and-net-40/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Following up on my recent post about the improvements to the Entity Framework in .Net 4.0 this is the first in a series of posts examining some of the new features and showing you how to put some of them into practice.</p>
<p>In this post I am going to look at one of the most anticipated features, Model First.  In the first version of the Entity Framework, you took a pre-existing database and the tools would generate your entity model for you.  In v2.0, you now have the option of creating your entity model first and then generating your database DDL from your finished model.</p>
<p>To follow this tutorial you will need Visual Studio 2010 Beta 1, .Net Framework 4.0 Beta 1 installed, SQL 2005 or 2008 / SQL Express.</p>
<p><strong><span style="color: #ff8000;">Please bear with me and let me know if there are any parts of this tutorial that are not completely clear.  I am new to writing these! So any comments welcome….</span></strong></p>
<p><strong>Creating your project and adding your blank entity model</strong></p>
<p>Open Visual Studio 2010 and create a new Dynamic Data Entities Web Application project(you could another project type to host your entity model, such as an ASP.Net Web Application).</p>
<p>Once your project has been created add a new <strong>ADO.Net Entity Data Model </strong>to the project. </p>
<p><a href="http://blog.garypretty.co.uk/wp-content/add-model-object.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="add_model_object" src="http://blog.garypretty.co.uk/wp-content/add-model-object-thumb.gif" border="0" alt="add_model_object" width="523" height="364" /></a></p>
<p>You will then be asked about what the model should contain.  As with version 1 of the Entity Framework, you have two options, <em>Generate from Database </em>and <em>Empty Model</em>.  To a large extent, the latter was redundant in the first version of the framework, but with version 2 it becomes very useful.  Select <strong><em>Empty Model</em></strong> and click Finish.<span id="more-121"></span></p>
<p><a href="http://blog.garypretty.co.uk/wp-content/choose-empty-model.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="choose_empty_model" src="http://blog.garypretty.co.uk/wp-content/choose-empty-model-thumb.gif" border="0" alt="choose_empty_model" width="526" height="467" /></a></p>
<p><strong></strong></p>
<p><strong><br />
Adding an entity and scalar properties to your model</strong></p>
<p>You will now be presented with your empty model designer ready for you to add your entities.  Right click on the designer and select <strong>Add –&gt; Entity…  </strong>For this example we will name our entity “Customer” and the entity set “Customers”.  This will create your Customer entity in the designer.</p>
<p>You will now need to add some properties to the entity.  You can do this by right clicking the entity and <strong>Add –&gt; Scalar Property </strong>and then selecting the data type of the property. Do this and create two string scalar properties for Name and Email.</p>
<p><strong></strong></p>
<p><strong><br />
Adding complex types</strong></p>
<p>You should now have a Customer entity with properties for the customer’s name and email address. A complex type is essentially a property that can contain sub properties.  To display this we are going to create one for the customer’s mailing address.</p>
<p>In your model browser, right click on the “Complex Types” folder and select <strong>Create Complex Type</strong>.  Rename this created type “Address”.  Now right click on the <strong>Address </strong>object you have created and select <strong>Add –&gt; Scalar Property –&gt; String</strong>.  Rename the created property “Line 1”.  Repeat this, adding properties for Line 2, Town, County, Post Code (you could use City, State, Zip if you are in the U.S. for example).</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/entity-created-add-scalar.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="entity_created_add_scalar" src="http://blog.garypretty.co.uk/wp-content/entity-created-add-scalar-thumb.gif" border="0" alt="entity_created_add_scalar" width="531" height="246" /></a></p>
<p>Once you have created your complex type, you can add this to your Customer entity by right clicking the entity and selecting <strong>Add –&gt; Complex Property</strong>. If you name your property Address, then the property type should automatically be set to <em>Address</em>, which is what we have just created.  You can check this and alter the type via the properties of the complex property (that’s a lot of properties <img src='http://blog.garypretty.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
<p> </p>
<p><strong>Create some additional entities</strong></p>
<p>Once I had created my Customer entity, I then went through the same steps to create two additional entities for <strong>Order</strong> and <strong>Product</strong>. If you do this, then you should end up with something like below.</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/finished-entities.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="finished_entities" src="http://blog.garypretty.co.uk/wp-content/finished-entities-thumb.gif" border="0" alt="finished_entities" width="531" height="419" /></a></p>
<p><strong></strong></p>
<p><strong><br />
Adding entity associations</strong></p>
<p>Now that we have our entities for Customer, Order and Product, we need to tell the model how the entities are associated.  For example, a customer will have related orders and an order will have related products.</p>
<p>To add our first association, right click the Customer entity and select <strong>Add –&gt; Association</strong>. You will then be presented with the dialog shown below to actually define the association.  As you can see, the primary entity for this association is Customer which is related to Order with a multiplicity of one-to-many.  Here is where we also set the navigation properties which will allow us to access related entities.  So for example, the Customer entity will have a navigation property of “Orders”.  There is also a plain English description of the association you are about to create to ensure that you are creating it correctly.</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/create-association.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="create_association" src="http://blog.garypretty.co.uk/wp-content/create-association-thumb.gif" border="0" alt="create_association" width="530" height="508" /></a>  </p>
<p>Once you are happy click ok and the association will be shown on your model connecting the Customer and Order entities.</p>
<p>We now need to also create an association between the Order and Product entities using the same steps as above, but this time the properties of the association are slightly different as the multiplicity is many-to-many as show below.</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/manytomanyassociation.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="many-to-many-association" src="http://blog.garypretty.co.uk/wp-content/manytomanyassociation-thumb.gif" border="0" alt="many-to-many-association" width="534" height="512" /></a></p>
<p><strong></strong></p>
<p><strong><br />
Generating the database</strong></p>
<p>Ok, so we have our model containing our entities.  Our entities have their properties and their associations, but we still do not have a database for our model to map to.  This is the bit I really like.  Right click on the model designer area and click “<strong>Generate Database Script From Model…</strong>”.</p>
<p>This will then open the standard database connection dialog we are all used to in Visual Studio. Add a connection, select your server and enter a name for your database (use a name that does not already exist, I called mine “ModelFirstTestDb”…..original I know!), then click OK.  You should then be prompted to create the database as seen below, so click Yes to create the database.</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/confirm-database-create.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="confirm_database_create" src="http://blog.garypretty.co.uk/wp-content/confirm-database-create-thumb.gif" border="0" alt="confirm_database_create" width="532" height="543" /></a></p>
<p>You should now see your new connection selected and the option to store the credentials in the web.config should be checked. Click next and the database schema scripts (DDL) will be generated for you and you will be presented with the generated script as below.  Take a minute to have a look through the DDL and see how it has created the tables and fields.  In the image below you can see the fields that have been generated to map to the complex type for the Customer address we created earlier.</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/generated-ddl.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="generated_ddl" src="http://blog.garypretty.co.uk/wp-content/generated-ddl-thumb.gif" border="0" alt="generated_ddl" width="535" height="517" /></a></p>
<p>Now we just need to click Finish and we will be warned that our existing mappings and store schema will be overwritten, click Yes to continue.</p>
<p><a href="http://blog.garypretty.co.uk/wp-content/ssdl-warning.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="ssdl_warning" src="http://blog.garypretty.co.uk/wp-content/ssdl-warning-thumb.gif" border="0" alt="ssdl_warning" width="434" height="129" /></a></p>
<p><strong></strong></p>
<p><strong><br />
Execute your DDL</strong></p>
<p>Now you need to take the generated script and execute it against your database.  The database itself will have been created, but in this beta version you still need to execute the scripts manually.  The ADO.Net team have indicated that this will not be the case in the final version.</p>
<p><strong></strong></p>
<p><strong><br />
Running your Dynamic Data Entities application</strong></p>
<p>If you now open the Global.asax.cs file in your project and add the following line in the RegisterRoutes method, you should be able to run your application and see your new model and database in action;</p>
<p><span style="font-size: xx-small;"><strong>DefaultModel.RegisterContext(typeof(Model1Container),new ContextConfiguration() { ScaffoldAllTables = true });</strong></span></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/.net+4' rel='tag' target='_self'>.net 4</a>, <a class='technorati-link' href='http://technorati.com/tag/ADO.NET' rel='tag' target='_self'>ADO.NET</a>, <a class='technorati-link' href='http://technorati.com/tag/Entity+Framework' rel='tag' target='_self'>Entity Framework</a>, <a class='technorati-link' href='http://technorati.com/tag/tutorial' rel='tag' target='_self'>tutorial</a>, <a class='technorati-link' href='http://technorati.com/tag/visual+studio+2010' rel='tag' target='_self'>visual studio 2010</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://blog.garypretty.co.uk/index.php/2009/05/20/tutorial-entity-framework-v20-model-first-using-visual-studio-2010-and-net-40/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

