<?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>Entity Framework Design : Designer</title><link>http://blogs.msdn.com/efdesign/archive/tags/Designer/default.aspx</link><description>Tags: Designer</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Foreign Keys in the Entity Framework</title><link>http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx</link><pubDate>Mon, 16 Mar 2009 23:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9481955</guid><dc:creator>efdesign</dc:creator><slash:comments>35</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9481955.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9481955</wfw:commentRss><description>&lt;H3&gt;Background&lt;/H3&gt;
&lt;P&gt;A number of months ago &lt;A href="http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx"&gt;we asked&lt;/A&gt; whether Foreign Keys (FKs) in Conceptual and Object models were important.&lt;/P&gt;
&lt;P&gt;The feedback we got indicated that there are a real mix of opinions on the topic. Some people are all for them while others think that FKs pollute the conceptual model. &lt;/P&gt;
&lt;P&gt;The fact that our customers were so divided meant we thought it was important to provide options here. Those who want FKs in their Entities should be able to do so, so they can gain all the benefits that having FKs in your Entities undoubtedly provide. On the other hand customers who are concerned that have FKs in their Entities in someway pollutes their model can continue to use the type of associations we had in .NET 3.5 SP1.&lt;/P&gt;
&lt;P&gt;In the past we've called the .NET 3.5 SP1 style Associations "first class" associations. Which while accurate somehow implies that associations based on FKs in Entities are "second class". In fact when we talked about this new work recently with our MVPs one of the first questions was "are these second class?", which they most definitely are not. &lt;/P&gt;
&lt;P&gt;The fact is these new associations are different but first class nevertheless.&lt;/P&gt;
&lt;H3&gt;So what's been added?&lt;/H3&gt;
&lt;P&gt;In .NET 4.0 we will add support for a new type of association called "FK Associations". &lt;/P&gt;
&lt;P&gt;The way this works is we will allow you to include your FK columns in your entities as "FK properties", and once you have FK Properties in your entity you can create an "FK Association" that is dependent upon those properties. &lt;/P&gt;
&lt;P&gt;Okay so we will have "FK Associations", what are we going to call the older style associations? Obviously saying something like ".NET 3.5 SP1 style Associations" every time isn't ideal. So we are going to call them "Independent Associations".&lt;/P&gt;
&lt;P&gt;The term "Independent Association" resonates for us because they are independently mapped, whereas FK Associations need no mapping, simply mapping the Entity(Set) is sufficient.&lt;/P&gt;
&lt;H3&gt;How do I use an "FK Association"?&lt;/H3&gt;
&lt;P&gt;The real reason so many customers and partners are asking for "FK Associations" is that they significantly simplify some key coding patterns. So much so that we are convinced that for most people "FK Associations" will be an automatic choice going forward.&lt;/P&gt;
&lt;P&gt;Some of the things that are hard with "Independent Associations" are trivially easy using FK Associations. Scenarios as diverse as DataBinding, Dynamic Data, Concurrency Control,&amp;nbsp; ASP.NET MVC Binders, N-Tier etc are all positively impacted.&lt;/P&gt;
&lt;P&gt;Let's have a look at some code snippets that will work with "FK Associations":&lt;/P&gt;
&lt;P&gt;1) Create a new Product and FK Association to an existing Category by setting the FK Property directly:&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;using&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; (&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; context = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Context&lt;/SPAN&gt;()) &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;//Create a product and a relationship to a known category by ID &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Product&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Product &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;ID = 1, &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Name = &lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Bovril"&lt;/SPAN&gt;, &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;CategoryID = 13 &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;};&amp;nbsp; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;//Add the product (and create the relationship by FK value) &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;context.Products.AddObject(p); &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;context.SaveChanges(); &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;This sort of approach works both for insert and update, and is particular useful for things like databinding, where you often have the new Value of the FK in a grid or something but you don't have the corresponding object, and you don't want to wear the cost of a query to pull back the principal object (i.e. the Category).&lt;/P&gt;
&lt;P&gt;2) Create a new Product and a new FK Association to an existing Category by setting the reference instead:&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Create_new_Product_in_existing_Category_by_reference() &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; context = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Context&lt;/SPAN&gt;()) &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;//Create a new product and relate to an existing category &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Product&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Product &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;ID = 1, &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Name = &lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Bovril"&lt;/SPAN&gt;, &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Category = context.Categories &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Single(c =&amp;gt; c.Name == &lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Food"&lt;/SPAN&gt;) &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Note: no need to add the product, because relating &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="COLOR: green"&gt;&amp;nbsp; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// to an existing category does that automatically. &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Also notice the use of the Single() query operator&amp;nbsp; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// this is new to EF in .NET 4.0 too. &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;context.SaveChanges(); &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;} &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;} &lt;BR&gt;&lt;BR&gt;This programming pattern is not new to the Entity Framework, you could do this in .NET 3.5 SP1. I called it out because it shows you can still write the sort of code you write with Independent Associations even when using FK Associations.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;3) Update an existing Product without informing the Entity Framework about the original value of the CategoryID (not supported with Independent Associations):&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Edit(&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Product&lt;/SPAN&gt; editedProduct) &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; context = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Context&lt;/SPAN&gt;()) &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Create a stand-in for the original entity &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// by just using the ID. Of the editedProduct &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Note: you don't have to provide an existing Category or CategoryID &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;context.Products.Attach( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Product&lt;/SPAN&gt; { ID = editedProduct.ID }); &lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Now update with new values including CategoryID &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;context.Products.ApplyCurrentValues(editedProduct); &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;context.SaveChanges(); &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;} &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;In this example "editedProduct" is a product which has been edited somewhere, this is exactly the sort of the code you might write in the Edit method of a ASP.NET MVC controller, and is a great improvement over the code you have to write using Independent Associations.&lt;/P&gt;
&lt;P&gt;There is much more you can do with FK Associations, but these 3 samples give you a flavor of the sort of coding patterns FK Associations allow. &lt;/P&gt;
&lt;H4&gt;Keeping FKs and References in Sync&lt;/H4&gt;
&lt;P&gt;One thing that hasn't been mentioned so far is that the Entity Framework tries to keep related References and FKs in sync as much as possible. &lt;/P&gt;
&lt;P&gt;When this synchronization occurs depends upon when the Entity Framework is notified of changes, which depends upon the type of Entity Classes involved: be they POCO with Proxies, POCO without Proxies, IPOCO or EntityObjects etc. &lt;/P&gt;
&lt;P&gt;Another post will cover this in more detail.&lt;/P&gt;
&lt;H3&gt;How do I create an "FK Association"?&lt;/H3&gt;
&lt;P&gt;There are a number of ways. &lt;/P&gt;
&lt;P&gt;One mainline scenario is when using the tools to infer a model from a database. We have added an option to choose whether "FK Associations" or "Independent Associations" are generated by default. The same is true if you use EdmGen.exe (i.e. our command line tool).&lt;/P&gt;
&lt;P&gt;The second mainline scenario for creating FK Associations is creating a model from scratch, aka &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;Model First&lt;/A&gt; which is new to .NET 4.0.&lt;/P&gt;
&lt;P&gt;Here are the steps involved:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create two Entities (say Product and Category) that look something like this: &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep1.jpg" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep1.jpg"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border=0 alt=ProductCategoryStep1 src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep1_thumb.jpg" width=455 height=158 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep1_thumb.jpg"&gt;&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;Add a property that will be the FK Property: i.e. CategoryID &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep2.jpg" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep2.jpg"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border=0 alt=ProductCategoryStep2 src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep2_thumb.jpg" width=495 height=227 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep2_thumb.jpg"&gt;&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;Create an association between the two EntityTypes with the correct end multiplicities and NavigationProperty names: &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep3.jpg" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep3.jpg"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border=0 alt=ProductCategoryStep3 src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep3_thumb.jpg" width=427 height=412 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep3_thumb.jpg"&gt;&lt;/A&gt; &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;Double Click the line between the two Entities, that represents the Association, and add a Referential Integrity Constraint: &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep4.jpg" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep4.jpg"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border=0 alt=ProductCategoryStep4 src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep4_thumb.jpg" width=374 height=297 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep4_thumb.jpg"&gt;&lt;/A&gt; &lt;BR&gt;This is the step that tells the Entity Framework that the CategoryID is an "FK Property" and that the "CategoryProduct" association is an "FK Association". &lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;You end up with something that looks like this: &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep5.jpg" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep5.jpg"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border=0 alt=ProductCategoryStep5 src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep5_thumb.jpg" width=476 height=235 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/ForeignKeysintheEntityFramework_D970/ProductCategoryStep5_thumb.jpg"&gt;&lt;/A&gt;&amp;nbsp; &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;And you are done you've set up an FK Association. Notice that this association doesn't need to be mapped you simply need to make sure you map all the properties of both Entities.&lt;/P&gt;
&lt;P&gt;Very easy.&lt;/P&gt;
&lt;H3&gt;Summary&lt;/H3&gt;
&lt;P&gt;In .NET 4.0 we will add support for FK Properties and FK Associations to the Entity Framework. The existing style Independent Associations will still be possible, but we expect that FK Associations will become most peoples automatic choice because they simplify so many common Entity Framework coding tasks.&lt;/P&gt;
&lt;P&gt;As always we'd love to hear your thoughts on this work.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;A href="http://blogs.msdn.com/alexj" mce_href="http://blogs.msdn.com/alexj"&gt;Alex James&lt;/A&gt;, &lt;BR&gt;&lt;/B&gt;Program Manager, Entity Framework Team, Microsoft.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;I&gt;This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at &lt;/I&gt;&lt;/B&gt;&lt;A href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx"&gt;&lt;B&gt;&lt;I&gt;this post&lt;/I&gt;&lt;/B&gt;&lt;/A&gt;&lt;B&gt;&lt;I&gt;. &lt;/I&gt;&lt;/B&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9481955" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/EDM/default.aspx">EDM</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/N-Tier/default.aspx">N-Tier</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Designer/default.aspx">Designer</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/FKs/default.aspx">FKs</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+4/default.aspx">Entity Framework 4</category></item><item><title>Customizing Entity Classes in VS 2010</title><link>http://blogs.msdn.com/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx</link><pubDate>Thu, 22 Jan 2009 22:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9370115</guid><dc:creator>efdesign</dc:creator><slash:comments>23</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9370115.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9370115</wfw:commentRss><description>&lt;P&gt;When VS 2010 ships it will include some significant improvements to our code generation story for the Entity Framework. The basic idea is to make use of T4 templates for code generation and ship strong integration into the Entity Framework Designer to make the experience of customizing those templates as seamless as possible. &lt;BR&gt;&lt;BR&gt;Below Sanjay from our Tools team outlines what will be possible once VS 2010 is released.&lt;/P&gt;
&lt;H3&gt;Customize the code generated by the Entity Designer with T4 templates&lt;/H3&gt;
&lt;P&gt;In Visual Studio 2008 SP1, the ADO.NET Entity Designer generates classes from the CSDL portion of the EDMX file using the &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.entity.design.entityclassgenerator.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.entity.design.entityclassgenerator.aspx"&gt;EntityClassGenerator&lt;/A&gt; APIs. Numerous customers have asked us how to customize the code generation for a variety of scenarios including:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Make the generated ObjectContext internal &lt;/LI&gt;
&lt;LI&gt;Make the generated ObjectContext and Entity classes implement a user-defined interface &lt;/LI&gt;
&lt;LI&gt;Add user-defined CLR attributes to generated ObjectContext and generated Entity classes &lt;/LI&gt;
&lt;LI&gt;Influence generated classes based on structural annotations in CSDL &lt;/LI&gt;
&lt;LI&gt;Generate the ObjectContext and Entity classes into separate files &lt;/LI&gt;
&lt;LI&gt;Generate “proxy classes” for the generated classes &lt;/LI&gt;
&lt;LI&gt;Partially or fully change how classes are generated, maybe even generate additional (non code) artifacts in the project &lt;/LI&gt;
&lt;LI&gt;Completely replace entity framework code generation with custom code &lt;/LI&gt;
&lt;LI&gt;Generate POCO classes from the model to use as a starting point in my applications &lt;/LI&gt;
&lt;LI&gt;Generate self-tracking entity classes &lt;/LI&gt;
&lt;LI&gt;· ... &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;While the SingleFileGenerator techniques described &lt;A href="http://blogs.msdn.com/adonet/archive/2008/01/24/customizing-code-generation-in-the-ado-net-entity-designer.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2008/01/24/customizing-code-generation-in-the-ado-net-entity-designer.aspx"&gt;here&lt;/A&gt; enable some of these scenarios these techniques are generally difficult to implement and install or are tricky to debug and customize.&lt;/P&gt;
&lt;P&gt;In Visual Studio 2010, we plan to give customers the ability to use &lt;A href="http://msdn.microsoft.com/en-us/library/bb126445.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb126445.aspx"&gt;T4 templates&lt;/A&gt; to generate their classes, and are also planning deep integration with the Entity Designer and Visual Studio to provide a great end-to-end experience.&lt;/P&gt;
&lt;H3&gt;Walkthrough&lt;/H3&gt;
&lt;P&gt;The example below customizes code generation to make the generated classes implement a user-defined interface (e.g. &lt;EM&gt;IValidate&lt;/EM&gt;).&lt;/P&gt;
&lt;H4&gt;Preconditions:&lt;/H4&gt;
&lt;UL&gt;
&lt;LI&gt;User has installed Visual Studio 2010 &lt;/LI&gt;
&lt;LI&gt;User is familiar with customizing T4 templates &lt;/LI&gt;
&lt;LI&gt;User has a C# or a VB console project that targets FX4.0 with an EDMX file in the project &lt;/LI&gt;&lt;/UL&gt;
&lt;H4&gt;Walkthrough:&lt;/H4&gt;
&lt;OL&gt;
&lt;LI&gt;User: opens EDMX file in the Entity Designer. Note that the generated classes are in a child file of the EDMX file (i.e. in Northwind.Designer.cs) &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_4.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=369 alt=image src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_1.png" width=241 border=0 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_1.png"&gt;&lt;/A&gt; &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;User: right-clicks on an empty area of the designer surface and chooses “&lt;EM&gt;Add New Artifact Generation Item…&lt;/EM&gt;” from the context menu. &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_6.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=328 alt=image src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_2.png" width=250 border=0 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_2.png"&gt;&lt;/A&gt; &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;Visual Studio: displays the standard “&lt;EM&gt;Add New Item&lt;/EM&gt;” dialog and shows a filtered list of items for the user to select from. As expected, the “&lt;EM&gt;Add New Item&lt;/EM&gt;” dialog only shows a list of item templates specific to the current project target framework version, project type and language. &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_8.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=377 alt=image src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_3.png" width=632 border=0 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_3.png"&gt;&lt;/A&gt; &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;User: selects “&lt;EM&gt;ADO.NET EntityObject Generator&lt;/EM&gt;”, specifies a file name and clicks “&lt;EM&gt;Add"&lt;/EM&gt; &lt;/LI&gt;
&lt;LI&gt;Visual Studio: checks out the project from Source Control if necessary &lt;/LI&gt;
&lt;LI&gt;Visual Studio: sets the “&lt;EM&gt;Custom Tool&lt;/EM&gt;” property of the EDMX file to empty, which will cause the existing .designer.cs file to be deleted &lt;/LI&gt;
&lt;LI&gt;Visual Studio: the VS item template adds a new .tt file to the project in the same project directory as the EDMX file &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_10.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=265 alt=image src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_4.png" width=325 border=0 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_4.png"&gt;&lt;/A&gt; &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;Visual Studio: configures the new .tt file to process the selected EDMX file. The EDMX file to process is a replaceable parameter in the .tt file, and VS updates it to point to the selected EDMX file. Thus, the .tt file now knows which EDMX file to generate code for. &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_12.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=186 alt=image src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_5.png" width=244 border=0 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_5.png"&gt;&lt;/A&gt; &lt;BR&gt;[Screenshot of .tt file open in the &lt;A href="http://www.t4editor.net/" mce_href="http://www.t4editor.net/"&gt;Clarius T4 Editor Community edition for VS 2008&lt;/A&gt;] &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;User: double-clicks the .tt file in the project to open it in VS10 &lt;/LI&gt;
&lt;LI&gt;User: edits the .tt file in Visual Studio to make every Entity object implement the &lt;EM&gt;IValidate&lt;/EM&gt; interface &lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_14.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=148 alt=image src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_6.png" width=244 border=0 mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/CustomizingEntityClasseswithT4_97EE/image_thumb_6.png"&gt;&lt;/A&gt; &lt;BR&gt;[Screenshot of .tt file open in the &lt;A href="http://www.t4editor.net/" mce_href="http://www.t4editor.net/"&gt;Clarius T4 Editor Community edition for VS 2008&lt;/A&gt;] &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;User: saves and closes .tt file &lt;/LI&gt;
&lt;LI&gt;Visual Studio: Transforms the .tt file to produce the generated classes as a child file of the .tt file &lt;/LI&gt;
&lt;LI&gt;User: notes that the generated classes (under the .tt file) implement the IValidate interface as expected &lt;/LI&gt;
&lt;LI&gt;User: switches to the EDMX file in the designer and makes some changes (e.g. add a new entity) &lt;/LI&gt;
&lt;LI&gt;User: saves EDMX file &lt;/LI&gt;
&lt;LI&gt;Visual Studio: Transforms all .tt files in the project that reference the EDMX file. As before, the generated classes are child files of the .tt files &lt;/LI&gt;&lt;/OL&gt;
&lt;H3&gt;Design&lt;/H3&gt;
&lt;P&gt;The design affects multiple parts of the Entity Framework and the Entity Designer and the key changes are described below:&lt;/P&gt;
&lt;H4&gt;Entity Framework Runtime&lt;/H4&gt;
&lt;OL&gt;
&lt;LI&gt;We started by creating C# and VB T4 templates to generate classes from CSDL and EDMX files &lt;/LI&gt;
&lt;LI&gt;Since the T4 engine is installed with Visual Studio and not with the.NET FX runtime, we preprocess the templates as described &lt;A href="https://blogs.msdn.com/garethj/archive/2008/11/11/dsl-2010-feature-dives-t4-preprocessing-rationale.aspx" mce_href="https://blogs.msdn.com/garethj/archive/2008/11/11/dsl-2010-feature-dives-t4-preprocessing-rationale.aspx"&gt;here&lt;/A&gt; and include the compiled code into System.Data.Entity.Design.dll &lt;/LI&gt;
&lt;LI&gt;We added a new class &lt;EM&gt;System.Data.Entity.Design.EntityCodeGenerator&lt;/EM&gt; (in System.Data.Entity.Design.dll ) that generates code using the preprocessed T4 template &lt;/LI&gt;
&lt;LI&gt;Finally, we enhanced a few runtime metadata APIs and added extension methods to load and iterate over models a lot easier. Our T4 templates make heavy use of these new methods. &lt;/LI&gt;&lt;/OL&gt;
&lt;H4&gt;Entity Designer&lt;/H4&gt;
&lt;P&gt;We took the C# and VB T4 templates used by the runtime and changed them to be more “VS friendly” as described below.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;We made the EDMX file name referenced in the T4 templates a replaceable parameter &lt;/LI&gt;
&lt;LI&gt;We created a wizard (with no GUI) that is launched by Visual Studio when the item is added to the project. The wizard does 2 things: 
&lt;OL&gt;
&lt;LI&gt;updates the EDMX file name referenced in the T4 templates and &lt;/LI&gt;
&lt;LI&gt;sets the “&lt;EM&gt;Custom Tool&lt;/EM&gt;” property of the EDMX file to empty &lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;
&lt;LI&gt;We rolled up the T4 templates (and wizard) into standard C# and VB Visual Studio item template which are installed with VS 2010; this makes them show up in the “&lt;EM&gt;Add New Item&lt;/EM&gt;” dialog in Visual Studio &lt;/LI&gt;
&lt;LI&gt;We added a new context menu to the designer surface to launch the “&lt;EM&gt;Add New Item&lt;/EM&gt;” dialog and also added code to only show VS item templates whose names start with the prefix &lt;EM&gt;ADONETArtifactGenerator_&lt;/EM&gt; &lt;/LI&gt;
&lt;LI&gt;Our design ensures that user installed VS item templates in &lt;EM&gt;%MyDocuments%&lt;/EM&gt; as well as the VS item templates we ship in box show up in the “&lt;EM&gt;Add…New…Item&lt;/EM&gt;” dialog (per the template name prefix rule above) &lt;/LI&gt;
&lt;LI&gt;We added code to the designer that finds all .tt files related to an EDMX file and automatically transform them when the EDMX file is saved. &lt;/LI&gt;
&lt;LI&gt;Finally, we added a new property called “&lt;EM&gt;Process related T4 templates on Save&lt;/EM&gt;” on the designer surface to let users control whether or not to transform .tt files related to the EDMX file on save. &lt;/LI&gt;&lt;/OL&gt;
&lt;H4&gt;Multi Targeting considerations&lt;/H4&gt;
&lt;P&gt;As many of you are aware, VS 2010 allows developers to target FX4.0 as well as FX3.5 (and older runtimes). The T4 templates that we ship in the Visual Studio box generate code that works with the Entity Framework in .NET FX 4.0. This means the VS item templates we ship in the box will not be available for projects that target FX3.5.&lt;/P&gt;
&lt;P&gt;However, it is certainly possible (and supported) for users to create new T4 templates (and new VS item templates) that generate code from an EDMX file in projects that target FX3.5 and light up in the same experience. In fact, we might release some new templates ourselves later, on CodeGallery perhaps.&lt;/P&gt;
&lt;H4&gt;How can 3&lt;SUP&gt;rd&lt;/SUP&gt; parties plug in?&lt;/H4&gt;
&lt;P&gt;Since our design is based upon VS item templates that wrap T4 templates, we have opened the door to let 3&lt;SUP&gt;rd&lt;/SUP&gt; party code generators participate in the end-to-end experience. Our contract is straight forward and easy to implement:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a (C# or VB) T4 template to generate code as you desire. The T4 templates we ship in the box can be invaluable points of reference as you roll your own. &lt;/LI&gt;
&lt;LI&gt;Make the EDMX reference in your .tt file a replaceable parameter by calling it &lt;EM&gt;$edmxInputFile$&lt;/EM&gt; like we do in our .tt file. &lt;/LI&gt;
&lt;LI&gt;Wrap the .tt file in a VS item template. This is pretty easy and well documented on &lt;A href="http://msdn.microsoft.com/en-us/library/ms247069(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms247069(VS.80).aspx"&gt;MSDN&lt;/A&gt;. You can also use the &lt;A href="http://msdn.microsoft.com/en-us/library/ms185318(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms185318(VS.80).aspx"&gt;Export Template wizard&lt;/A&gt; included Visual Studio. Also specify additional VS item template settings such as target framework, project type, etc &lt;/LI&gt;
&lt;LI&gt;Prefix the name of your .vstemplate file with &lt;EM&gt;ADONETArtifactGenerator_&lt;/EM&gt; if you want your VS item template to show up in the “&lt;EM&gt;Add New Item&lt;/EM&gt;” dialog when it is launched from the context menu in the Entity Designer. &lt;/LI&gt;
&lt;LI&gt;You can use the wizard we ship in Visual Studio to do parameter replacement in your VS item template by adding the following to your .vstemplate file: &lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;WizardExtension&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Assembly&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Microsoft.Data.Entity.Design, Version=10.0.0.0, Culture=neutral,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;PublicKeyToken=b03f5f7f11d50a3a &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Assembly&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;FullClassName&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.AddArtifactGeneratorWizard&amp;nbsp; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;FullClassName&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;WizardExtension&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;BR&gt;Of course, you can also create your own wizard that does custom processing when the item is added to the project as described &lt;A href="http://msdn.microsoft.com/en-us/library/ms185301(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms185301(VS.80).aspx"&gt;here&lt;/A&gt;. &lt;/LI&gt;
&lt;LI&gt;Create a &lt;A href="http://msdn.microsoft.com/en-us/library/aa992029(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa992029(VS.80).aspx"&gt;VSCONTENT&lt;/A&gt; file and zip up your custom item template &amp;amp; related files into a &lt;A href="http://msdn.microsoft.com/en-us/library/ms246580(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms246580(VS.80).aspx"&gt;Visual Studio Content Installer (.vsi)&lt;/A&gt; file &lt;/LI&gt;
&lt;LI&gt;Upload the .vsi file someplace your customers can download it from &lt;/LI&gt;
&lt;LI&gt;Visual Studio already knows how to install .vsi files and everything you need is already available in Visual Studio. &lt;/LI&gt;&lt;/OL&gt;
&lt;H3&gt;A few key takeaways:&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;This design lets the EF team release new T4 templates outside the usual Visual Studio and .NET FX ship cycles and the experience to consume them is exactly the same.&lt;/LI&gt;
&lt;LI&gt;There are several excellent resources on the web that describe advanced T4 template scenarios and our design enables them. In fact, we expect users to leverage these resources while creating new T4 templates or customizing the ones we ship. For example, code in a custom T4 template can use the &lt;A href="http://msdn.microsoft.com/en-us/library/envdte(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/envdte(VS.80).aspx"&gt;EnvDTE APIs&lt;/A&gt; to access the project system and generate outputs to multiple files.&lt;/LI&gt;
&lt;LI&gt;Over time, we expect to blog about some of these advanced scenarios and also work with MVPs, industry experts and Patterns &amp;amp; Practices to provide guidance to customers in a manner similar to the &lt;A href="http://msdn.microsoft.com/en-us/vsts2008/aa718948.aspx" mce_href="http://msdn.microsoft.com/en-us/vsts2008/aa718948.aspx"&gt;Guidance Automation Toolkit&lt;/A&gt;.&lt;/LI&gt;
&lt;LI&gt;Custom T4 templates can generate anything you want, not just code. For example, we have an internal prototype of a T4 template that iterates over a model and generates an HTML file with a nicely formatted report of the entities and associations in the model&lt;/LI&gt;
&lt;LI&gt;VS item templates can add multiple files to your project (more details &lt;A href="http://msdn.microsoft.com/en-us/library/ms247115(VS.80).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms247115(VS.80).aspx"&gt;here&lt;/A&gt;). This means your custom item templates can include all kinds of files (e.g. workflow files, multiple T4 files, pre-canned code files, etc). For example, you can easily imagine a 3&lt;SUP&gt;rd&lt;/SUP&gt; party VS item template that adds 2 .tt files in your project that process the same EDMX file but generate different kinds of outputs.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;We hope this gives you an overview of our design and intent of this feature and we would love to hear your comments.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Sanjay Nagamangalam,&lt;/STRONG&gt; &lt;BR&gt;Lead PM, ADO.NET Entity Designer&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at &lt;/EM&gt;&lt;/STRONG&gt;&lt;A href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx"&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;FONT color=#4c6d7e&gt;this post&lt;/FONT&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/A&gt;&lt;STRONG&gt;&lt;EM&gt;. &lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9370115" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Customization/default.aspx">Customization</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Designer/default.aspx">Designer</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+4/default.aspx">Entity Framework 4</category></item></channel></rss>