<?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>ADO.NET team blog</title><link>http://blogs.msdn.com/b/adonet/</link><description /><dc:language>en</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>EF5 Release Candidate Available on NuGet</title><link>http://blogs.msdn.com/b/adonet/archive/2012/05/15/ef5-release-candidate-available-on-nuget.aspx</link><pubDate>Tue, 15 May 2012 20:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10305553</guid><dc:creator>dpblogs</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10305553</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/05/15/ef5-release-candidate-available-on-nuget.aspx#comments</comments><description>&lt;p&gt;A couple of months ago we released EF5 Beta 2. Since releasing Beta 2 we have made a number of changes to the code base, so we decided to publish a Release Candidate before we make the RTM available.&lt;/p&gt;  &lt;p&gt;This release is licensed for use in production applications. Because it is a pre-release version of EF5 there are some limitations, see the license for more details.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed Since Beta 2?&lt;/h2&gt;  &lt;p&gt;This Release Candidate includes the following changes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Added a CommandTimeout property to DbMigrationsConfiguration&lt;/strong&gt; to allow you to override the timeout for applying migrations to the database. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;We updated Code First to add tables to existing database&lt;/strong&gt; if the target database doesn’t contain any tables from the model. Previously, Code First would assume that the database contained the correct schema if it was pointed at an existing database that was not created using Code First. Now it checks to see if the database contains any of the tables from the model. If it does, Code First will continue to try and use the existing schema. If not, Code First will add the tables for the model to the database. This is useful in scenarios where a web hoster gives you a pre-created database to use, or adding a Code First model to a database created by the ASP.NET Membership Provider etc. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This release also includes fixes for the following bugs found in Beta 2:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Exception in partial trust applications ‘Request for ConfigurationPermission failed while attempting to access configuration section 'entityFramework'.’ &lt;/li&gt;    &lt;li&gt;Migrations: Using a login that has a default schema other than ‘dbo’ for the user causes runtime failures &lt;/li&gt;    &lt;li&gt;Migrations: DateTime format issue on non-en cultures &lt;/li&gt;    &lt;li&gt;Migrations: Migrate.exe does not set error code after a failure &lt;/li&gt;    &lt;li&gt;Migrations: Error renaming entity in many:many relationship &lt;/li&gt;    &lt;li&gt;Migrations: Checking for Seed override fails in partial trust &lt;/li&gt;    &lt;li&gt;Migrations: Better error message when startup project doesn't reference assembly with migrations &lt;/li&gt;    &lt;li&gt;Migrations: ModuleToProcess deprecated in PowerShell 3 causes warning when installing EF NuGet package &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What’s New in EF5?&lt;/h2&gt;  &lt;p&gt;EF 5 includes bug fixes to the 4.3.1 release and a number of new features. Most of the new features are only available in applications targeting .NET 4.5, see the Compatibility section for more details.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Enum&lt;/strong&gt; support allows you to have enum properties in your entity classes. This new feature is available for Model, Database and Code First. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Table-Valued functions&lt;/strong&gt; in your database can now be used with Database First. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Spatial data types&lt;/strong&gt; can now be exposed in your model using the DbGeography and DbGeometry types. Spatial data is supported in Model, Database and Code First. &lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;Performance enhancements&lt;/strong&gt; that &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx"&gt;we recently blogged about&lt;/a&gt; are included in EF 5. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Visual Studio 11 includes LocalDb database server rather than SQLEXPRESS.&lt;/strong&gt; During installation, the EntityFramework NuGet package checks which database server is available. The NuGet package will then update the configuration file by setting the default database server that Code First uses when creating a connection by convention. If SQLEXPRESS is running, it will be used. If SQLEXPRESS is not available then LocalDb will be registered as the default instead. No changes are made to the configuration file if it already contains a setting for the default connection factory. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The following new features are also available in the Entity Model Designer in &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Multiple-diagrams per model allows you to have several diagrams that visualize subsections of your overall model. &lt;/li&gt;    &lt;li&gt;Shapes on the design surface can now have coloring applied. &lt;/li&gt;    &lt;li&gt;Batch import of stored procedures allows multiple stored procedures to be added to the model during model creation. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting Started&lt;/h2&gt;  &lt;p&gt;You can get EF 5 Release Candidate by installing the latest pre-release version of the &lt;a href="http://nuget.org/packages/EntityFramework/"&gt;EntityFramework NuGet package&lt;/a&gt;. &lt;/p&gt;  &lt;div style="border-bottom: gray 4px solid; border-left: gray 4px solid; padding-bottom: 10px; background-color: black; padding-left: 15px; width: 550px; color: white; border-top: gray 4px solid; border-right: gray 4px solid; padding-top: 10px"&gt;PM&amp;gt; Install-Package EntityFramework –Pre&lt;/div&gt;  &lt;p&gt;These existing walkthroughs provide a good introduction to using the Code First, Model First &amp;amp; Database First workflows available in Entity Framework:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx"&gt;Model &amp;amp; Database First Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We have created walkthroughs for the new features in EF 5:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859576"&gt;EF 5.0 Enums Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859577"&gt;EF 5.0 Table-Valued Functions Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859721"&gt;EF 5.0 Spatial Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Compatibility&lt;/h2&gt;  &lt;p&gt;This version of the NuGet package is fully compatible with Visual Studio 2010 and &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt; and can be used for applications targeting .NET 4.0 and 4.5. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Some features are only available when writing an application that targets .NET 4.5. &lt;/strong&gt;This includes enum support, spatial data types, table-valued functions and the performance improvements. If you are targeting .NET 4.0 you still get all the bug fixes and other minor improvements.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Support&lt;/h2&gt;  &lt;p&gt;We are seeing a lot of great Entity Framework questions (and answers) from the community on &lt;a href="http://stackoverflow.com"&gt;Stack Overflow&lt;/a&gt;. As a result, our team is going to continue spending more time reading and answering questions posted on Stack Overflow. &lt;/p&gt;  &lt;p&gt;We would encourage you to &lt;a href="http://stackoverflow.com/questions/ask"&gt;post questions on Stack Overflow&lt;/a&gt; using the entity-framework tag. We will also continue to monitor the Entity Framework forum.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10305553" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-30-55-53/License.rtf" length="91518" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF5 Sample Provider Published</title><link>http://blogs.msdn.com/b/adonet/archive/2012/05/08/ef5-sample-provider-published.aspx</link><pubDate>Tue, 08 May 2012 21:28:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10302604</guid><dc:creator>dpblogs</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10302604</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/05/08/ef5-sample-provider-published.aspx#comments</comments><description>&lt;p&gt;We have published a new version of the sample provider that supports features introduced in the Entity Framework 5. It can be downloaded from the &lt;a href="http://code.msdn.microsoft.com/Entity-Framework-Sample-6a9801d0"&gt;MSDN Code Samples Gallery&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;This updated sample takes the sample provider code we released for Entity Framework 4 and demonstrates how to add support for spatial types and how to move to the new version of schema views to be able to reverse-engineer table valued functions (stored procedures with multiple resultsets did not require any changes to the provider). This version does not contain a sample provider for Migrations or Code First which currently use a separate provider model.&lt;/p&gt;  &lt;p&gt;The tests (which are now using xUnit) not only test the sample provider but also show how to build queries leveraging features introduced in Entity Framework 5. &lt;/p&gt;  &lt;p&gt;The code also contains a Data Designer Extensibility (DDEX) provider sample that now works with Visual Studio 11. &lt;/p&gt;  &lt;p&gt;Note: To build and use the sample Visual Studio 11 and .NET Framework 4.5 is needed. xUnit test runner is required to be able to run the tests inside the Visual Studio. See the &lt;a href="http://code.msdn.microsoft.com/Entity-Framework-Sample-6a9801d0"&gt;project page&lt;/a&gt; for more details.&lt;/p&gt;  &lt;p&gt;Pawel Kadluczka,    &lt;br /&gt;Entity Framework Developer&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10302604" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Sample+Provider/">Sample Provider</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Using SqlDataReader’s new async methods in .Net 4.5 Beta</title><link>http://blogs.msdn.com/b/adonet/archive/2012/04/20/using-sqldatareader-s-new-async-methods-in-net-4-5-beta.aspx</link><pubDate>Fri, 20 Apr 2012 19:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10295946</guid><dc:creator>dpblogs</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10295946</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/04/20/using-sqldatareader-s-new-async-methods-in-net-4-5-beta.aspx#comments</comments><description>&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;With the Developer Preview release of .Net 4.5, ADO.NET introduced two asynchronous methods to the &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;SqlDataReader&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt; class: &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh204846(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;ReadAsync&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt; and &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh204868(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;NextResultAsync&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;. These two methods allow you to move to the next row or result set asynchronously and represent a much more fine-grained level of asynchronous access to data compared to .Net 4.0 (which only had asynchronous command execution). However, for the Beta of .Net 4.5 we have gone a step further and introduced two new column-level asynchronous methods: &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh485626(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;IsDBNullAsync&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt; and &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh485670(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;GetFieldValueAsync&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="font-size: medium;" size="4"&gt;&lt;span style="color: #4f81bd;" color="#4f81bd"&gt;&lt;span style="font-family: Cambria;" face="Cambria"&gt;Flow of Data&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;To understand the need for different levels of asynchronous methods, we need to have a look at how SQL Server sends data to clients like ADO.NET.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19/7220.Picture.png"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19/7220.Picture.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table style="width: 100%;" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div class="shape"&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="color: #4f81bd;" color="#4f81bd"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Figure 1 - Row data sent from SQL Server via the TDS protocol&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="color: #4f81bd;" color="#4f81bd"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br clear="all" /&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;SQL Server sends data to the client using the Tabular Data Stream (TDS) protocol which, as shown in the diagram above, separates each result set with a &amp;ldquo;DONE&amp;rdquo; token, each row with a &amp;ldquo;ROW&amp;rdquo; token and each column has both a header and actual data (note that this is a simplification of TDS useful for understanding ADO.NET, if you want to learn more about TDS you can &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc448435.aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;get the full specification on the MSDN&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;). So, when you call NextResult, ADO.NET reads the rest of the current result set and the &amp;ldquo;DONE&amp;rdquo; token afterwards, and then returns true if there is more data available. Similarly, calling Read will read off the rest of the current row and the next &amp;ldquo;ROW&amp;rdquo; token and getting the value for a column will cause the data for any preceding column to also be read. For getting column values there is also multiple behaviors for what to do with the previous columns&amp;rsquo; data: in &amp;ldquo;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.commandbehavior(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;Default&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&amp;rdquo; mode (also called &amp;ldquo;non-sequential&amp;rdquo; mode) the previous columns&amp;rsquo; data is stored and you can go back and re-read that column from SqlDataReader&amp;rsquo;s data storage; whereas in &amp;ldquo;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.commandbehavior(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;SequentialAccess&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&amp;rdquo; mode the data for the previous columns is discarded and you will get an exception if you try to re-read it.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;For the new asynchronous methods in .Net 4.5, their behavior is exactly the same as with the synchronous methods, except for one notable exception: ReadAsync in non-sequential mode. The difference is that ReadAsync will not just read up to the &amp;ldquo;ROW&amp;rdquo; token of the current row, but will also read and parse all of the column data for that row as well. By doing this, ADO.NET only pays the overhead to call into its parsing code once per row, instead of paying it every time a new column is read.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="font-size: medium;" size="4"&gt;&lt;span style="color: #4f81bd;" color="#4f81bd"&gt;&lt;span style="font-family: Cambria;" face="Cambria"&gt;Asynchronous column access&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;An important guidance for writing asynchronous APIs is to ensure that asynchronous methods do large or &amp;ldquo;chunky&amp;rdquo; operations, which doesn&amp;rsquo;t seem to mesh well with column-level asynchronous access until you look closely at how they work. Consider a table that has a varchar column with about 2000 characters per row. This means that you can fit about four of these columns per packet (assuming that you are using the default packet size, and ignoring any headers or other overhead). If you are reading the row via the Read method, or you are in sequential mode, then getting the column synchronously will block the thread for every fifth row. For an application or web server, having every fifth row block a thread can end up becoming a serious scalability issue. The same issue can occur if you are ignoring this 2000 character column, but checking if the next column in the row is null: IsDBNull needs to read the header for the target column, which means that it will need to read past any previous columns (such as our 2000 character column). This can become even worse if you have a column that is megabytes, or even gigabytes, in size, since you will be typically using sequential mode, and you can partially read such a column (using methods like GetStream), which means that IsDBNull will need to read off the rest of the row synchronously.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="font-size: medium;" size="4"&gt;&lt;span style="color: #4f81bd;" color="#4f81bd"&gt;&lt;span style="font-family: Cambria;" face="Cambria"&gt;When to use what&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;The new asynchronous methods that SqlDataReader exposes make it very easy to create a scalable application, but they only provide a benefit if their synchronous counterpart would have blocked a thread, and that only happens if ADO.NET requires more data from the network.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;So to choose whether or not to call an asynchronous method, you need to consider how much data the method is likely to use and therefore if it is likely to require a new network packet. You can calculate the probability of needing a new packet by comparing the size of the data you are reading with the maximum packet size, or you can use the following guidelines:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;b&gt;Default (non-sequential) mode vs SequentialAccess&lt;/b&gt; &amp;ndash; It is important to decide &lt;/span&gt;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh204861(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;before you create a SqlDataReader&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt; whether you want to use non-sequential or sequential access mode. In most cases using the Default (non-sequential) access mode is the better choice, as it allows an easier programming model (you can access any column in any order) and you will get better performance using ReadAsync. However, since non-sequential access mode has to store the data for the entire row, it can cause issues if you are reading a large column from the server (such as varbinary(MAX), varchar(MAX), nvarchar(MAX) or XML). In this case using sequential access mode will allow you to stream these large columns rather than having to buffer the entire column into memory.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;b&gt;NextResult vs. NextResultAsync&lt;/b&gt; &amp;ndash; Whenever possible, you should be using NextResultAsync() to move between result sets. This will allow leftover rows in the old result set to be read off asynchronously, as well as other TDS tokens (which were excluded from figure 1 for simplicity) which can sit between result sets.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;b&gt;Read vs ReadAsync&lt;/b&gt; &amp;ndash; It is also a good idea to call ReadAsync: in sequential mode this will read in all column data, which can potential span multiple packets, allowing faster access to the column values. In non-sequential mode ADO.NET will need to finish reading the data for the current row (if it hasn&amp;rsquo;t been read entirely), and there is the potential for some TDS tokens to sit between rows which can then be read asynchronously.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;b&gt;IsDBNull and GetFieldValue&amp;lt;T&amp;gt; vs IsDBNullAsync and GetFieldValueAsync&amp;lt;T&amp;gt;&lt;/b&gt; - If you had previously called ReadAsync and are using non-sequential access, then calling the synchronous versions of these methods will provide the best performance since the column data has already been read and processed (so calling the asynchronous method just adds the overhead of wrapping the value in a Task). However, if you called Read in non-sequential access mode, or if you are using sequential access mode, then the decision is much harder as you need to consider how much data you need to read to get to your desired column and how much data that column may contain. If you&amp;rsquo;ve read the previous column, and the target column is small (like a Boolean, a DateTime or a numeric type) then you may want to consider using a synchronous method. Alternatively, if the target column is large (like a varbinary(8000)) or you need to read past large columns, then using an asynchronous method is much better. Finally, if the target column is massive (like a varbinary(MAX), varchar(MAX), nvarchar(MAX) or XML) then you should consider the new &lt;/span&gt;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstream(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;GetStream&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;, &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.gettextreader(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;GetTextReader&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt; or &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getxmlreader(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;GetXmlReader&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt; methods instead.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="font-size: medium;" size="4"&gt;&lt;span style="color: #4f81bd;" color="#4f81bd"&gt;&lt;span style="font-family: Cambria;" face="Cambria"&gt;Test early, test often&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Finally, please be aware that the recommendations above are based on the workings of ADO.NET and what we have observed in our tests. You need to consider what type of data your application is using to see at what level (result set, row or column) it makes sense to call asynchronous methods. The best way to check this is to build up a suite of performance and scalability tests such that you can accurately measure and tune your application.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;And the best way to do this is with the &lt;/span&gt;&lt;a href="http://www.microsoft.com/visualstudio/11/en-us/downloads"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;Visual Studio 11 and .Net 4.5 betas&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Happy Coding!&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Daniel Paoliello&lt;br /&gt;Software Development Engineer&lt;br /&gt;ADO.NET Managed Providers and DataSet Team&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10295946" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/SQL+Server+2008/">SQL Server 2008</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ASP-NET/">ASP.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/SQLClient/">SQLClient</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/SQL+Azure/">SQL Azure</category></item><item><title>EF Power Tools Beta 2 Available</title><link>http://blogs.msdn.com/b/adonet/archive/2012/04/09/ef-power-tools-beta-2-available.aspx</link><pubDate>Mon, 09 Apr 2012 22:29:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10292042</guid><dc:creator>dpblogs</dc:creator><slash:comments>30</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10292042</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/04/09/ef-power-tools-beta-2-available.aspx#comments</comments><description>&lt;p&gt;The Entity Framework Power Tools provide additional design-time tools in Visual Studio to help you develop using the Entity Framework. Today we are releasing Beta 2 of the Power Tools. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Where do I get it? &lt;/h2&gt;  &lt;p&gt;The &lt;a href="http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d"&gt;Entity Framework Power Tools&lt;/a&gt; are available on the Visual Studio Gallery. &lt;/p&gt;  &lt;p&gt;You can also install them directly from Visual Studio by selecting 'Tools -&amp;gt; Extension Manager...' then searching for &amp;quot;Entity Framework Power Tools&amp;quot; in the Online Gallery. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;If you encounter an error while upgrading from an older version of EF Power Tools, you will need to uninstall the older version first then install EF Power Tools Beta 2.&lt;/font&gt;&lt;/strong&gt; This is due to a bug in Visual Studio that blocks the upgrade because the certificate used to sign Beta 2 has a different expiry date than Beta 1.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What's new in Beta 2? &lt;/h2&gt;  &lt;p&gt;Beta 2 improves the quality of the previous release, and also adds new functionality. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Code generated by reverse engineer can be customized using T4 templates.&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Fixed many reverse engineer bugs&lt;/strong&gt; relating to the code that we generate. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Generate Views (formerly Optimize Entity Data Model) is now available for database-first and model-first.&lt;/strong&gt; Right-click on an EDMX file to access the command. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Enhanced DbContext and database discovery.&lt;/strong&gt; We fixed many bugs around how we locate your DbContext and the database it connects to. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For a complete list of the features included in the Power Tools see the What does it add to Visual Studio section. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Compatibility &lt;/h2&gt;  &lt;p&gt;The Power Tools are compatible with Visual Studio 2010 and Entity Framework 4.2 or later. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;You can also install the Power Tools on Visual Studio 11 Beta, but you may receive the error &amp;quot;A constructible type deriving from DbContext could not be found in the selected file.&amp;quot;&lt;/strong&gt; This is caused by a bug in the Visual Studio 11 unit test tools where an older version of EntityFramework.dll is being loaded. We are working closely with that team to resolve the issue. As a workaround, you can delete the HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0_Config\BindingPaths\{BFC24BF4-B994-4757-BCDC-1D5D2768BF29} registry key. Be aware, however, that this will cause the Unit Test commands to stop working. Thank you, early adopters, for your superhuman patience with prerelease software. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Support &lt;/h2&gt;  &lt;p&gt;This release is a preview of features that we are considering for a future release and is designed to allow you to provide feedback on the design of these features. EF Power Tools Beta 2 is not intended or licensed for use in production. &lt;/p&gt;  &lt;p&gt;If you have a question, &lt;a href="http://stackoverflow.com/questions/ask"&gt;ask it on Stack Overflow&lt;/a&gt; using the entity-framework tag. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What does it add to Visual Studio? &lt;/h2&gt;  &lt;p&gt;EF Power Tools Beta 2 is for Code First, model-first, and database-first development and adds the following context menu options to an 'Entity Framework' sub-menu inside Visual Studio. &lt;/p&gt;  &lt;h3&gt;When right-clicking on a C# project &lt;/h3&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0363.ProjectMenu_5F00_74E128CA.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="ProjectMenu" border="0" alt="ProjectMenu" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3000.ProjectMenu_5F00_thumb_5F00_499C91C3.png" width="620" height="234" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Reverse Engineer Code First&lt;/strong&gt;      &lt;br /&gt;This command allows one-time generation of Code First mappings for an existing database. This option is useful if you want to use Code First to target an existing database as it takes care of a lot of the initial coding. The command prompts for a connection to an existing database and then reverse engineers POCO classes, a derived DbContext, and Code First mappings that can be used to access the database.       &lt;ul&gt;       &lt;li&gt;If you have not already added the EntityFramework NuGet package to your project, the latest version will be downloaded as part of running reverse engineer. &lt;/li&gt;        &lt;li&gt;The reverse engineer process by default produces a complete mapping using the fluent API. Items such as column name will always be configured, even when they would be correctly inferred by conventions. This allows you to refactor property/class names etc. without needing to manually update the mapping.          &lt;ul&gt;           &lt;li&gt;The Customize Reverse Engineer Templates command (see below) lets you customize how code is generated. &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;        &lt;li&gt;A connection string is added to the App/Web.config file and is used by the context at runtime. If you are reverse engineering to a class library, you will need to copy this connection string to the configuration file of the consuming application(s). &lt;/li&gt;        &lt;li&gt;This process is designed to help with the initial coding of a Code First model. You may need to adjust the generated code if you have a complex database schema or are using advanced database features. &lt;/li&gt;        &lt;li&gt;Running this command multiple times will overwrite any previously generated files, including any changes that have been made to generated files. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Customize Reverse Engineer Templates&lt;/strong&gt;      &lt;br /&gt;Adds the default reverse engineer T4 templates to your project for editing. After updating these files, run the Reverse Engineer Code First command again to reverse engineer POCO classes, a derived DbContext, and Code First mappings using your project's customized templates.       &lt;ul&gt;       &lt;li&gt;The templates are added to your project under the CodeTemplates\ReverseEngineerCodeFirst folder. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;When right-clicking on a code file containing a derived DbContext class &lt;/h3&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0451.ItemMenu_5F00_2D8BD2D8.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="ItemMenu" border="0" alt="ItemMenu" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3107.ItemMenu_5F00_thumb_5F00_6D55B95D.png" width="567" height="288" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;View Entity Data Model (Read-only)&lt;/strong&gt;      &lt;br /&gt;Displays the Code First model in the Entity Framework designer.       &lt;ul&gt;       &lt;li&gt;This is a read-only representation of the model; you cannot update the Code First model using the designer. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;View Entity Data Model XML&lt;/strong&gt;      &lt;br /&gt;Displays the EDMX XML representing the Code First model. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;View Entity Data Model DDL SQL&lt;/strong&gt;      &lt;br /&gt;Displays the DDL SQL to create the database targeted by the Code First model. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Generate Views&lt;/strong&gt;      &lt;br /&gt;Generates pre-compiled views used by the EF runtime to improve start-up performance. Adds the generated views file to the containing project.       &lt;ul&gt;       &lt;li&gt;View compilation is discussed in the &lt;a href="http://msdn.microsoft.com/en-us/library/cc853327.aspx"&gt;Performance Considerations&lt;/a&gt; article on MSDN. &lt;/li&gt;        &lt;li&gt;If you change your model then you will need to re-generate the pre-compiled views by running this command again. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;When right-clicking on an Entity Data Model (*.edmx) file &lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Generate Views&lt;/strong&gt;      &lt;br /&gt;This is the same as the Generate Views command above except that, instead of generating pre-compiled views for a derived DbContext class, it generates them for your database-first or model-first entity data model. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Brice Lambson   &lt;br /&gt;ADO.NET Entity Framework &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10292042" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF5 Performance Considerations</title><link>http://blogs.msdn.com/b/adonet/archive/2012/04/05/ef5-performance-considerations.aspx</link><pubDate>Thu, 05 Apr 2012 23:49:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10291333</guid><dc:creator>dpblogs</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10291333</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/04/05/ef5-performance-considerations.aspx#comments</comments><description>&lt;p&gt;Over the past few weeks the EF team has been putting together a whitepaper that talks about the performance considerations developers should take when using Entity Framework. Performance is one critical aspect of developing modern applications, and this document will help developers make informed design decisions and get the most out of their applications when using the Entity Framework 5 (and also EF 4).&lt;/p&gt;  &lt;p&gt;There’s no such thing as a magic bullet when it comes to performance. Some of the topics covered by this whitepaper are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;View generation. &lt;/li&gt;    &lt;li&gt;Caching. &lt;/li&gt;    &lt;li&gt;Query Execution Options. &lt;/li&gt;    &lt;li&gt;Inheritance Strategies. &lt;/li&gt;    &lt;li&gt;Lazy Loading vs. Eager Loading. &lt;/li&gt;    &lt;li&gt;And more. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The document can be found here: &lt;a title="http://msdn.microsoft.com/en-us/data/hh949853" href="http://msdn.microsoft.com/en-us/data/hh949853"&gt;http://msdn.microsoft.com/en-us/data/hh949853&lt;/a&gt;. So go ahead: read it, and let us know your thoughts.&lt;/p&gt;  &lt;p&gt;David Obando,   &lt;br /&gt;Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10291333" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Performance/">Performance</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF5 Beta 2 Available on NuGet</title><link>http://blogs.msdn.com/b/adonet/archive/2012/03/22/ef5-beta-2-available-on-nuget.aspx</link><pubDate>Thu, 22 Mar 2012 16:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10286469</guid><dc:creator>dpblogs</dc:creator><slash:comments>38</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10286469</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/03/22/ef5-beta-2-available-on-nuget.aspx#comments</comments><description>&lt;p&gt;A few weeks ago we released EF5 Beta 1, since then we’ve been working to improve quality and polish up the release. Today we are making EF5 Beta 2 available on NuGet. &lt;/p&gt;  &lt;p&gt;This release is licensed for use in production applications. Because it is a pre-release version of EF5 there are some limitations, see the license for more details.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed Since Beta 1?&lt;/h2&gt;  &lt;p&gt;Beta 2 is mainly about improving quality, here are the more notable changes since Beta 1:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Migrations commands now work in Visual Studio 2010&lt;/strong&gt;. There was a bug in Beta 1 that caused them to only work in Visual Studio 11 Beta.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Moved database related Data Annotations to &lt;em&gt;System.ComponentModel.DataAnnotations.Schema&lt;/em&gt; namespace&lt;/strong&gt;. The annotations that originally shipped in EntityFramework.dll have moved into System.ComponentModel.dll in .NET 4.5. As part of this move, the database related annotations were moved into a .Schema sub-namespace. In EF 5 we made the same namespace change for applications targeting .NET 4.0.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Fixed the ‘Sequence contains no elements’ bug&lt;/strong&gt; that several users reported on Beta 1.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Simplified the web.config/app.config settings to register SQLEXPRESS or LocalDb as the default.&lt;/strong&gt; We added a LocalDbConnectionFactory which makes the config that is created while installing the NuGet package much simpler. See the ‘Visual Studio 11 includes LocalDb rather than SQLEXPRESS.’ point in the next section for more details on how the default database is selected.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;LocalDb database now created in App_Data directory of ASP.NET applications by default.&lt;/strong&gt; The new LocalDbConnectionFactory will add LocalDb database files to the App_Data directory when used in ASP.NET applications.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;EF Power Tools Beta 2 is Coming&lt;/h2&gt;  &lt;p&gt;Entity Framework has progressed a lot in the last 6 months and we’ve let the Power Tools fall behind. We are working on an updated version at the moment and we’re aiming to have it available in the next couple of weeks.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Simplified &lt;a href="http://msdn.com/data/ef"&gt;msdn.com/data/ef&lt;/a&gt; &lt;/h2&gt;  &lt;p&gt;We’ve been doing some work to simplify the main Entity Framework to make it a better starting point for our product. There is still plenty of room for improvement but you should find the site much more useful already.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What’s New in EF5?&lt;/h2&gt;  &lt;p&gt;EF 5 includes bug fixes to the 4.3.1 release and a number of new features. Most of the new features are only available in applications targeting .NET 4.5, see the Compatibility section for more details.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Enum&lt;/strong&gt; support allows you to have enum properties in your entity classes. This new feature is available for Model, Database and Code First. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Table-Valued functions&lt;/strong&gt; in your database can now be used with Database First. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Spatial data types&lt;/strong&gt; can now be exposed in your model using the DbGeography and DbGeometry types. Spatial data is supported in Model, Database and Code First. &lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;Performance enhancements&lt;/strong&gt; that &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx"&gt;we recently blogged about&lt;/a&gt; are included in EF 5 Beta 1. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Visual Studio 11 includes LocalDb database server rather than SQLEXPRESS.&lt;/strong&gt; During installation, the EntityFramework NuGet package checks which database server is available. The NuGet package will then update the configuration file by setting the default database server that Code First uses when creating a connection by convention. If SQLEXPRESS is running, it will be used. If SQLEXPRESS is not available then LocalDb will be registered as the default instead. No changes are made to the configuration file if it already contains a setting for the default connection factory.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The following new features are also available in the Entity Model Designer in &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Multiple-diagrams per model allows you to have several diagrams that visualize subsections of your overall model. &lt;/li&gt;    &lt;li&gt;Shapes on the design surface can now have coloring applied. &lt;/li&gt;    &lt;li&gt;Batch import of stored procedures allows multiple stored procedures to be added to the model during model creation. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting Started&lt;/h2&gt;  &lt;p&gt;You can get EF 5 Beta 2 by installing the latest pre-release version of the &lt;a href="http://nuget.org/packages/EntityFramework/"&gt;EntityFramework NuGet package&lt;/a&gt;. &lt;/p&gt;  &lt;div style="border-bottom: gray 4px solid; border-left: gray 4px solid; padding-bottom: 10px; background-color: black; padding-left: 15px; width: 550px; color: white; border-top: gray 4px solid; border-right: gray 4px solid; padding-top: 10px"&gt;PM&amp;gt; Install-Package EntityFramework –Pre&lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;These existing walkthroughs provide a good introduction to using the Code First, Model First &amp;amp; Database First workflows available in Entity Framework:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx"&gt;Model &amp;amp; Database First Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We have created walkthroughs for the new features in EF 5:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859576"&gt;EF 5.0 Enums Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859577"&gt;EF 5.0 Table-Valued Functions Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859721"&gt;EF 5.0 Spatial Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Compatibility&lt;/h2&gt;  &lt;p&gt;This version of the NuGet package is fully compatible with Visual Studio 2010 and &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt; and can be used for applications targeting .NET 4.0 and 4.5. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Some features are only available when writing an application that targets .NET 4.5. &lt;/strong&gt;This includes enum support, spatial data types, table-valued functions and the performance improvements. If you are targeting .NET 4.0 you still get all the bug fixes and other minor improvements.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Support&lt;/h2&gt;  &lt;p&gt;We are seeing a lot of great Entity Framework questions (and answers) from the community on &lt;a href="http://stackoverflow.com"&gt;Stack Overflow&lt;/a&gt;. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. &lt;/p&gt;  &lt;p&gt;We would encourage you to &lt;a href="http://stackoverflow.com/questions/ask"&gt;post questions on Stack Overflow&lt;/a&gt; using the entity-framework tag. We will also continue to monitor the Entity Framework forum.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10286469" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-28-64-69/License.rtf" length="91563" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Safer passwords with SqlCredential</title><link>http://blogs.msdn.com/b/adonet/archive/2012/03/09/safer-passwords-with-sqlcredential.aspx</link><pubDate>Fri, 09 Mar 2012 17:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10280510</guid><dc:creator>dpblogs</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10280510</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/03/09/safer-passwords-with-sqlcredential.aspx#comments</comments><description>&lt;h1&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;b&gt;Introduction&lt;/b&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;Many users of &lt;span&gt;SqlClient&lt;/span&gt; with SQL Server Authentication have expressed interest in setting credentials outside of the connection string to mitigate the memory dump vulnerability of keeping the User Name and Password in the connection string. Starting with .Net Framework 4.5, we have introduced the ability to set the credentials outside of the connection string via the new &lt;span&gt;SqlCredential&lt;/span&gt; Credential property of &lt;span&gt;SqlConnection&lt;/span&gt;. Now the developer can create a &lt;span&gt;SqlCredential&lt;/span&gt; object with a &lt;span&gt;UserId&lt;/span&gt; and a &lt;span&gt;SecureString&lt;/span&gt; Password to hold the credential values of a connection when connecting to a server. This helps mitigate the threat of credentials being leaked out to the page file in a page swap or being evident in a crash dump.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h1&gt;&lt;b&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Use Case Example&lt;/span&gt;&lt;/b&gt;&lt;/h1&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;System.Windows.Controls.TextBox txtUserId = new System.Windows.Controls.TextBox();&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;System.Windows.Controls.PasswordBox txtPwd = new System.Windows.Controls.PasswordBox();&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;using (SqlConnection conn = new SqlConnection("Server=myServer;Initial Catalog=myDB;"))&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;{&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left: 30px;" class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;SecureString pwd = txtPwd.SecurePassword;&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left: 30px;" class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;pwd.MakeReadOnly();&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left: 30px;" class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;SqlCredential cred = new SqlCredential(txtUserId.Text, pwd);&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left: 30px;" class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;conn.Credential = cred;&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left: 30px;" class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;conn.Open();&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;}&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Alternatively we can use the new SqlConnection constructor overload which takes both a connection string and credential object:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;SecureString pwd = txtPwd.SecurePassword;&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;pwd.MakeReadOnly();&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;SqlCredential cred = new SqlCredential(txtUserId.Text, pwd);&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;using (SqlConnection conn = new SqlConnection("Server=myServer;Initial Catalog=myDB;", cred))&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;{&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left: 30px;" class="scroll"&gt;&lt;span style="font-size: small;"&gt;&lt;code class="csharp"&gt;conn.Open();&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="scroll"&gt;&lt;code class="csharp"&gt;&lt;span style="font-size: small;"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;b&gt;&lt;span&gt;&lt;span&gt;SqlCredential&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt; Class&lt;/b&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&lt;span&gt;More information about the new &lt;span&gt;SqlCredential&lt;/span&gt; class can be found at:&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcredential(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;&lt;span&gt;http://msdn.microsoft.com/en-us/library/system.data.&lt;span&gt;sqlclient&lt;/span&gt;.&lt;span&gt;SqlCredential&lt;/span&gt;(v=vs.110).&lt;span&gt;aspx&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&lt;span&gt;For information on how to get or set the &lt;span&gt;SqlConnection&lt;/span&gt;.Credential property, please refer to:&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.credential(v=vs.110).aspx"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;&lt;span&gt;http://msdn.microsoft.com/en-us/library/system.data.&lt;span&gt;sqlclient&lt;/span&gt;.&lt;span&gt;sqlconnection&lt;/span&gt;.credential(v=vs.110).&lt;span&gt;aspx&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;It&amp;rsquo;s important to note that the &lt;span&gt;SqlCredential&lt;/span&gt; constructor only allows &lt;span&gt;SecureString&lt;/span&gt; marked as read only to be passed in as the Password parameter or it will raise an &lt;span&gt;ArgumentException&lt;/span&gt;. The new credential property is incompatible with existing &lt;span&gt;UserId&lt;/span&gt;, Password, Context Connection=True, and Integrated Security=True connection string keywords, and setting the credential property on an open connection is not allowed. It is strongly recommended that you set &lt;span&gt;PersistSecurityInfo&lt;/span&gt;=False (default) so the credential property is not returned as part of the connection once it is opened.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h1&gt;&lt;b&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Connection Pooling with Credential Property&lt;/span&gt;&lt;/b&gt;&lt;/h1&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;With this new improvement now the connection pooling algorithm also takes the Credential property into consideration in addition to the connection string property when creating connection pools and getting connections from the pool. Connections with the same connection string and same instance of Credential property will use the same connection pool, but connections with different instances of the Credential property will use different connection pools, even if they use the same &lt;span&gt;UserId&lt;/span&gt; and Password. For example, the developer tries to open several connections with different configurations as below:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;string str1 = &amp;ldquo;Server=&lt;span&gt;myServer&lt;/span&gt;;Initial Catalog=&lt;span&gt;myDB&lt;/span&gt;;User Id=user1;Password=pwd1;&amp;rdquo;;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;string str2 = &amp;ldquo;Server=&lt;span&gt;myServer&lt;/span&gt;;Initial Catalog=&lt;span&gt;myDB&lt;/span&gt;;&amp;rdquo;;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;SqlCredential&lt;/span&gt; cred1 = new &lt;span&gt;SqlCredential&lt;/span&gt;(user1, pwd1);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;SqlCredential&lt;/span&gt; cred2 = new &lt;span&gt;SqlCredential&lt;/span&gt;(user1, pwd1);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;SqlCredential&lt;/span&gt; cred3 = new &lt;span&gt;SqlCredential&lt;/span&gt;(user2, pwd2);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;1.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;&lt;span&gt;SqlConnection&lt;/span&gt; conn1 = &lt;span&gt;SqlConnection&lt;/span&gt;(str1, null);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //different connection string&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;2.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;&lt;span&gt;SqlConnection&lt;/span&gt; conn2 = &lt;span&gt;SqlConnection&lt;/span&gt;(str2, cred1); //different credential object&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;3.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;&lt;span&gt;SqlConnection&lt;/span&gt; conn3 = &lt;span&gt;SqlConnection&lt;/span&gt;(str2, cred2); //different credential object&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;4.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;&lt;span&gt;SqlConnection&lt;/span&gt; conn4 = &lt;span&gt;SqlConnection&lt;/span&gt;(str2, cred3); //different credential object and user/&lt;span&gt;pwd&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;All 4 connections will use different connection pools, the most important thing to note here is that conn2 and conn3 will not share the same connection pool, as they use different credential object instances, even though those two instances use the same &lt;span&gt;UserId&lt;/span&gt; and Password.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h1&gt;&lt;b&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;Using &lt;span&gt;SqlCredential&lt;/span&gt; with other classes&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h1&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;To use the new secure password feature with &lt;span&gt;SqlDataAdapter&lt;/span&gt; or &lt;span&gt;SqlBulkCopy&lt;/span&gt;, a &lt;span&gt;SqlConnection&lt;/span&gt; object with &lt;span&gt;SqlCredential&lt;/span&gt; property needs to be constructed first and passed into the appropriate constructor of &lt;span&gt;SqlDataAdapter&lt;/span&gt; and &lt;span&gt;SqlBulkCopy&lt;/span&gt; that takes in a &lt;span&gt;SqlConnection&lt;/span&gt; object rather than a connection string. The &lt;span&gt;SqlDependency&lt;/span&gt; class currently does not support starting a listener for receiving dependency change notifications from SQL Server for both connection string and credential.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;Of course the usage of SQL Server Integrated Authentication Mode is still the recommended way to authenticate for users with an Active Directory&amp;reg; infrastructure as there is no credential propagation and all security sensitive information is stored in the Active Directory&amp;rsquo;s database. And the usage of SQL Server Mixed Mode Authentication with &lt;span&gt;UserId&lt;/span&gt; and Password specified in the connection string remains unchanged.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Stay safe and secure,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span&gt;&lt;span&gt;Wenchang&lt;/span&gt; &lt;span&gt;Liu&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;ADO.NET Software Development Engineer in Test&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10280510" width="1" height="1"&gt;</description></item><item><title>EF4.3.1 and EF5 Beta 1 Available on NuGet</title><link>http://blogs.msdn.com/b/adonet/archive/2012/02/29/ef4-3-1-and-ef5-beta-1-available-on-nuget.aspx</link><pubDate>Wed, 29 Feb 2012 14:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10274108</guid><dc:creator>dpblogs</dc:creator><slash:comments>102</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10274108</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/02/29/ef4-3-1-and-ef5-beta-1-available-on-nuget.aspx#comments</comments><description>&lt;p&gt;Today we are pleased to announce the release of two new versions of the EntityFramework NuGet package. &lt;/p&gt;  &lt;p&gt;The 4.3.1 version includes some bug fixes for the 4.3 package we released a few weeks ago. We are also making the first beta of EF 5 available as a pre-release package. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;hr /&gt;  &lt;h2&gt;What’s in 5.0.0-beta1&lt;/h2&gt;  &lt;p&gt;This is the first preview of EF 5 and includes all the features and bug fixes in EF 4.3.1 in addition to a number of new features that are available in applications targeting .NET Framework 4.5. (&lt;em&gt;You will need &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt; to work with the .NET Framework 4.5)&lt;/em&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Enum&lt;/strong&gt; support allows you to have enum properties in your entity classes. This new feature is available for Model, Database and Code First. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Table-Valued functions&lt;/strong&gt; in your database can now be used with Database First. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Spatial data types&lt;/strong&gt; can now be exposed in your model using the DbGeography and DbGeometry types. Spatial data is supported in Model, Database and Code First. &lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;Performance enhancements&lt;/strong&gt; that &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx"&gt;we recently blogged about&lt;/a&gt; are included in EF 5 Beta 1. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Visual Studio 11 includes LocalDb rather than SQLEXPRESS.&lt;/strong&gt; The EntityFramework NuGet package will now check which database is available when the package is installed and use a configuration file setting to set the default database server that Code First databases will be created on.&lt;strong&gt; &lt;/strong&gt;&lt;font style="background-color: #ffff00"&gt;If SQLEXPRESS is running, it will be used.&lt;/font&gt; If SQLEXPRESS is not available then LocalDb will be registered as the default instead. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The following new features are also available in the Entity Model Designer in &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Multiple-diagrams per model allows you to have several diagrams that visualize subsections of your overall model. &lt;/li&gt;    &lt;li&gt;Shapes on the design surface can now have coloring applied. &lt;/li&gt;    &lt;li&gt;Batch import of stored procedures allows multiple stored procedures to be added to the model during model creation. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font color="#ff0000"&gt;Known Issues&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&lt;font color="#000000"&gt;We will keep this list updated with known issues for this release:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font color="#000000"&gt;There is a known issue using the Code First Migrations commands in Visual Studio 2010.&lt;/font&gt; We are working on an updated package that addresses this issue. You will receive the following error when using migrations commands from EF 5 Beta 1 in Visual Studio 2010:       &lt;br /&gt;&lt;em&gt;Could not load file or assembly 'Microsoft.VisualStudio.Shell.10.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;There is a known issue in MVC4 applications in Visual Studio 11 that is &lt;a href="http://go.microsoft.com/fwlink/?LinkId=243166"&gt;described in more detail here&lt;/a&gt;.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;h3&gt;Getting Started&lt;/h3&gt;  &lt;p&gt;You can get EF 5 Beta 1 by installing the latest pre-release version of the &lt;a href="http://nuget.org/packages/EntityFramework/"&gt;EntityFramework NuGet package&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5305.InstallPackage_5F00_5C3674DE.jpg"&gt;&lt;img title="InstallPackage" border="0" alt="InstallPackage" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/6471.InstallPackage_5F00_thumb_5F00_54AB0571.jpg" width="743" height="71" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;These existing walkthroughs provide a good introduction to using the Code First, Model First &amp;amp; Database First workflows available in Entity Framework:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx"&gt;Model &amp;amp; Database First Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We have created walkthroughs for the new features in EF 5:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;a href="http://msdn.com/en-us/hh859576"&gt;EF 5.0 Enums Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859577"&gt;EF 5.0 Table-Valued Functions Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.com/en-us/hh859721"&gt;EF 5.0 Spatial Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;h3&gt;Compatibility&lt;/h3&gt;  &lt;p&gt;This version of the NuGet package is fully compatible with &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;Visual Studio 11 Beta&lt;/a&gt; and can be used for applications targeting .NET 4.0 and 4.5. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The new features listed above will only be available when writing an application that targets .NET 4.5. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;EF 5 will work with Visual Studio 2010 but there is currently a known issue that prevents the Code First Migrations commands from being used in Visual Studio 2010. See the Known Issues section (above) for more details.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;hr /&gt;  &lt;h2&gt;What’s in 4.3.1?&lt;/h2&gt;  &lt;p&gt;A few weeks back we released EF 4.3, which included the new Code First Migrations feature and a few bug fixes. Today we are releasing the 4.3.1 patch release that includes some bug fixes to the 4.3 release. &lt;/p&gt;  &lt;p&gt;This is a fully supported go-live release.&lt;/p&gt;  &lt;p&gt;The changes in 4.3.1 compared to 4.3 include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Templates for using DbContext with Model First and Database First have been renamed from &lt;strong&gt;ADO.NET DbContext Generator&lt;/strong&gt; to &lt;strong&gt;EF 4.x DbContext Generator. &lt;/strong&gt;We made this rename to easily distinguish between the &lt;strong&gt;EF 5.x DbContext Generator&lt;/strong&gt; that is to be used for EF 5 applications. &lt;/li&gt;    &lt;li&gt;Added the ability to enable Code First Migrations against an existing database. &lt;a href="http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/"&gt;Julie Lerman blogged about&lt;/a&gt; using an empty initial migration to capture the initial state of the model when it is in sync with an existing database. Once this migration is generated Code First Migrations will correctly detect and scaffold changes to your model. You can now use the –IgnoreChanges to tell Code First Migrations not to scaffold any code for this initial migration (i.e. ‘Add-Migration InitialMigration –IgnoreChanges). &lt;/li&gt;    &lt;li&gt;Visual Studio 11 includes LocalDb rather than SQLEXPRESS. The EntityFramework NuGet package will now check which database is available when the package is installed and use a configuration file setting to set the default database server that Code First databases will be created on. If SQLEXPRESS is running, it will be used. If SQLEXPRESS is not available then LocalDb will be registered as the default. &lt;/li&gt;    &lt;li&gt;Fixed issue where TPC mapping in Code First was generating “Unknown Columnname” exceptions when accessing the database. &lt;/li&gt;    &lt;li&gt;Fixed issue where hard coded column data types were not honored in generated databases. If you used the Column data annotation or HasColumnType Fluent API method to hard code a column data type (i.e. xml, money, etc.), this data type was not used when creating the database. &lt;/li&gt;    &lt;li&gt;Fixed issue preventing decimal columns from being configured with the ‘Identity’ store generated pattern. &lt;/li&gt;    &lt;li&gt;We now provide a better exception message when context/database initialization fails due to connectivity issues with the database. &lt;/li&gt;    &lt;li&gt;Enabled support for configsource in configuration files when using Code First Migrations. &lt;/li&gt;    &lt;li&gt;Fixed issues using Moq with DbContext – as reported in this &lt;a href="http://stackoverflow.com/questions/9245402/entity-framework-4-3-and-moq-cant-create-dbcontext-mock"&gt;Stack Overflow question&lt;/a&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Getting Started&lt;/h3&gt;  &lt;p&gt;You can get EF 4.3.1 by installing the latest version of the &lt;a href="http://nuget.org/packages/EntityFramework/"&gt;EntityFramework NuGet package&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/1145.InstallEntityFramework_5F00_120A2301.png"&gt;&lt;img title="InstallEntityFramework" border="0" alt="InstallEntityFramework" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5857.InstallEntityFramework_5F00_thumb_5F00_7FC15C3E.png" width="758" height="83" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Check out the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx"&gt;EF 4.3 release post&lt;/a&gt; for details and walkthroughs for EF 4.3.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Compatibility&lt;/h3&gt;  &lt;p&gt;This version of the NuGet package is fully compatible with Visual Studio 2010 and Visual Studio 11 Beta. It is designed to be used when writing .NET 4.0 applications. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;If writing a .NET 4.5 application we recommend using the 5.0.0-beta1 version of the package instead.&lt;/strong&gt; You can use the package in .NET 4.5 applications but you should be aware of the following known issue:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Entity Framework 4.1 thru 4.3 included additional data annotations in the System.ComponentModel.DataAnnotations namespace in the EntityFramework assembly. In .NET 4.5 these annotations were moved to be part of the .NET Framework in the System.ComponentModel.DataAnnotations.Schema namespace of the System.ComponentModel.DataAnnotations.dll assembly. If you are using EF 4.x and targeting .NET 4.5 this results in two data annotations with the same name in different assemblies. Because the annotations in the .NET Framework are in a different namespace we were not      &lt;br /&gt;able to use type forwarding to avoid this conflict. &lt;/p&gt;    &lt;p&gt;If you are not using the affected data annotations there is no impact on your code. If you are using the data annotations in a C# project you can use the extern modifier to ensure your code uses the annotations from EntityFramework.dll (&lt;a href="http://msdn.microsoft.com/en-us/library/e59b22c5(v=VS.80).aspx)"&gt;http://msdn.microsoft.com/en-us/library/e59b22c5(v=VS.80).aspx)&lt;/a&gt;. If you use the new annotations from the System.ComponentModel.DataAnnotations.dll assembly in .NET 4.5&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;hr /&gt;  &lt;h2&gt;Support&lt;/h2&gt;  &lt;p&gt;We are seeing a lot of great Entity Framework questions (and answers) from the community on &lt;a href="http://stackoverflow.com"&gt;Stack Overflow&lt;/a&gt;. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. &lt;/p&gt;  &lt;p&gt;We would encourage you to &lt;a href="http://stackoverflow.com/questions/ask"&gt;post questions on Stack Overflow&lt;/a&gt; using the entity-framework tag. We will also continue to monitor the Entity Framework forum.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10274108" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-27-41-08/EF5.0.0_2D00_beta1_5F00_License.rtf" length="86884" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Sneak Preview: Entity Framework 5.0 Performance Improvements</title><link>http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx</link><pubDate>Tue, 14 Feb 2012 19:38:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10267860</guid><dc:creator>dpblogs</dc:creator><slash:comments>78</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10267860</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx#comments</comments><description>&lt;p&gt;An O/RM, like any layer of abstraction, introduces overhead to data access. In EF 5.0 we have taken steps to reduce this overhead and improve performance. As a result in one of our tests, repeat execution time of the same LINQ query has been reduced by around 6x. We also have an end-to-end application that we use for performance testing that is running 67% faster.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;LINQ to Entities Query Repeat Execution Time&lt;/h2&gt;  &lt;p&gt;In EF 5.0 we are introducing &lt;a href="http://blogs.msdn.com/b/efdesign/archive/2011/06/30/auto-compiled-linq-queries-entity-framework-june-2011-ctp.aspx"&gt;automatic compilation of LINQ to Entities queries&lt;/a&gt;. &lt;a href="http://thedatafarm.com/blog/data-access/compiled-queries-in-entity-framework/"&gt;From the first version of EF&lt;/a&gt;, developers have been able to compile LINQ queries explicitly by calling &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.compiledquery.compile.aspx"&gt;CompiledQuery.Compile&lt;/a&gt;, but many developers either were not aware of this ability or found the API harder to work with than regular LINQ. In EF 5.0 we took on some of this work for you, so that the translation of inline LINQ queries is now cached without having to use CompiledQuery. This improvement, together with an optimization in how we evaluate query parameter values, has had a profound impact on the query performance of EF:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0363.ExecutionTimeResults_5F00_229770DE.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="ExecutionTimeResults" border="0" alt="ExecutionTimeResults" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/6724.ExecutionTimeResults_5F00_thumb_5F00_347404AB.png" width="629" height="337" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The graph above represents the relative time spent in the execution of a query that retrieves an entity by its key. The same logical query was executed via ADO.NET, LINQ to SQL and several EF methodologies. In the benchmark, the query is executed in a loop using different key values each time. From the graph above we can see that before these performance improvements, executing a LINQ to Entities query for the second time took 23.14 times as long as the same SQL query using classic ADO.NET. With automatic query compilation and the changes to parameter evaluation, we’ve improved the performance of LINQ to Entities queries nearly 600% when compared against EF 4.0. As a bonus, EF 4.0 applications will get this performance improvement for free by just upgrading to .NET 4.5.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;End-to-End Performance&lt;/h2&gt;  &lt;p&gt;It would be amazing if we could give you 600% improved performance across the board. Unfortunately, most applications do more interesting things than run the same query repeatedly. Internally we have a suite of performance tests that run a variety of operations designed to simulate the typical usage a real-world application would have. We set thresholds based on the performance of EF 4.0 to ensure that we do not regress performance. These test runs are showing a 67% performance increase over EF 4.0 since we upgraded the server to EF 5.0:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/4572.clip_5F00_image004_5F00_45F5CB84.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/1033.clip_5F00_image004_5F00_thumb_5F00_57D25F51.jpg" width="628" height="102" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;We Aren’t Done Yet!&lt;/h2&gt;  &lt;p&gt;This means that many real-world applications will automatically show substantially improved performance when they start using EF 5.0. Naturally, how much faster a specific application will become will depend on many variables that would be impossible to predict in a benchmark.&lt;/p&gt;  &lt;p&gt;We aren’t done improving performance yet, but we’re excited about how much faster EF 5.0 is. These numbers are based on an internal build, but we’re working hard to get a build available for public verification of these improvements.&lt;/p&gt;  &lt;p&gt;As always, your feedback is important to us. Feel free to leave comments or concerns below.&lt;/p&gt;  &lt;p&gt;Thanks,   &lt;br /&gt;The Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10267860" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Performance/">Performance</category></item><item><title>EF 4.3 Released</title><link>http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx</link><pubDate>Thu, 09 Feb 2012 19:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10266073</guid><dc:creator>dpblogs</dc:creator><slash:comments>73</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10266073</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx#comments</comments><description>&lt;p&gt;Over the last six months we’ve released a series of previews of our Code First Migrations work. Today we are making the first fully supported go-live release of our migrations work available as part of EF 4.3.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed Between EF 4.2 and EF 4.3&lt;/h2&gt;  &lt;p&gt;The notable changes between EF 4.2 and EF 4.3 include: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;New Code First Migrations Feature. &lt;/strong&gt;This is the primary new feature in EF 4.3 and allows a database created by Code First to be incrementally changed as your Code First model evolves. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Removal of EdmMetadata table.&lt;/strong&gt; If you allow Code First to create a database by simply running your application (i.e. without explicitly enabling Migrations) the creation will now take advantage of improvements to database schema generation we have implemented as part of Migrations. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Bug fix for GetDatabaseValues.&lt;/strong&gt; In earlier releases this method would fail if your entity classes and context were in different namespaces. This issue is now fixed and the classes don’t need to be in the same namespace to use GetDatabaseValues. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Bug fix to support Unicode DbSet names.&lt;/strong&gt; In earlier releases you would get an exception when running a query against a DbSet that contained some Unicode characters. This issue is now fixed. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Data Annotations on non-public properties.&lt;/strong&gt; Code First will not include private, protected, or internal properties by default. Even if you manually included these members in your model, using the Fluent API in previous versions of Code First would ignore any Data Annotations on these members. This is now fixed and Code First will process the Data Annotations once the private, protected, or internal properties are manually included in the model. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;More configuration file settings.&lt;/strong&gt; We’ve enabled more Code First related settings to be specified in the App/Web.config file. This gives you the ability to set the default connection factory and database initializers from the config file. You can also specify constructor arguments to be used when constructing these objects. More details are available in the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx"&gt;EF 4.3 Configuration File Settings&lt;/a&gt; blog post. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;&lt;font color="#ff0000"&gt;Known Issues&lt;/font&gt;&lt;/h2&gt;  &lt;p&gt;We’ll keep this section updated with issues that are reported on EF 4.3. A few weeks after the EF 4.3 release, we will publish an EF 4.3.1 patch release that includes fixes to some of these issues. &lt;/p&gt;  &lt;p&gt;The known issues with the EF 4.3 release include: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Errors using MOQ to mock DbContext.&lt;/b&gt; See &lt;a href="http://stackoverflow.com/questions/9245402/entity-framework-4-3-and-moq-cant-create-dbcontext-mock"&gt;this Stack Overflow question&lt;/a&gt; for more details. We will be including a fix for this issue in the 4.3.1 patch release.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;TPC mapping&lt;/b&gt; generating “Unknown Columnname” exceptions when accessing the database. We will be including a fix for this issue in the 4.3.1 patch release.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Not all hard coded column data types are honored in generated databases. &lt;/b&gt;If you use the Column data annotation or HasColumnType Fluent API method to hard code a column data type (i.e. xml, money, etc.), this data type is not used when creating the database. We will be including a fix for this issue in the 4.3.1 patch release.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed from EF 4.3 Beta 1&lt;/h2&gt;  &lt;p&gt;Aside from some minor bug fixes the changes to Code First Migrations since EF 4.3 Beta 1 include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Enable-Migrations will now scaffold a code-based migration if the database has already been created. &lt;/strong&gt;If you use Code First to create a database and then later enable migrations, a code-based migration will be scaffolded that represents the objects that have already been created in the database. (See the Enabling Migrations section of the &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx"&gt;Code-Based Migrations Walkthrough&lt;/a&gt; for more details).       &lt;br /&gt;You can use the &lt;font color="#c0504d"&gt;–EnableAutomaticMigrations&lt;/font&gt; flag to avoid a code-based migration from being scaffolded, and have the creation of these objects be treated as an automatic migration. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Added $InitialDatabase constant.&lt;/strong&gt; This can be used in place of “0” when specifying a source or target migration. For example, migrating down to an empty database can be performed with the &lt;font color="#c0504d"&gt;Update-Database –TargetMigration:$InitialDatabase&lt;/font&gt; command. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Renamed Update-Database.exe to migrate.exe. &lt;/strong&gt;The command line tool for applying migrations was originally named to be consistent with the Power Shell command. We’ve now changed it to be more consistent with other command line names. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Updated migrate.exe so it can be invoked from the NuGet tools folder.&lt;/strong&gt; In Beta 1 you needed to copy migrate.exe to the same directory as the assembly that contained migrations, you can now invoke it directly from the tools folder. You will need to specify the directory containing your assembly in the /StartUpDirectory parameter. For example:       &lt;br /&gt;&lt;font color="#c0504d"&gt;C:\MyProject\packages\EntityFramework.4.3.0\tools&amp;gt;migrate.exe MyAssembly /StartUpDirectory:C:\MyProject\MyProject\bin\debug &lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Fixed errors when running migrations commands&lt;/strong&gt; &lt;strong&gt;from Package Manager Console&lt;/strong&gt;. A number of folks reported errors when using the migrations commands in certain scenarios. We’ve fixed the underlying bugs causing these issues. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Fixed –Script issues.&lt;/strong&gt; There were a number of bugs in the scripting functionality of EF 4.3 Beta 1 that prevented you generating a script starting from a migration other than an empty database.&amp;#160; These bugs are now fixed. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting Started&lt;/h2&gt;  &lt;p&gt;You can get EF 4.3 by installing the latest version of the &lt;a href="http://nuget.org/packages/EntityFramework/"&gt;EntityFramework NuGet package&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/1145.InstallEntityFramework_5F00_120A2301.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="InstallEntityFramework" border="0" alt="InstallEntityFramework" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5857.InstallEntityFramework_5F00_thumb_5F00_7FC15C3E.png" width="758" height="83" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;These existing walkthroughs provide a good introduction to using the Code First, Model First &amp;amp; Database First workflows available in Entity Framework:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx"&gt;Model &amp;amp; Database First Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are two new walkthroughs that cover the new Code First Migrations feature. One focuses on the no-magic workflow that uses a code-based migration for every change. The other looks at using automatic migrations to avoid having lots of code in your project for simple changes.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx"&gt;Code-Based Migrations Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx"&gt;Automatic Migrations Walkthrough&lt;/a&gt; &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;h2&gt;Upgrading from ‘EF 4.3 Beta 1’&lt;/h2&gt;  &lt;p&gt;If you have EF 4.3 Beta 1 installed you can upgrade to the new package by running &lt;font color="#c0504d"&gt;Update-Package EntityFramework&lt;/font&gt; in Package Manager Console.&lt;/p&gt;  &lt;p&gt;&lt;span style="background-color: #ffff00"&gt;&lt;strong&gt;You may need to close and re-open Visual Studio after upgrading new package&lt;/strong&gt;&lt;/span&gt;, this is required to unload the old migrations commands.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;EF 5.0 (Enum support is coming… finally!)&lt;/h2&gt;  &lt;p&gt;We’ve been working on a number of features that required updates to some assemblies that are still part of the .NET Framework. These features include enums, spatial data types and some significant performance improvements.&lt;/p&gt;  &lt;p&gt;As soon as the next preview of the .NET Framework 4.5 is available we will be shipping EF 5.0 Beta 1, which will include all these new features.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Support&lt;/h2&gt;  &lt;p&gt;This release can be used in a live operating environment subject to the terms in the license terms. The &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads"&gt;ADO.NET Entity Framework Forum&lt;/a&gt; can be used for questions relating to this release.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;ADO.NET Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10266073" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-26-60-73/License.rtf" length="174133" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF 4.3 Code-Based Migrations Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx</link><pubDate>Thu, 09 Feb 2012 19:14:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10266066</guid><dc:creator>dpblogs</dc:creator><slash:comments>64</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10266066</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx#comments</comments><description>&lt;p&gt;We have released the first go-live release of our Code First Migrations work as part of &lt;font style="style"&gt;&lt;/font&gt;&lt;font style="style"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx"&gt;Entity Framework 4.3&lt;/a&gt;&lt;/font&gt;&lt;font style="style"&gt;&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. &lt;strong&gt;&lt;font style="background-color: #ffff00"&gt;We will focus on the code-based workflow for using migrations&lt;/font&gt;&lt;/strong&gt;. In this workflow each change is written out to a code-based migration that can reside in your project. &lt;/p&gt;  &lt;p&gt;There is a &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;separate &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;font style="style"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx"&gt;Automatic Migrations Walkthrough&lt;/a&gt;&lt;/font&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt; that shows how this same set of changes can be applied using a mixture of code-based and automatic migrations.&lt;/p&gt;  &lt;p&gt;This post assumes you have a basic understanding of Code First, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Building an Initial Model &amp;amp; Database&lt;/h2&gt;  &lt;p&gt;Before we start using migrations we need a project and a Code First model to work with. For this walkthrough we are going to use the canonical &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; and &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; model.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new &lt;font color="#c0504d"&gt;MigrationsCodeDemo&lt;/font&gt; Console application.       &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;Add the latest version of the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework &lt;/span&gt;NuGet package to the project.       &lt;ul&gt;       &lt;li&gt;Tools –&amp;gt; Library Package Manager –&amp;gt; Package Manager Console. &lt;/li&gt;        &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework&lt;/span&gt;’ command.           &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Add a &lt;span style="color: #c0504d" color="#c0504d"&gt;Model.cs&lt;/span&gt; file with the code shown below. This code defines a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class that makes up our domain model and a &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; class that is our EF Code First context.       &lt;br /&gt;      &lt;pre class="code"&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Infrastructure;

&lt;span style="color: blue"&gt;namespace &lt;font color="#000000"&gt;MigrationsCodeDemo&lt;/font&gt;&lt;/span&gt;
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: #2b91af"&gt;DbContext
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Blog&lt;/span&gt;&amp;gt; Blogs { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }

    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Now that we have a model it’s time to use it to perform data access. Update the &lt;font color="#c0504d"&gt;Program.cs&lt;/font&gt; file with the code shown below. 

      &lt;br /&gt;&lt;/p&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Text;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo
{
    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Program
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
        {
            &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;db = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext&lt;/span&gt;())
            {
                db.Blogs.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;{ Name = &lt;span style="color: #a31515"&gt;&amp;quot;Another Blog &amp;quot; &lt;/span&gt;});
                db.SaveChanges();

                &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;blog &lt;span style="color: blue"&gt;in &lt;/span&gt;db.Blogs)
                {
                    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(blog.Name);
                }
            }
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Run your application and you will see that a &lt;font color="#c0504d"&gt;MigrationsCodeDemo.BlogContext&lt;/font&gt; database is created on your local SQLEXPRESS instance. 

      &lt;br /&gt;

      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/6165.MigrationsCodeDemoDatabase_5F00_699A94C9.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MigrationsCodeDemoDatabase" border="0" alt="MigrationsCodeDemoDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/7144.MigrationsCodeDemoDatabase_5F00_thumb_5F00_7A9EC2AC.jpg" width="353" height="203" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;pre class="code"&gt;&lt;font size="2" face="Calibri"&gt;&lt;/font&gt; &lt;/pre&gt;

&lt;h2&gt;Enabling Migrations&lt;/h2&gt;

&lt;p&gt;It’s time to make some more changes to our model.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s introduce a &lt;font color="#c0504d"&gt;Url &lt;/font&gt;property to the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Url { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;
}&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;If you were to run the application again you would get an &lt;font color="#c0504d"&gt;InvalidOperationException&lt;/font&gt; because the database no longer matches your model. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;em&gt;”The model backing the 'BlogContext' context has changed since the database was created. Consider using Code First Migrations to update the database (&lt;/em&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=238269"&gt;&lt;em&gt;http://go.microsoft.com/fwlink/?LinkId=238269&lt;/em&gt;&lt;/a&gt;&lt;em&gt;).” 
      &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/em&gt;&lt;/li&gt;

  &lt;li&gt;As the exception suggests, it’s time to start using Code First Migrations. The first step is to enable migrations for our context. 
    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Enable-Migrations&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;This command has added a &lt;span style="color: #c0504d" color="#c0504d"&gt;Migrations&lt;/span&gt; folder to our project. This new folder contains two files: 

    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class.&lt;/strong&gt; This class allows you to configure how Migrations behaves for your context. For this walkthrough we will just use the default configuration. 

        &lt;br /&gt;&lt;em&gt;Because there is just a single Code First context in your project, Enable-Migrations has automatically filled in the context type that this configuration applies to. &lt;/em&gt;&lt;/li&gt;

      &lt;li&gt;&lt;strong&gt;An &lt;font color="#c0504d"&gt;InitialCreate&lt;/font&gt; migration&lt;/strong&gt;. This migration was generated because we already had Code First create a database for us, before we enabled migrations. The code in this scaffolded migration represents the objects that have already been created in the database. In our case that is the &lt;font color="#c0504d"&gt;Blog&lt;/font&gt; table with a &lt;font color="#c0504d"&gt;BlogId&lt;/font&gt; and &lt;font color="#c0504d"&gt;Name&lt;/font&gt; columns. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Your First Migration&lt;/h2&gt;

&lt;p&gt;Code First Migrations has two commands that you are going to become familiar with. &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; will scaffold the next migration based on changes you have made to your model. &lt;/li&gt;

  &lt;li&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; will apply any pending changes to the database. &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;We need to scaffold a migration to take care of the new &lt;font color="#c0504d"&gt;Url&lt;/font&gt; property we have added. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command allows us to give these migrations a name, let’s just call ours &lt;font color="#c0504d"&gt;AddBlogUrl&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration AddBlogUrl&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;In the Migrations folder we now have a new &lt;font color="#c0504d"&gt;AddBlogUrl &lt;/font&gt;migration. The migration filename is pre-fixed with a timestamp to help with ordering. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AddBlogUrl &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Url&amp;quot;&lt;/span&gt;, c =&amp;gt; c.String());
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Url&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;We could now edit or add to this migration but everything looks pretty good. Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to apply this migration to the database. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations has now updated the&lt;span style="color: #c0504d" color="#c0504d"&gt; MigrationsCodeDemo.BlogContext&lt;/span&gt; database to include the &lt;font color="#c0504d"&gt;Url&lt;/font&gt; column in the &lt;font color="#c0504d"&gt;Blogs&lt;/font&gt; table.&amp;#160; &lt;br /&gt;

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0218.MigrationsCodeDemoDatabaseUpdated_5F00_79C65CC2.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MigrationsCodeDemoDatabaseUpdated" border="0" alt="MigrationsCodeDemoDatabaseUpdated" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/2043.MigrationsCodeDemoDatabaseUpdated_5F00_thumb_5F00_605E5988.jpg" width="353" height="221" /&gt;&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Customizing Migrations &lt;/h2&gt;

&lt;p&gt;So far we’ve generated and run a migration without making any changes. Now let’s look at editing the code that gets generated by default. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It’s time to make some more changes to our model, let’s introduce a &lt;font color="#c0504d"&gt;Blog.Rating&lt;/font&gt; property and a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }     &lt;br /&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;    public string &lt;/span&gt;Url { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } 
&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;Rating { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;
&lt;/font&gt;    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;}

&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="background-color: #ffff00"&gt;  &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations scaffold its best guess at the migration for us. We’re going to call this migration &lt;font color="#c0504d"&gt;AddPostClass&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration AddPostClass&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations did a pretty good job of scaffolding these changes, but there are some things we might want to change: 
    &lt;ul&gt;
      &lt;li&gt;First up, let’s add a unique index to &lt;span style="color: #c0504d" color="#c0504d"&gt;Posts.Title&lt;/span&gt; column. &lt;/li&gt;

      &lt;li&gt;We’re also adding a non-nullable&lt;span style="color: #c0504d" color="#c0504d"&gt; Blogs.Rating&lt;/span&gt; column, if there is any existing data in the table it will get assigned the CLR default of the data type for new column (&lt;font color="#c0504d"&gt;Rating&lt;/font&gt; is integer, so that would be 0). But we want to specify a default value of 3 so that existing rows in the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blogs&lt;/span&gt; table will start with a decent rating. 

        &lt;br /&gt;

        &lt;br /&gt;&lt;em&gt;(These changes to the scaffolded migration are highlighted&lt;/em&gt;) 

        &lt;br /&gt;

        &lt;br /&gt;

        &lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;

    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AddPostClass &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            CreateTable(
                &lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;,
                c =&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;new
                    &lt;/span&gt;{
                        PostId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, identity: &lt;span style="color: blue"&gt;true&lt;/span&gt;),
                        Title = c.String(maxLength: 200),
                        Content = c.String(),
                        BlogId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;),
                    })
                .PrimaryKey(t =&amp;gt; t.PostId)
                .ForeignKey(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, t =&amp;gt; t.BlogId, cascadeDelete: &lt;span style="color: blue"&gt;true&lt;/span&gt;)
                .Index(t =&amp;gt; t.BlogId)
                &lt;font style="background-color: #ffff00"&gt;.Index(p =&amp;gt; p.Title, unique: &lt;span style="color: blue"&gt;true&lt;/span&gt;)&lt;/font&gt;;

            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;, c =&amp;gt; c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;, defaultValue: 3&lt;/font&gt;));
        }

        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropIndex(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot; &lt;/span&gt;});
            DropForeignKey(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;);
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;);
            DropTable(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Our edited migration is looking pretty good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. This time let’s specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Verbose &lt;/span&gt;flag so that you can see the SQL that Code First Migrations is running. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;ol&gt;&lt;/ol&gt;

&lt;h2&gt;Data Motion / Custom SQL&lt;/h2&gt;

&lt;p&gt;So far we have looked at migration operations that don’t change or move any data, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a &lt;font color="#c0504d"&gt;Post.Abstract&lt;/font&gt; property to our model. Later, we’re going to pre-populate the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; for existing posts using some text from the start of the &lt;font color="#c0504d"&gt;Content&lt;/font&gt; column. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Abstract { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;     

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
    &lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;/li&gt;

  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations scaffold its best guess at the migration for us. We’re going to call this migration &lt;font color="#c0504d"&gt;AddPostAbstract&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration AddPostAbstract&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;The generated migration takes care of the schema changes but we also want to pre-populate the Abstract column using the first 100 characters of content for each post. We can do this by dropping down to SQL and running an UPDATE statement after the column is added. 
    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AddPostAbstract &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Abstract&amp;quot;&lt;/span&gt;, c =&amp;gt; c.String());

            &lt;font style="background-color: #ffff00"&gt;Sql(&lt;span style="color: #a31515"&gt;&amp;quot;UPDATE Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL&amp;quot;&lt;/span&gt;);&lt;/font&gt;
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Abstract&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Our edited migration looks good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. We’ll specify the &lt;font color="#c0504d"&gt;–Verbose&lt;/font&gt; flag so that we can see the SQL being run against the database.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Migrate to a Specific Version (Including Downgrade)&lt;/h2&gt;

&lt;p&gt;So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s say we want to migrate our database to the state it was in after running our &lt;font color="#c0504d"&gt;AddBlogUrl &lt;/font&gt;migration. We can use the &lt;font color="#c0504d"&gt;–TargetMigration&lt;/font&gt; switch to downgrade to this migration. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;AddBlogUrl&amp;quot;&lt;/font&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This command will run the Down script for our &lt;font color="#c0504d"&gt;AddBlogAbstract&lt;/font&gt; and &lt;font color="#c0504d"&gt;AddPostClass &lt;/font&gt;migrations. &lt;/p&gt;

&lt;p&gt;If you want to roll all the way back to an empty database then you can use the &lt;font color="#c0504d"&gt;Update-Database –TargetMigration:$InitialDatabase&lt;/font&gt; command.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Getting a SQL Script&lt;/h2&gt;

&lt;p&gt;If another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the &lt;font color="#c0504d"&gt;Update-Database&lt;/font&gt; command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s run the &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; command but this time we’ll specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Script&lt;/span&gt; flag so that changes are written to a script rather than applied. We’ll also specify a source and target migration to generate the script for. We want a script to go from an empty database ($InitialDatabase) to the latest version (migration “AddPostAbstract”). 

    &lt;br /&gt;

    &lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;em&gt; If you don’t specify a target migration, Migrations will use the latest migration as the target.&lt;/em&gt; 

    &lt;br /&gt;

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database -Script -SourceMigration:$InitialDatabase -TargetMigration:&amp;quot;AddPostAbstract&amp;quot;&lt;/font&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough you saw how to scaffold, edit and run code-based migrations to upgrade and downgrade your database. You also saw how to get a SQL script to apply migrations to a database.&lt;/p&gt;

&lt;p&gt;Rowan Miller 
  &lt;br /&gt;Program Manager 

  &lt;br /&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10266066" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF 4.3 Automatic Migrations Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx</link><pubDate>Thu, 09 Feb 2012 19:08:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10266064</guid><dc:creator>dpblogs</dc:creator><slash:comments>36</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10266064</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx#comments</comments><description>&lt;p&gt;We have released the first go-live release of our Code First Migrations work as part of &lt;font style="style"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx"&gt;Entity Framework 4.3&lt;/a&gt;&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. &lt;strong&gt;&lt;font style="background-color: #ffff00"&gt;We will focus on the workflow that combines automatic and code-based migrations&lt;/font&gt;&lt;/strong&gt;. In this workflow most changes can be automatically calculated and applied. More complex changes can be written out to code-based migrations that can reside in your project. &lt;/p&gt;  &lt;p&gt;There is a separate &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;font style="style"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx"&gt;Code-Based Migrations Walkthrough&lt;/a&gt;&lt;/font&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt; that shows how this same set of changes can be applied using purely code-based migrations.&lt;/p&gt;  &lt;p&gt;This post assumes you have a basic understanding of Code First, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Building an Initial Model &amp;amp; Database&lt;/h2&gt;  &lt;p&gt;Before we start using migrations we need a project and a Code First model to work with. For this walkthrough we are going to use the canonical &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; and &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; model.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new &lt;font color="#c0504d"&gt;MigrationsAutomaticDemo&lt;/font&gt; Console application.       &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;Add the latest version of the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework &lt;/span&gt;NuGet package to the project.       &lt;ul&gt;       &lt;li&gt;Tools –&amp;gt; Library Package Manager –&amp;gt; Package Manager Console. &lt;/li&gt;        &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework&lt;/span&gt;’ command.           &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Add a &lt;span style="color: #c0504d" color="#c0504d"&gt;Model.cs&lt;/span&gt; file with the code shown below. This code defines a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class that makes up our domain model and a &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; class that is our EF Code First context.       &lt;br /&gt;      &lt;pre class="code"&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Infrastructure;

&lt;span style="color: blue"&gt;namespace &lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;&lt;/span&gt;
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: #2b91af"&gt;DbContext
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Blog&lt;/span&gt;&amp;gt; Blogs { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }

    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Now that we have a model it’s time to use it to perform data access. Update the &lt;font color="#c0504d"&gt;Program.cs&lt;/font&gt; file with the code shown below. 

      &lt;br /&gt;&lt;/p&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Text;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsAutomaticDemo
{
    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Program
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
        {
            &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;db = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext&lt;/span&gt;())
            {
                db.Blogs.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;{ Name = &lt;span style="color: #a31515"&gt;&amp;quot;Another Blog &amp;quot; &lt;/span&gt;});
                db.SaveChanges();

                &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;blog &lt;span style="color: blue"&gt;in &lt;/span&gt;db.Blogs)
                {
                    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(blog.Name);
                }
            }
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Run your application and you will see that a &lt;font color="#c0504d"&gt;MigrationsAutomaticDemo.BlogContext&lt;/font&gt; database is created on your local SQLEXPRESS instance. 

      &lt;br /&gt;

      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/2335.MigrationsAutomaticDemoDatabase_5F00_01D84ECB.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MigrationsAutomaticDemoDatabase" border="0" alt="MigrationsAutomaticDemoDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3324.MigrationsAutomaticDemoDatabase_5F00_thumb_5F00_59C59FAB.jpg" width="325" height="201" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Enabling Migrations&lt;/h2&gt;

&lt;p&gt;It’s time to make some more changes to our model.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s introduce a &lt;font color="#c0504d"&gt;Url &lt;/font&gt;property to the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Url { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;
}&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;If you were to run the application again you would get an &lt;font color="#c0504d"&gt;InvalidOperationException&lt;/font&gt; because the database no longer matches your model. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;em&gt;”The model backing the 'BlogContext' context has changed since the database was created. Consider using Code First Migrations to update the database (&lt;/em&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=238269"&gt;&lt;em&gt;http://go.microsoft.com/fwlink/?LinkId=238269&lt;/em&gt;&lt;/a&gt;&lt;em&gt;).” 
      &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/em&gt;&lt;/li&gt;

  &lt;li&gt;As the exception suggests, it’s time to start using Code First Migrations. The first step is to enable migrations for our context. Because we want to use automatic migrations we’re going to specify the &lt;font color="#c0504d"&gt;–EnableAutomaticMigrations&lt;/font&gt; switch. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Enable-Migrations -EnableAutomaticMigrations&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;This command has added a &lt;span style="color: #c0504d" color="#c0504d"&gt;Migrations&lt;/span&gt; folder to our project. This new folder contains a&lt;strong&gt; &lt;/strong&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class. This class allows you to configure how Migrations behaves for your context. For this walkthrough we will just use the default configuration. 

    &lt;br /&gt;&lt;em&gt;Because there is just a single Code First context in your project, Enable-Migrations has automatically filled in the context type that this configuration applies to. &lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Your First Automatic Migration&lt;/h2&gt;

&lt;p&gt;Code First Migrations has two commands that you are going to become familiar with. &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; will scaffold the next migration based on changes you have made to your model. &lt;/li&gt;

  &lt;li&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; will apply any pending changes to the database. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are going to avoid using &lt;font color="#c0504d"&gt;Add-Migration&lt;/font&gt; (unless we really need to) and focus on letting Code First Migrations automatically calculate and apply the changes.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to get Code First Migrations to push the changes to our model (the new &lt;font color="#c0504d"&gt;Blog.Url&lt;/font&gt; property) to the database. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations has now updated the&lt;span style="color: #c0504d" color="#c0504d"&gt; MigrationsAutomaticDemo.BlogContext&lt;/span&gt; database to include the &lt;font color="#c0504d"&gt;Url&lt;/font&gt; column in the &lt;font color="#c0504d"&gt;Blogs&lt;/font&gt; table. 

    &lt;br /&gt;

    &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5875.MigrationsAutomaticDemoDatabaseUpdated_5F00_58ED39C1.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MigrationsAutomaticDemoDatabaseUpdated" border="0" alt="MigrationsAutomaticDemoDatabaseUpdated" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5140.MigrationsAutomaticDemoDatabaseUpdated_5F00_thumb_5F00_13D46C8B.jpg" width="308" height="221" /&gt;&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Our Second Automatic Migration&lt;/h2&gt;

&lt;p&gt;Let’s make another change and let Code First Migrations automatically push the changes to the database for us.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s introduce a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }    &lt;br /&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;    public string &lt;/span&gt;Url { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt; 
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;}

&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt; &lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Now use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. This time let’s specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Verbose &lt;/span&gt;flag so that you can see the SQL that Code First Migrations is running. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/p&gt;

&lt;h2&gt;Adding a Code Based Migration&lt;/h2&gt;

&lt;p&gt;Now let’s look at something we might want to use a code-based migration for.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a Blog.Rating property. 
    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;span style="color: blue"&gt;    public string &lt;/span&gt;Url { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } 
    &lt;/font&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;Rating { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;We could just run Update-Database to push these changes to the database. However, were adding a non-nullable&lt;span style="color: #c0504d" color="#c0504d"&gt; Blogs.Rating&lt;/span&gt; column, if there is any existing data in the table it will get assigned the CLR default of the data type for new column (&lt;font color="#c0504d"&gt;Rating&lt;/font&gt; is integer, so that would be 0). But we want to specify a default value of 3 so that existing rows in the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blogs&lt;/span&gt; table will start with a decent rating. 

    &lt;br /&gt;

    &lt;br /&gt;Let’s use the Add-Migration command to write this change out to a code-based migration so that we can edit it. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command allows us to give these migrations a name, let’s just call ours &lt;font color="#c0504d"&gt;AddBlogRating&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration AddBlogRating&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;In the Migrations folder we now have a new &lt;font color="#c0504d"&gt;AddBlogRating &lt;/font&gt;migration. The migration filename is pre-fixed with a timestamp to help with ordering. Let’s edit the generated code to specify a default value of 3 for Blog.Rating. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;em&gt;The migration also has a code-behind file that captures some metadata. This metadata will allow Code First Migrations to replicate the automatic migrations we performed before this code-based migration. This is important if another developer wants to run our migrations or when it’s time to deploy our application. 
      &lt;br /&gt;&lt;/em&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AddBlogRating &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;, c =&amp;gt; c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, &lt;font style="background-color: #ffff00"&gt;defaultValue: 3&lt;/font&gt;));
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Our edited migration is looking pretty good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Back to Automatic Migrations&lt;/h2&gt;

&lt;p&gt;We are now free to switch back to automatic migrations for our simpler changes. Code First Migrations will take care of performing the automatic and code-based migrations in the correct order based on the metadata it is storing in the code-behind file for each code-based migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a &lt;font color="#c0504d"&gt;Post.Abstract&lt;/font&gt; property to our model. 

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Abstract { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;     

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to get Code First Migrations to push this change to the database using an automatic migration. &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;ul&gt;
    &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Data Motion / Custom SQL&lt;/h2&gt;

&lt;p&gt;So far we have just looked at migration operations that can leave all the data in place, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script. &lt;/p&gt;

&lt;p&gt;We just added the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column. Let’s pre-populate it for existing posts using text from the &lt;font color="#c0504d"&gt;Content&lt;/font&gt; column.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations add an empty migration for us. We’re going to call this migration &lt;font color="#c0504d"&gt;PopulatePostAbstract&lt;/font&gt;. 

    &lt;br /&gt;(The migration will be empty because there are no pending model changes that haven’t been applied to the database) 

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration PopulatePostAbstract&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Update the migration to run some custom SQL that will populate the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PopulatePostAbstract &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            &lt;font style="background-color: #ffff00"&gt;Sql(&lt;span style="color: #a31515"&gt;&amp;quot;UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL&amp;quot;&lt;/span&gt;);
&lt;/font&gt;        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
    &lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Our edited migration looks good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. We’ll specify the &lt;font color="#c0504d"&gt;–Verbose&lt;/font&gt; flag so that we can see the SQL being run against the database.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Migrate to a Specific Version (Including Downgrade)&lt;/h2&gt;

&lt;p&gt;So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s say we want to migrate our database to the state it was in after running our &lt;font color="#c0504d"&gt;AddBlogRating &lt;/font&gt;migration. We can use the &lt;font color="#c0504d"&gt;–TargetMigration&lt;/font&gt; switch to downgrade to this migration. This is going to cause some columns that were added as part of an automatic migration to be dropped automatically on the way down. Code First Migrations won’t let this happen without you knowing about it, so we need to specify the &lt;font color="#c0504d"&gt;–Force&lt;/font&gt; switch to acknowledge that we are OK with the potential data loss. 

    &lt;br /&gt;

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;AddBlogRating&amp;quot; –Force&lt;/font&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;This command will run the Down script for our &lt;font color="#c0504d"&gt;PopulatePostAbstract&lt;/font&gt; migration, then use the automatic pipeline to revert the addition of the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to roll all the way back to an empty database then you can use the &lt;font color="#c0504d"&gt;Update-Database –TargetMigration:$InitialDatabase –Force&lt;/font&gt; command.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Getting a SQL Script&lt;/h2&gt;

&lt;p&gt;If another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the &lt;font color="#c0504d"&gt;Update-Database&lt;/font&gt; command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s run the &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; command but this time we’ll specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Script&lt;/span&gt; flag so that changes are written to a script rather than applied. We want a script to go from an empty database ($InitialDatabase) to the latest version. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;em&gt; You can also specify a target migration to generate a script to the database state at the end of a code-based migration. If you don’t specify a target migration, Migrations will use the latest version as the target (including any automatic migrations that have been applied since the last code-based migration).&lt;/em&gt; 

    &lt;br /&gt;

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database -Script -SourceMigration:$InitialDatabase&lt;/font&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;ol&gt;&lt;/ol&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough you saw how to use automatic migrations to push model changes to the database. You saw how to scaffold and run code-based migrations when you need more control. You also saw how to upgrade and downgrade your database. Finally we looked at how to get a SQL script to apply migrations to a database.&lt;/p&gt;

&lt;p&gt;Rowan Miller 
  &lt;br /&gt;Program Manager 

  &lt;br /&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10266064" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF 4.3 Beta 1 Released</title><link>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx</link><pubDate>Thu, 12 Jan 2012 15:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10255772</guid><dc:creator>dpblogs</dc:creator><slash:comments>38</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10255772</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx#comments</comments><description>&lt;hr /&gt;    &lt;span style="color: #ff0000; font-size: x-large" color="#ff0000" size="6"&gt;&lt;font size="6"&gt;     &lt;p align="center"&gt;This preview is no longer current.&lt;/p&gt;   &lt;/font&gt;&lt;/span&gt;  &lt;p align="center"&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;A &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx" target="_blank"&gt;go-live release of Entity Framework 4.3&lt;/a&gt; is now available.&lt;/span&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;At the end of November we released Beta 1 of Code First Migrations. At the time we released Code First Migration Beta 1 we also announced that we would be rolling the migrations work into the main EntityFramework NuGet package and releasing it as EF 4.3.&lt;/p&gt;  &lt;p&gt;Today we are making Beta 1 of EF 4.3 available. This release also includes a number of bug fixes for the DbContext API and Code First.&lt;/p&gt;  &lt;p&gt;We are planning for this to be the last pre-release version of migrations and our next release will be the final RTM of EF 4.3.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed&lt;/h2&gt;  &lt;p&gt;This release has been primarily about integrating migrations into the EntityFramework NuGet package, improving quality and cleaning up the API surface ready to RTM. &lt;/p&gt;  &lt;p&gt;Notable changes to Code First Migrations include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;New Enable-Migrations command. &lt;/strong&gt;You now use the Enable-Migrations command to add the Migrations folder and Configuration class to your project. This command will also automatically fill in your context type in the Configuration class (provided you have a single context defined in your project). &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Update-Database.exe command line tool.&lt;/strong&gt; In addition to the power shell commands this release also includes &lt;font color="#c0504d"&gt;Update-Database.exe&lt;/font&gt; which can be used to run the migrations process from a command line. You can find this tool in the ‘packages\EntityFramework.4.3.0-beta1\tools\’ under your solutions directory. The syntax for this command line tool is very similar to the Update-Database power shell command. Run ‘&lt;font color="#c0504d" size="2" face="Courier New"&gt;Update-Database /?&lt;/font&gt;’ from a command prompt for more information on the syntax. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Migrations database initializer.&lt;/strong&gt; This release includes the &lt;font color="#c0504d"&gt;System.Data.Entity.MigrateDatabaseToLatestVersion&lt;/font&gt; database initializer that can be used to automatically upgrade to the latest version when your application launches. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Complete xml documentation.&lt;/strong&gt; This release now includes xml documentation (IntelliSense) for the migrations API surface. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Improved logging.&lt;/strong&gt; If you specify the –Verbose flag when running commands in Package Manager Console we now provide more information to help with debugging. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Other notable changes in EF 4.3 include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Removal of EdmMetadata table.&lt;/strong&gt; If you allow Code First to create a database by simply running your application (without using Migrations) the creation is now performed as an Automatic Migration. You can then enable migrations and continue evolving your database using migrations. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Bug fix for GetDatabaseValues.&lt;/strong&gt; In earlier releases this method would fail if your entity classes and context were in different namespaces. This issue is now fixed and the classes don’t need to be in the same namespace to use GetDatabaseValues. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Bug fix to support Unicode DbSet names.&lt;/strong&gt; In earlier releases you would get an exception when running a query against a DbSet that contained some Unicode characters. This issue is now fixed. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Data Annotations on non-public properties.&lt;/strong&gt; Code First will not include private, protected or internal properties by default. If you manually include them in your model Code First used to ignore any Data Annotations on those members. This is now fixed and Code First will process the Data Annotations. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;More configuration file settings.&lt;/strong&gt; We’ve enabled more Code First related settings to be specified in the App/Web.config file. This gives you the ability to set the default connection factory and database initializers from the config file. You can also specify constructor arguments to be used when constructing these objects. More details are available in the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx"&gt;EF 4.3 Configuration File Settings&lt;/a&gt; blog post. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting Started&lt;/h2&gt;  &lt;p&gt;You can get EF 4.3 Beta 1 by installing the latest pre-release version of the &lt;a href="http://nuget.org/packages/EntityFramework/"&gt;EntityFramework NuGet package&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;font style="background-color: #ffff00"&gt;You will need NuGet 1.6 installed and specify the –IncludePrerelease flag at the Package Manager Console to get this pre-release version. Pre-release packages can only be installed from the Package Manager Console.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5305.InstallPackage_5F00_5C3674DE.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="InstallPackage" border="0" alt="InstallPackage" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/6471.InstallPackage_5F00_thumb_5F00_54AB0571.jpg" width="743" height="71" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;There are two walkthroughs for EF 4.3 Beta 1. One focuses on the no-magic workflow that uses a code-based migration for every change. The other looks at using automatic migrations to avoid having lots of code in you project for simple changes.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-code-based-migrations-walkthrough.aspx"&gt;EF 4.3 Beta 1: Code-Based Migrations Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-automatic-migrations-walkthrough.aspx"&gt;EF 4.3 Beta 1: Automatic Migrations Walkthrough&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;h2&gt;Upgrading From ‘Code First Migrations Beta 1’&lt;/h2&gt;  &lt;p&gt;If you have Code First Migrations Beta 1 installed you will need to uninstall the EntityFramework.Migrations package by running ‘&lt;font color="#c0504d"&gt;Uninstall-Package EntityFramework.Migrations’&lt;/font&gt; in Package Manager Console.&lt;/p&gt;  &lt;p&gt;You can then install EF 4.3 Beta 1 using the ‘&lt;font color="#c0504d"&gt;Install-Package EntityFramework –IncludePrerelease&lt;/font&gt;’ command.&lt;/p&gt;  &lt;p&gt;&lt;span style="background-color: #ffff00"&gt;&lt;strong&gt;You will need to close and re-open Visual Studio after installing the new package&lt;/strong&gt;&lt;/span&gt;, this is required to unload the old migrations commands.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;RTM Timeline&lt;/h2&gt;  &lt;p&gt;We are planning for this to be the last pre-release version of migrations and are still on-track to get a full supported, go-live, release of EF 4.3 published this quarter (first quarter of 2012).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;MSDeploy Provider Update&lt;/h2&gt;  &lt;p&gt;We originally blogged about our plans to deliver an MSDeploy provider that could be used to apply migrations to a remote server. After many long hours iterating on this and working with the MSDeploy team we’ve concluded that we can’t deliver a good MSDeploy story for Migrations at this stage. &lt;/p&gt;  &lt;p&gt;The primary issues arise from us needing to execute code from your application assemblies on the remote server, in order to calculate the SQL to apply to the database. This is a requirement that other MSDeploy providers have not had in the past. We are going to continue working with the MSDeploy team to see if we can deliver something in the future, but unfortunately we won’t be shipping an MSDeploy provider in the immediate future.&lt;/p&gt;  &lt;p&gt;If you are able to connect to the remote database from the machine you are deploying from, then you can use the Update-Database.exe command line tool to perform the upgrade process. You can also use the System.Data.Entity.Migrations.DbMigrator class to write your own code that performs the migration process.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;EF 5.0 (Enum support is coming… finally!)&lt;/h2&gt;  &lt;p&gt;We’ve been working on a number of features that required updates to some assemblies that are still part of the .NET Framework. These features include enums, spatial data types and some serious performance improvements.&lt;/p&gt;  &lt;p&gt;As soon as the next preview of the .NET Framework 4.5 is available we will be shipping EF 5.0 Beta 1, which will include all these new features.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;   &lt;ul&gt;&lt;/ul&gt; Support&lt;/h2&gt;  &lt;p&gt;This is a preview of features that will be available in future releases and is designed to allow you to provide feedback on the design of these features. It is not intended or licensed for use in production. If you need assistance we have an &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads"&gt;Entity Framework Pre-Release Forum&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;ADO.NET Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10255772" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-25-57-72/License.rtf" length="86765" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF 4.3 Beta 1: Code-Based Migrations Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-code-based-migrations-walkthrough.aspx</link><pubDate>Thu, 12 Jan 2012 15:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10255771</guid><dc:creator>dpblogs</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10255771</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-code-based-migrations-walkthrough.aspx#comments</comments><description>&lt;hr /&gt;    &lt;span style="color: #ff0000; font-size: x-large" color="#ff0000" size="6"&gt;&lt;font size="6"&gt;     &lt;p align="center"&gt;This preview is no longer current.&lt;/p&gt;   &lt;/font&gt;&lt;/span&gt;  &lt;p align="center"&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;A &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx" target="_blank"&gt;go-live release of Entity Framework 4.3&lt;/a&gt; is now available.&lt;/span&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;We have released the final preview of the Code First Migrations work as part of &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx"&gt;Entity Framework 4.3 Beta 1&lt;/a&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. &lt;strong&gt;&lt;font style="background-color: #ffff00"&gt;We will focus on the code-based workflow for using migrations&lt;/font&gt;&lt;/strong&gt;. In this workflow each change is written out to a code-based migration that resides in your project. &lt;/p&gt;  &lt;p&gt;There is a &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;separate &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-automatic-migrations-walkthrough.aspx"&gt;EF 4.3 Beta 1: Automatic Migrations Walkthrough&lt;/a&gt; that shows how this same set of changes can be applied using a mixture of code-based and automatic migrations.&lt;/p&gt;  &lt;p&gt;This post assumes you have a basic understanding of Code First, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Building an Initial Model&lt;/h2&gt;  &lt;p&gt;Before we start using migrations we need a project and a Code First model to work with. For this walkthrough we are going to use the canonical &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; and &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; model.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new &lt;font color="#c0504d"&gt;MigrationsCodeDemo&lt;/font&gt; Console application.       &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;Add the latest &lt;strong&gt;&lt;u&gt;prerelease&lt;/u&gt;&lt;/strong&gt; version of the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework &lt;/span&gt;NuGet package to the project.       &lt;ul&gt;       &lt;li&gt;Tools –&amp;gt; Library Package Manager –&amp;gt; Package Manager Console. &lt;/li&gt;        &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework –IncludePrerelease&lt;/span&gt;’ command.           &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Add a &lt;span style="color: #c0504d" color="#c0504d"&gt;Model.cs&lt;/span&gt; class with the code shown below. This code defines a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class that makes up our domain model and a &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; class that is our EF Code First context.       &lt;br /&gt;      &lt;br /&gt;      &lt;pre class="code"&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Infrastructure;

&lt;span style="color: blue"&gt;namespace &lt;font color="#000000"&gt;MigrationsCodeDemo&lt;/font&gt;&lt;/span&gt;
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: #2b91af"&gt;DbContext
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Blog&lt;/span&gt;&amp;gt; Blogs { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }

    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;pre class="code"&gt;&lt;font size="2" face="Calibri"&gt;&lt;/font&gt;&amp;#160;&lt;/pre&gt;

&lt;h2&gt;Enabling Migrations&lt;/h2&gt;

&lt;p&gt;Now that we have a Code First model let’s enable Migrations to work with our context.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Enable-Migrations&lt;/span&gt;’ command in Package Manager Console. 

    &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;

  &lt;li&gt;This command has added a &lt;span style="color: #c0504d" color="#c0504d"&gt;Migrations&lt;/span&gt; folder to our project. At the moment this folder just contains a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class allows you to configure how Migrations behaves for your context. 

    &lt;br /&gt;&lt;em&gt;Because there is just a single Code First context in your project, Enable-Migrations has automatically filled in the context type in the base class and Seed method for you. &lt;/em&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;

    &lt;span style="color: blue"&gt;internal sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Configuration &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DbMigrationsConfiguration&lt;/span&gt;&amp;lt;MigrationsCodeDemo.&lt;span style="color: #2b91af"&gt;BlogContext&lt;/span&gt;&amp;gt;
    {
        &lt;span style="color: blue"&gt;public &lt;/span&gt;Configuration()
        {
            AutomaticMigrationsEnabled = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
        }

        &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;Seed(MigrationsCodeDemo.&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;context)
        {
            &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;//  This method will be called after migrating to the latest version.

            //  You can use the DbSet&amp;lt;T&amp;gt;.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p =&amp;gt; p.FullName,
            //      new Person { FullName = &amp;quot;Andrew Peters&amp;quot; },
            //      new Person { FullName = &amp;quot;Brice Lambson&amp;quot; },
            //      new Person { FullName = &amp;quot;Rowan Miller&amp;quot; }
            //    );
            //
        &lt;/span&gt;}
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;pre class="code"&gt;&amp;#160;&lt;/pre&gt;

&lt;h2&gt;Our First Migration&lt;/h2&gt;

&lt;p&gt;Code First Migrations has two commands that you are going to become familiar with. &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; will scaffold the next migration based on changes you have made to your model. &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; will apply any pending changes to the database.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We haven’t generated any migrations yet so this will be our initial migration that creates the first set of tables (in our case that’s just the &lt;font color="#c0504d"&gt;Blogs&lt;/font&gt; table). We can call the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; command and Code First Migrations will scaffold a migration for us with its best guess at what we should do to bring the database up-to-date with the current model. 

    &lt;br /&gt;

    &lt;br /&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command allows us to give these migrations a name, let’s just call ours &lt;font color="#c0504d"&gt;MyFirstMigration&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration MyFirstMigration&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;In the Migrations folder we now have a new &lt;span style="color: #c0504d" color="#c0504d"&gt;MyFirstMigration&lt;/span&gt; migration. The migration is pre-fixed with a timestamp to help with ordering. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyFirstMigration &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            CreateTable(
                &lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;,
                c =&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;new
                    &lt;/span&gt;{
                        BlogId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, identity: &lt;span style="color: blue"&gt;true&lt;/span&gt;),
                        Name = c.String(),
                    })
                .PrimaryKey(t =&amp;gt; t.BlogId);
            
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropTable(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;We could now edit or add to this migration but everything looks pretty good. Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to apply this migration to the database. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations has now created a&lt;span style="color: #c0504d" color="#c0504d"&gt; MigrationsCodeDemo.BlogContext&lt;/span&gt; database on our local SQL Express instance. We could now write code that uses our &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; to perform data access against this database. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5282.MigrationsCodeDemoDatabase_5F00_359D2C36.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MigrationsCodeDemoDatabase" border="0" alt="MigrationsCodeDemoDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3618.MigrationsCodeDemoDatabase_5F00_thumb_5F00_275EB346.jpg" width="317" height="211" /&gt;&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Customizing Migrations &lt;/h2&gt;

&lt;p&gt;So far we’ve generated and run a migration without making any changes. Now let’s look at editing the code that gets generated by default. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It’s time to make some more changes to our model, let’s introduce a &lt;font color="#c0504d"&gt;Blog.Rating&lt;/font&gt; property and a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }     
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;Rating { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;}

&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="background-color: #ffff00"&gt;  &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations scaffold its best guess at the migration for us. We’re going to call this migration &lt;font color="#c0504d"&gt;MySecondSetOfChanges&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration MySecondSetOfChanges&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations did a pretty good job of scaffolding these changes, but there are some things we might want to change: 
    &lt;ul&gt;
      &lt;li&gt;First up, let’s add a unique index to &lt;span style="color: #c0504d" color="#c0504d"&gt;Posts.Title&lt;/span&gt; column. &lt;/li&gt;

      &lt;li&gt;We’re also adding a non-nullable&lt;span style="color: #c0504d" color="#c0504d"&gt; Blogs.Rating&lt;/span&gt; column, if there is any existing data in the table it will get assigned the CLR default of the data type for new column (&lt;font color="#c0504d"&gt;Rating&lt;/font&gt; is integer, so that would be 0). But we want to specify a default value of 3 so that existing rows in the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blogs&lt;/span&gt; table will start with a decent rating. 

        &lt;br /&gt;

        &lt;br /&gt;&lt;em&gt;(These changes to the scaffolded migration are highlighted&lt;/em&gt;) 

        &lt;br /&gt;

        &lt;br /&gt;

        &lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;

    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MySecondSetOfChanges &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            CreateTable(
                &lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;,
                c =&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;new
                    &lt;/span&gt;{
                        PostId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, identity: &lt;span style="color: blue"&gt;true&lt;/span&gt;),
                        Title = c.String(maxLength: 200),
                        Content = c.String(),
                        BlogId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;),
                    })
                .PrimaryKey(t =&amp;gt; t.PostId)
                .ForeignKey(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, t =&amp;gt; t.BlogId, cascadeDelete: &lt;span style="color: blue"&gt;true&lt;/span&gt;)
                .Index(t =&amp;gt; t.BlogId)
                &lt;font style="background-color: #ffff00"&gt;.Index(p =&amp;gt; p.Title, unique: &lt;span style="color: blue"&gt;true&lt;/span&gt;)&lt;/font&gt;;

            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;, c =&amp;gt; c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;, defaultValue: 3&lt;/font&gt;));
        }

        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropIndex(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot; &lt;/span&gt;});
            DropForeignKey(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;);
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;);
            DropTable(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Our edited migration is looking pretty good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. This time let’s specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Verbose &lt;/span&gt;flag so that you can see the SQL that Code First Migrations is running. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;ol&gt;&lt;/ol&gt;

&lt;h2&gt;Data Motion / Custom SQL&lt;/h2&gt;

&lt;p&gt;So far we have just looked at migration operations that don’t change or move any data, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a &lt;font color="#c0504d"&gt;Post.Abstract&lt;/font&gt; property to our model. Later, we’re going to pre-populate the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; for existing posts using some text from the start of the &lt;font color="#c0504d"&gt;Content&lt;/font&gt; column. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Abstract { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;     

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
    &lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;/li&gt;

  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations scaffold its best guess at the migration for us. We’re going to call this migration &lt;font color="#c0504d"&gt;AddPostAbstract&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration AddPostAbstract&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;The generated migration takes care of the schema changes but we also want to pre-populate the Abstract column using the first 100 characters of content for each post. We can do this by dropping down to SQL and running an UPDATE statement after the column is added. 
    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MigrationsCodeDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AddPostAbstract &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Abstract&amp;quot;&lt;/span&gt;, c =&amp;gt; c.String());

            &lt;font style="background-color: #ffff00"&gt;Sql(&lt;span style="color: #a31515"&gt;&amp;quot;UPDATE Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL&amp;quot;&lt;/span&gt;);&lt;/font&gt;
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Abstract&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Our edited migration looks good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. We’ll specify the &lt;font color="#c0504d"&gt;–Verbose&lt;/font&gt; flag so that we can see the SQL being run against the database.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Migrate to a Specific Version (Including Downgrade)&lt;/h2&gt;

&lt;p&gt;So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s say we want to migrate our database to the state it was in after running our &lt;font color="#c0504d"&gt;MyFirstMigration&lt;/font&gt; migration. We can use the &lt;font color="#c0504d"&gt;–TargetMigration&lt;/font&gt; switch to downgrade to this migration. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;MyFirstMigration&amp;quot;&lt;/font&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This command will run the Down script for our &lt;font color="#c0504d"&gt;AddBlogAbstract&lt;/font&gt; and &lt;font color="#c0504d"&gt;MySecondSetOfChanges&lt;/font&gt; migrations. If you want to roll all the way back to an empty database then you can use the &lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;0&amp;quot;&lt;/font&gt; command.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Getting a SQL Script&lt;/h2&gt;

&lt;p&gt;Now that we have performed a few iterations on our local database let’s look at applying those same changes to another database.&lt;/p&gt;

&lt;p&gt;If another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the &lt;font color="#c0504d"&gt;Update-Database&lt;/font&gt; command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Now let’s run the &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; command but this time we’ll specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Script&lt;/span&gt; flag so that changes are written to a script rather than applied. We’ll also specify a source and target migration to generate the script for. We want a script to go from an empty database (migration “0”) to the latest version (migration “AddPostAbstract”). 

    &lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;em&gt; If you don’t specify a target migration, Migrations will use the latest migration as the target.&lt;/em&gt; 

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database -Script -SourceMigration:&amp;quot;0&amp;quot; -TargetMigration:&amp;quot;AddPostAbstract&amp;quot;&lt;/font&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; There are a number of bugs in the scripting functionality in EF 4.3 Beta1 that prevent you generating a script starting from a migration other than an empty database. These bugs will be fixed in the final RTM.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough you saw how to scaffold, edit and run code-based migrations to upgrade and downgrade your database. You also saw how to get a SQL script to apply migrations to a database.&lt;/p&gt;

&lt;p&gt;Rowan Miller 
  &lt;br /&gt;Program Manager 

  &lt;br /&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10255771" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF 4.3 Beta 1: Automatic Migrations Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-automatic-migrations-walkthrough.aspx</link><pubDate>Thu, 12 Jan 2012 15:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10255769</guid><dc:creator>dpblogs</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10255769</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-automatic-migrations-walkthrough.aspx#comments</comments><description>&lt;hr /&gt;    &lt;span style="color: #ff0000; font-size: x-large" color="#ff0000" size="6"&gt;&lt;font size="6"&gt;     &lt;p align="center"&gt;This preview is no longer current.&lt;/p&gt;   &lt;/font&gt;&lt;/span&gt;  &lt;p align="center"&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;A &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx" target="_blank"&gt;go-live release of Entity Framework 4.3&lt;/a&gt; is now available.&lt;/span&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;We have released the final preview of the Code First Migrations work as part of &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx"&gt;Entity Framework 4.3 Beta 1&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. &lt;strong&gt;&lt;font style="background-color: #ffff00"&gt;We will focus on the workflow that combines automatic and code-based migrations&lt;/font&gt;&lt;/strong&gt;. In this workflow most changes can be automatically calculated and applied. More complex changes are written out to code-based migrations that reside in your project. &lt;/p&gt;  &lt;p&gt;There is a separate &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-code-based-migrations-walkthrough.aspx"&gt;EF 4.3 Beta 1: Code-Based Migrations Walkthrough&lt;/a&gt; that shows how this same set of changes can be applied using purely code-based migrations.&lt;/p&gt;  &lt;p&gt;This post assumes you have a basic understanding of Code First, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Building an Initial Model&lt;/h2&gt;  &lt;p&gt;Before we start using migrations we need a project and a Code First model to work with. For this walkthrough we are going to use the canonical &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; and &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; model.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new &lt;font color="#c0504d"&gt;MigrationsAutomaticDemo&lt;/font&gt; Console application.       &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;Add the latest &lt;strong&gt;&lt;u&gt;prerelease&lt;/u&gt;&lt;/strong&gt; version of the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework &lt;/span&gt;NuGet package to the project.       &lt;ul&gt;       &lt;li&gt;Tools –&amp;gt; Library Package Manager –&amp;gt; Package Manager Console. &lt;/li&gt;        &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework –IncludePrerelease&lt;/span&gt;’ command.           &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Add a &lt;span style="color: #c0504d" color="#c0504d"&gt;Model.cs&lt;/span&gt; class with the code shown below. This code defines a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class that makes up our domain model and a &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; class that is our EF Code First context.       &lt;br /&gt;      &lt;br /&gt;      &lt;pre class="code"&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Infrastructure;

&lt;span style="color: blue"&gt;namespace &lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;&lt;/span&gt;
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: #2b91af"&gt;DbContext
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Blog&lt;/span&gt;&amp;gt; Blogs { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }

    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;pre class="code"&gt;&lt;font size="2" face="Calibri"&gt;&lt;/font&gt; &lt;/pre&gt;

&lt;h2&gt;Enabling Migrations&lt;/h2&gt;

&lt;p&gt;Now that we have a Code First model, let’s enable Migrations to work with our context.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Enable-Migrations&lt;/span&gt;’ command in Package Manager Console. 

    &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;

  &lt;li&gt;This command has added a &lt;span style="color: #c0504d" color="#c0504d"&gt;Migrations&lt;/span&gt; folder to our project. At the moment this folder just contains a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class allows you to configure how migrations behaves for your context. 

    &lt;br /&gt;&lt;em&gt;Because there is just a single Code First context in your project, Enable-Migrations has automatically filled in the context type in the base class and Seed method for you. 
      &lt;br /&gt;

      &lt;br /&gt;&lt;/em&gt;We’ll just edit the Configuration class to enable automatic migrations (highlighted below). 

    &lt;br /&gt;

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;

    &lt;span style="color: blue"&gt;internal sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Configuration &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DbMigrationsConfiguration&lt;/span&gt;&amp;lt;&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.&lt;span style="color: #2b91af"&gt;BlogContext&lt;/span&gt;&amp;gt;
    {
        &lt;span style="color: blue"&gt;public &lt;/span&gt;Configuration()
        {
            &lt;font style="background-color: #ffff00"&gt;AutomaticMigrationsEnabled = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/font&gt;
        }

        &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;Seed(&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;context)
        {
            &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;//  This method will be called after migrating to the latest version.

            //  You can use the DbSet&amp;lt;T&amp;gt;.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p =&amp;gt; p.FullName,
            //      new Person { FullName = &amp;quot;Andrew Peters&amp;quot; },
            //      new Person { FullName = &amp;quot;Brice Lambson&amp;quot; },
            //      new Person { FullName = &amp;quot;Rowan Miller&amp;quot; }
            //    );
            //
        &lt;/span&gt;}
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Our First Automatic Migration&lt;/h2&gt;

&lt;p&gt;Code First Migrations has two commands that you are going to become familiar with. &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; will scaffold a code-based migration based on changes you have made to your model. &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; will apply any pending changes to the database. We are going to avoid using &lt;font color="#c0504d"&gt;Add-Migration&lt;/font&gt; (unless we really need to) and focus on letting Code First Migrations automatically calculate and apply the changes.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to get Code First Migrations to push our model to the database. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations has now created a&lt;span style="color: #c0504d" color="#c0504d"&gt; MigrationsAutomaticDemo.BlogContext&lt;/span&gt; database on our local SQL Express instance. We could now write code that uses our &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; to perform data access against this database.&amp;#160; &lt;br /&gt;

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/7853.MigrationsAutomaticDemoDatabase_5F00_65644298.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MigrationsAutomaticDemoDatabase" border="0" alt="MigrationsAutomaticDemoDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/6371.MigrationsAutomaticDemoDatabase_5F00_thumb_5F00_1E0EECA6.jpg" width="318" height="204" /&gt;&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Our Second Automatic Migration&lt;/h2&gt;

&lt;p&gt;Let’s make another change and let Code First Migrations automatically push the changes to the database for us.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s introduce a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }    &lt;br /&gt; 
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;}

&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt; &lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. This time let’s specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Verbose &lt;/span&gt;flag so that you can see the SQL that Code First Migrations is running. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/p&gt;

&lt;h2&gt;Adding a Code Based Migration&lt;/h2&gt;

&lt;p&gt;Now let’s look at something we might want to use a code-based migration for.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a Blog.Rating property. 
    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;Rating { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/font&gt;
&lt;br /&gt;    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;We could just run Update-Database to push these changes to the database. However, were adding a non-nullable&lt;span style="color: #c0504d" color="#c0504d"&gt; Blogs.Rating&lt;/span&gt; column, if there is any existing data in the table it will get assigned the CLR default of the data type for new column (&lt;font color="#c0504d"&gt;Rating&lt;/font&gt; is integer, so that would be 0). But we want to specify a default value of 3 so that existing rows in the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blogs&lt;/span&gt; table will start with a decent rating. 

    &lt;br /&gt;

    &lt;br /&gt;Let’s use the Add-Migration command to write this change out to a code-based migration so that we can edit it. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command allows us to give these migrations a name, let’s just call ours &lt;font color="#c0504d"&gt;MyFirstCodeMigration&lt;/font&gt;. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration MyFirstCodeMigration&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;In the Migrations folder we now have a new &lt;span style="color: #c0504d" color="#c0504d"&gt;MyFirstCodeMigration&lt;/span&gt; migration. The migration is pre-fixed with a timestamp to help with ordering. Let’s edit the generated code to specify a default value of 3 for Blog.Rating. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;em&gt;The migration also has a code-behind file that captures some metadata. This metadata will allow Code First Migrations to replicate the automatic migrations we performed before this code-based migration. This is important if another developer wants to run our migrations or when it’s time to deploy our application. 
      &lt;br /&gt;&lt;/em&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyFirstCodeMigration &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;, c =&amp;gt; c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, &lt;font style="background-color: #ffff00"&gt;defaultValue: 3&lt;/font&gt;));
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Our edited migration is looking pretty good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Back to Automatic Migrations&lt;/h2&gt;

&lt;p&gt;Fortunately we don’t have to keep using code-based migrations now, we can switch back to automatic migrations for our simpler changes. Code First Migrations will take care of performing the automatic and code-based migrations in the correct order based on the metadata it is storing in the code-behind file for each code-based migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a &lt;font color="#c0504d"&gt;Post.Abstract&lt;/font&gt; property to our model. 

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Abstract { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;     

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to get Code First Migrations to push this change to the database using an automatic migration. &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;ul&gt;
    &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Data Motion / Custom SQL&lt;/h2&gt;

&lt;p&gt;So far we have just looked at migration operations that can leave all the data in place, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script. &lt;/p&gt;

&lt;p&gt;We just added the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column. Let’s pre-populate it for existing posts using text from the &lt;font color="#c0504d"&gt;Content&lt;/font&gt; column.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations add an empty migration for us. We’re going to call this migration &lt;font color="#c0504d"&gt;PopulatePostAbstract&lt;/font&gt;. 

    &lt;br /&gt;(The migration will be empty because there are no pending model changes that haven’t been applied to the database) 

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration PopulatePostAbstract&lt;/span&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Update the migration to run some custom SQL that will populate the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;&lt;font color="#000000"&gt;MigrationsAutomaticDemo&lt;/font&gt;.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PopulatePostAbstract &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            &lt;font style="background-color: #ffff00"&gt;Sql(&lt;span style="color: #a31515"&gt;&amp;quot;UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL&amp;quot;&lt;/span&gt;);
&lt;/font&gt;        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
    &lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Our edited migration looks good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. We’ll specify the &lt;font color="#c0504d"&gt;–Verbose&lt;/font&gt; flag so that we can see the SQL being run against the database.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Migrate to a Specific Version (Including Downgrade)&lt;/h2&gt;

&lt;p&gt;So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s say we want to migrate our database to the state it was in after running our &lt;font color="#c0504d"&gt;MyFirstCodeMigration&lt;/font&gt; migration. We can use the &lt;font color="#c0504d"&gt;–TargetMigration&lt;/font&gt; switch to downgrade to this migration. This is going to cause some columns that were added as part of an automatic migration to be dropped automatically on the way down. Code First Migrations won’t let this happen without you knowing about it, so we need to specify the &lt;font color="#c0504d"&gt;–Force&lt;/font&gt; switch to acknowledge that we are OK with the potential data loss. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;MyFirstCodeMigration&amp;quot; –Force&lt;/font&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This command will run the Down script for our &lt;font color="#c0504d"&gt;PopulatePostAbstract&lt;/font&gt; migration, then use the automatic pipeline to revert the addition of the Abstract column. &lt;/p&gt;

&lt;p&gt;If you want to roll all the way back to an empty database then you can use the &lt;font color="#c0504d"&gt;Update-Database –TagetMigration:&amp;quot;0&amp;quot;&lt;/font&gt; command.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Getting a SQL Script&lt;/h2&gt;

&lt;p&gt;Now that we have performed a few iterations on our local database let’s look at applying those same changes to another database.&lt;/p&gt;

&lt;p&gt;If another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the &lt;font color="#c0504d"&gt;Update-Database&lt;/font&gt; command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Now let’s run the &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; command but this time we’ll specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Script&lt;/span&gt; flag so that changes are written to a script rather than applied. We’ll also specify a source migration to generate the script from. We want a script to go from an empty database (migration “0”) to the latest version. 

    &lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;em&gt; You can also specify a target migration to generate a script to the database state at the end of a code-based migration. If you don’t specify a target migration, Migrations will use the latest version as the target (including any automatic migrations that have been applied since the last code-based migration).&lt;/em&gt; 

    &lt;br /&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database -Script -SourceMigration:&amp;quot;0&amp;quot;&lt;/font&gt;’ command in Package Manager Console. 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; There are a number of bugs in the scripting functionality in EF 4.3 Beta1 that prevent you generating a script starting from a migration other than an empty database. These bugs will be fixed in the final RTM.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;ol&gt;&lt;/ol&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough you saw how to use automatic migrations to push model changes to the database. You saw how to scaffold and run code-based migrations when you need more control. You also saw how to upgrade and downgrade your database. Finally we looked at how to get a SQL script that represents the pending changes to a database.&lt;/p&gt;

&lt;p&gt;Rowan Miller 
  &lt;br /&gt;Program Manager 

  &lt;br /&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10255769" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>EF 4.3 Configuration File Settings</title><link>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx</link><pubDate>Thu, 12 Jan 2012 14:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10256102</guid><dc:creator>dpblogs</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10256102</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx#comments</comments><description>&lt;p&gt;We recently released &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx"&gt;Entity Framework 4.3 Beta 1&lt;/a&gt; which includes the ability to configure more DbContext and Code First related settings from your applications Web.config or App.config file.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;These Settings are Optional&lt;/h2&gt; &lt;p&gt;Code First and the DbContext API follow a ‘convention over configuration’ principle. All the settings discussed in this post have a default behavior, you only need to worry about changing the setting when the default no longer satisfies your requirements.&lt;/p&gt; &lt;p&gt;All of these settings can also be applied using code. The configuration file option allows these settings to be easily changed during deployment without updating your code.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;The Entity Framework Configuration Section&lt;/h2&gt; &lt;p&gt;In earlier versions of Entity Framework you could set the database initializer for a context using the &lt;font color="#c0504d"&gt;appSettings&lt;/font&gt; section of the configuration file. In EF 4.3 we are introducing the custom &lt;font color="#c0504d"&gt;entityFramework&lt;/font&gt; section to handle the new settings. Entity Framework will still recognize database initializers set using the old format, but we recommend moving to the new format where possible.&lt;/p&gt; &lt;p&gt;The &lt;font color="#c0504d"&gt;entityFramework&lt;/font&gt; section was automatically added to the configuration file of your project when you installed the EntityFramework NuGet package.&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml &lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;1.0&lt;/span&gt;" &lt;span style="color: red"&gt;encoding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;utf-8&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configSections&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;!-- &lt;/span&gt;&lt;span style="color: green"&gt;For more information on Entity Framework configuration, &lt;br&gt;visit http://go.microsoft.com/fwlink/?LinkID=237468 &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;--&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;section &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;entityFramework&lt;/span&gt;" &lt;br&gt;      &lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, &lt;br&gt;Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configSections&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/pre&gt;&lt;font color="#000000" face="Calibri"&gt;&lt;/font&gt;&lt;/span&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;h2&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;Connection Strings&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;Setting a connection string for a context uses the same syntax as previous releases of Entity Framework. &lt;/p&gt;
&lt;p&gt;Code First uses normal ADO.NET connection strings. For example:&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;BlogContext&lt;/span&gt;" 
        &lt;span style="color: red"&gt;providerName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.SqlClient&lt;/span&gt;" 
        &lt;span style="color: red"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Server=.\SQLEXPRESS;Database=Blogging;Integrated Security=True;&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx"&gt;This post&lt;/a&gt; provides more details on how Code First determines the database to be used, including connection strings in the configuration file.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The Database First and Model First approaches with an EDMX file use special EF connection strings. For example:&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;BlogContext&lt;/span&gt;" 
        &lt;span style="color: red"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;metadata=res://*/BlogModel.csdl|&lt;br&gt;                                                              res://*/BlogModel.ssdl|&lt;br&gt;                                                              res://*/BlogModel.msl;&lt;br&gt;                                           provider=System.Data.SqlClient;&lt;br&gt;                                           provider connection string=&lt;br&gt;                                               &lt;/span&gt;&lt;span style="color: red"&gt;&amp;amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;data source=.\SQLEXPRESS;&lt;br&gt;                                               initial catalog=Blogging;&lt;br&gt;                                               integrated security=True;&lt;br&gt;                                               multipleactiveresultsets=True;&lt;/span&gt;&lt;span style="color: red"&gt;&amp;amp;quot;&lt;/span&gt;" &lt;br&gt;    &lt;span style="color: red"&gt;providerName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.EntityClient&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;Connection strings go in the standard &lt;font color="#c0504d"&gt;connectionStrings&lt;/font&gt; element and do not require the new &lt;font color="#c0504d"&gt;entityFramework&lt;/font&gt; section.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;Code First Default Connection Factory&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;The new configuration section allows you to specify a default connection factory that Code First should use to locate a database to use for a context. The default connection factory is only used when no connection string has been added to the configuration file for a context.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;To set a connection factory, you specify the assembly qualified type name in the &lt;font color="#c0504d"&gt;deafultConnectionFactory&lt;/font&gt; element. &lt;br&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;An assembly qualified name is the namespace qualified name, followed by a comma, then the assembly that the type resides in. You can optionally also specify the assembly version, culture and public key token.&lt;/em&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;Here is an example of setting your own default connection factory:&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;entityFramework&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;defaultConnectionFactory &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;MyNamespace.MyCustomFactory, MyAssembly&lt;/span&gt;"&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;&lt;br&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;entityFramework&lt;/span&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The above example requires the custom factory to have a parameterless constructor. If needed, you can specify constructor parameters using the &lt;font color="#c0504d"&gt;parameters&lt;/font&gt; element. &lt;/p&gt;
&lt;p&gt;For example, the &lt;font color="#c0504d"&gt;SqlCeConnectionFactory&lt;/font&gt;, that is included in Entity Framework, requires you to supply a provider invariant name to the constructor. The provider invariant name identifies the version of SQL Compact you want to use. The following configuration will cause contexts to use SQL Compact version 4.0 by default.&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;entityFramework&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;defaultConnectionFactory &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameters&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameter &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.SqlServerCe.4.0&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameters&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;defaultConnectionFactory&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;entityFramework&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you don’t set a default connection factory, Code First uses the &lt;font color="#c0504d"&gt;SqlConnectionFactory&lt;/font&gt;, pointing to .\SQLEXPRESS. &lt;font color="#c0504d"&gt;SqlConnectionFactory &lt;/font&gt;also has a constructor that allows you to override parts of the connection string. If you want to use a SQL Server instance other than .\SQLEXPRESS you can use this constructor to set the server.&lt;/p&gt;
&lt;p&gt;The following configuration will cause Code First to use a &lt;a href="http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx"&gt;LocalDB&lt;/a&gt; instance for contexts that don’t have an explicit connection string set.&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;entityFramework&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;defaultConnectionFactory &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameters&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameter &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameters&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;defaultConnectionFactory&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;entityFramework&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;By default, it’s assumed that constructor arguments are of type string. You can use the type attribute to change this. &lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameter &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;2&lt;/span&gt;" &lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Int32&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;/&amp;gt;
&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Database Initializers&lt;/h2&gt;
&lt;p&gt;Database initializers are configured on a per-context basis. They can be set in the configuration file using the &lt;font color="#c0504d"&gt;context&lt;/font&gt; element. This element uses the assembly qualified name to identify the context being configured.&lt;/p&gt;
&lt;p&gt;By default, Code First contexts are configured to use the &lt;font color="#c0504d"&gt;CreateDatabaseIfNotExists&lt;/font&gt; initializer. There is a &lt;font color="#c0504d"&gt;disableDatabaseInitialization&lt;/font&gt; attribute on the context element that can be used to disable database initialization.&lt;/p&gt;
&lt;p&gt;For example, the following configuration disables database initialization for the&lt;font color="#c0504d"&gt; Blogging.BlogContext&lt;/font&gt; context defined in &lt;font color="#c0504d"&gt;MyAssembly.dll&lt;/font&gt;.&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;context &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;" &lt;span style="color: blue"&gt;Blogging.BlogContext, MyAssembly&lt;/span&gt;" &lt;span style="color: red"&gt;disableDatabaseInitialization&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;true&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can use the &lt;font color="#c0504d"&gt;databaseInitializer&lt;/font&gt; element to set a custom initializer.&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;context &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;" &lt;span style="color: blue"&gt;Blogging.BlogContext, MyAssembly&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;databaseInitializer &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Blogging.MyCustomBlogInitializer, MyAssembly&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;context&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Constructor parameters use the same syntax as default connection factories.&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;context &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;" &lt;span style="color: blue"&gt;Blogging.BlogContext, MyAssembly&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;databaseInitializer &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Blogging.MyCustomBlogInitializer, MyAssembly&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameters&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameter &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;MyConstructorParameter&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;parameters&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;databaseInitializer&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;context&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can configure one of the generic database initializers that are included in Entity Framework. The type name should specify your context as the &lt;font color="#c0504d"&gt;TContext&lt;/font&gt; generic. The type attribute uses the .NET Framework format for generic types.&lt;/p&gt;
&lt;p&gt;The following configuration sets the &lt;font color="#c0504d"&gt;DropCreateDatabaseAlways&amp;lt;TContext&amp;gt;&lt;/font&gt; initializer for the &lt;font color="#c0504d"&gt;BlogContext&lt;/font&gt;.&lt;br&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The type name is wrapped in the example but must appear on a single line in your configuration file.&lt;/em&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;context &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;" &lt;span style="color: blue"&gt;Blogging.BlogContext, MyAssembly&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;databaseInitializer &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.Entity.DropCreateDatabaseAlways`1[&lt;br&gt;[Blogging.BlogContext, MyAssembly]], EntityFramework&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;context&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you are using Code First Migrations, you can configure the database to be migrated automatically using the &lt;font color="#c0504d"&gt;MigrateDatabaseToLatestVersion&amp;lt;TContext, TMigrationsConfiguration&amp;gt;&lt;/font&gt; initializer.&lt;br&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The type name is wrapped in the example but must appear on a single line in your configuration file.&lt;/em&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;context &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Blogging.BlogContext, MyAssembly&lt;/span&gt;"&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;databaseInitializer &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Data.Entity.MigrateDatabaseToLatestVersion`2[[Blogging.BlogContext, &lt;br&gt;MyAssembly], [Blogging.Migrations.Configuration, MyAssembly]], EntityFramework&lt;/span&gt;" &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;context&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;contexts&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;In this walkthrough you saw how to use the configuration file to set connection strings, default connection factory and database initializers.&lt;/p&gt;
&lt;p&gt;Rowan Miller &lt;br&gt;Program Manager &lt;br&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10256102" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>SQL Azure Federations and the Entity Framework</title><link>http://blogs.msdn.com/b/adonet/archive/2012/01/10/sql-azure-federations-and-the-entity-framework.aspx</link><pubDate>Tue, 10 Jan 2012 22:20:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10255299</guid><dc:creator>dpblogs</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10255299</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2012/01/10/sql-azure-federations-and-the-entity-framework.aspx#comments</comments><description>&lt;p&gt;The recent SQL Azure Q4 2011 Service Release includes the new feature SQL Azure Federations which enables greater scalability and performance from the database tier of your application through horizontal partitioning. One or more tables within a database are split by row and partitioned across multiple databases (Federation members). This type of horizontal partitioning is often referred to as ‘sharding’. Detailed information on the SQL Azure Federations feature is available &lt;a href="http://social.technet.microsoft.com/wiki/contents/articles/2281.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The current release of Entity Framework can be used to work with SQL Azure Federations, however a federated database cannot be created by the Entity Framework. Our Customer Advisory Team has started a series of blog posts with the goal of providing guidance around common scenarios and issues that arise when using the Entity Framework with SQL Azure Federations. &lt;/p&gt;  &lt;p&gt;The first blog post in this series,&lt;del datetime="2012-01-09T06:08" cite="mailto:Mark%20Stafford"&gt; &lt;/del&gt;&lt;a href="http://windowsazurecat.com/2011/09/sql-azure-federations-entity-framework-code-first/"&gt;SQL Azure Federations with Entity Framework Code-First&lt;/a&gt;&lt;ins datetime="2012-01-09T06:08" cite="mailto:Mark%20Stafford"&gt;,&lt;/ins&gt; is a great getting started guide. It explains the correct procedure to submit the USE FEDERATION statement before sending queries to the database via the Entity Framework (query execution or&lt;del datetime="2012-01-09T06:08" cite="mailto:Mark%20Stafford"&gt; &lt;/del&gt;update operations).&lt;/p&gt;  &lt;p&gt;The next post, &lt;a href="http://windowsazurecat.com/2011/09/understanding-sql-azure-federations-no-mars-support-and-entity-framework"&gt;Understanding SQL Azure Federations No-MARS Support and Entity Framework&lt;/a&gt;&lt;ins datetime="2012-01-09T06:13" cite="mailto:Mark%20Stafford"&gt;,&lt;/ins&gt; explains the impact of the lack of support for MARS on Entity Framework applications. &lt;/p&gt;  &lt;p&gt;We are working with the Customer Advisory Team to continue adding posts to this series. While these blog posts provide concrete scenarios detailed walkthroughs and code samples, here are some general guidelines/considerations:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The Entity Framework based application needs to be aware and manage the access to the different federation members. What this means is that the application would have to explicitly open the store conne&lt;b&gt;c&lt;/b&gt;tion with which the context is associated and issue the “USE FEDERATION” statement to connect to the correct federation member before interacting with the database via the Entity Framework.&lt;/li&gt;    &lt;li&gt;Any needed database transaction would have to be started after the “USE FEDERATION” statement is issued. This is because federated databases do not support the “USE FEDERATION” statement in a transaction. &lt;/li&gt;    &lt;li&gt;Any connection retries would also need to be handled by the application.&lt;/li&gt;    &lt;li&gt;Instances of the context class should not span across federation members. In general, this also means that all changes managed by the context should be associated with a single federation member. This is because at the time SaveChanges is invoked, it would issue the corresponding database data modification operations only to the federation member to which the associated store connection currently points. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In the future, our plan is to provide more integrated support for SQL Azure Federations. We would love to hear your experiences on using the Entity Framework with federated databases as well as your suggestions on how we can improve it.&lt;/p&gt;  &lt;p&gt;ADO.NET Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10255299" width="1" height="1"&gt;</description></item><item><title>Code First Migrations: Beta 1 Released</title><link>http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx</link><pubDate>Tue, 29 Nov 2011 15:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10242155</guid><dc:creator>dpblogs</dc:creator><slash:comments>34</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10242155</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx#comments</comments><description>&lt;hr /&gt;    &lt;span style="color: #ff0000; font-size: x-large" color="#ff0000" size="6"&gt;&lt;font size="6"&gt;     &lt;p align="center"&gt;This preview is no longer current.&lt;/p&gt;   &lt;/font&gt;&lt;/span&gt;  &lt;p align="center"&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;Code First Migrations &lt;/span&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;is included in Entity Framework      &lt;br /&gt;starting with &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx" target="_blank"&gt;Entity Framework 4.3&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;At the end of September we released Alpha 3 of Code First Migrations. Based on Alpha 2 and Alpha 3 feedback your telling us that we are building the right thing… so it’s time to start improving quality and working towards our first go-live release. Today we are making Beta 1 available which is our first step in that direction.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed&lt;/h2&gt;  &lt;p&gt;This release has been primarily about improving quality and cleaning up the API surface ready to RTM. There aren’t any significant changes to the user experience.&lt;/p&gt;  &lt;p&gt;Some more notable changes include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Class and method renames. &lt;/strong&gt;We’ve done a lot of renaming and refactoring since Alpha 3. There are some important notes in the next section about how these changes affect migrations that were generated with Alpha 3. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Migrations will now create indexes on foreign key columns.&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Improved model change detection&lt;/strong&gt;. We fixed a number of bugs in the way we look for changes in your model and scaffold migrations. This includes things such as detecting CascadeDelete changes on relationships. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Upgrading From Alpha 3&lt;/h2&gt;  &lt;p&gt;If you have Alpha 3 installed you can use the ‘&lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Package EntityFramework.Migrations&lt;/span&gt;’ command in Package Manager Console to upgrade to Beta 1. &lt;span style="background-color: #ffff00"&gt;&lt;strong&gt;You will need to close and re-open Visual Studio after updating&lt;/strong&gt;&lt;/span&gt;, this is required to reload the updated command assemblies.&lt;/p&gt;  &lt;p&gt;You will also need to update any existing code to reflect a series of class and method renames:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;Settings&lt;/span&gt; class has been renamed to &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration&lt;/span&gt;. When you update the NuGet package you will get a new Configuration.cs (or Configuration.vb) file added to your project. You will need to remove the old Settings file. If you added any logic for seed data etc. you will need to copy this over to the new Configuration class before removing Settings.       &lt;br /&gt;&lt;em&gt;(This file rename is a result of us changing the base class for this class from DbMigrationContext to DbMigrationsConfiguration)&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;If you have existing migrations that call &lt;span style="color: #c0504d" color="#c0504d"&gt;ChangeColumn&lt;/span&gt; you will need to update them to call &lt;span style="color: #c0504d" color="#c0504d"&gt;AlterColumn &lt;/span&gt;instead. &lt;/li&gt;    &lt;li&gt;There is a designer code file associated with each migration in your project, for migrations generated with Alpha 3 you will need to edit this file. You will need to add a using statement for &lt;span style="color: #c0504d" color="#c0504d"&gt;System.Data.Entity.Migrations.Infrastructure&lt;/span&gt; and change the references to &lt;span style="color: #c0504d" color="#c0504d"&gt;IDbMigrationMetadata&lt;/span&gt; to &lt;span style="color: #c0504d" color="#c0504d"&gt;IMigrationMetadata&lt;/span&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;RTM as EF4.3&lt;/h2&gt;  &lt;p&gt;So far we have been shipping Migrations as a separate EntityFramework.Migrations NuGet package that adds on to the EntityFramework package. As our team has been looking at the grow-up story to Migrations from a Code First database that was created by just running a Code First application it’s becoming clear that Migrations is a very integral part of Code First. We’ve also heard feedback that we need to reduce the number of separate components that make up EF. Because of this we are planning to roll the migrations work into the EntityFramework NuGet package so that you get everything you need for Code First applications in a single package. This will be the EF4.3 release.&lt;/p&gt;  &lt;p&gt;The timeline to our first RTM of Code First Migrations depends on the feedback we get on this release but we are currently aiming to have a go-live release available in early 2012. We’re planning to make a Release Candidate available before we publish the final RTM.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What’s Still Coming&lt;/h2&gt;  &lt;p&gt;Beta 1 only includes the Visual Studio integrated experience for Code First Migrations. We also plan to deliver a command line tool and an MSDeploy provider for running Code First Migrations.&lt;/p&gt;  &lt;p&gt;We are planning to include the command line tool as part of the upcoming RTM.&lt;/p&gt;  &lt;p&gt;We’re working with the MSDeploy team to get some changes into the next release of MSDeploy to support our new provider. Our MSDeploy provider will be available when the next version of MSDeploy is published. This will be after our initial RTM of Code First Migrations.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting Started&lt;/h2&gt;  &lt;p&gt;There are two walkthroughs for Beta 1. One focuses on the no-magic workflow that uses a code-based migration for every change. The other looks at using automatic migrations to avoid having lots of code in you project for simple changes.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;span style="background-color: #ff0000"&gt;&lt;/span&gt;&lt;span style="background-color: #ff0000"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx"&gt;Code First Migrations: Beta 1 ‘No-Magic’ Walkthrough&lt;/a&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt; &lt;span style="background-color: #ff0000"&gt;&lt;/span&gt;&lt;/li&gt;    &lt;li&gt;&lt;span&gt;&lt;span style="background-color: #ff0000"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-with-magic-walkthrough-automatic-migrations.aspx"&gt;Code First Migrations: Beta 1 ‘With-Magic’ Walkthrough (Automatic Migrations)&lt;/a&gt;&lt;/span&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Code First Migrations Beta 1 is available via &lt;a href="http://nuget.org/"&gt;NuGet&lt;/a&gt; as the &lt;a href="http://nuget.org/List/Packages/EntityFramework.Migrations"&gt;EntityFramework.Migrations&lt;/a&gt; package.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;ul&gt;&lt;!--EndFragment--&gt;&lt;/ul&gt;  &lt;h2&gt;Support&lt;/h2&gt;  &lt;p&gt;This is a preview of features that will be available in future releases and is designed to allow you to provide feedback on the design of these features. It is not intended or licensed for use in production. If you need assistance we have an &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads"&gt;Entity Framework Pre-Release Forum&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;ADO.NET Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10242155" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-24-21-55/License.rtf" length="151370" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Code First Migrations: Beta 1 ‘No-Magic’ Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx</link><pubDate>Tue, 29 Nov 2011 15:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10238626</guid><dc:creator>dpblogs</dc:creator><slash:comments>22</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10238626</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx#comments</comments><description>&lt;hr /&gt;    &lt;span style="color: #ff0000; font-size: x-large" color="#ff0000" size="6"&gt;&lt;font size="6"&gt;     &lt;p align="center"&gt;This preview is no longer current.&lt;/p&gt;   &lt;/font&gt;&lt;/span&gt;  &lt;p align="center"&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;Code First Migrations &lt;/span&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;is included in Entity Framework      &lt;br /&gt;starting with &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx" target="_blank"&gt;Entity Framework 4.3&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;We have released the fourth preview of our migrations story for Code First development; &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx"&gt;Code First Migrations Beta 1&lt;/a&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;. This release includes a preview of the developer experience for incrementally evolving a database as your Code First model evolves over time.&lt;/p&gt;  &lt;p&gt;This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. &lt;strong&gt;&lt;font style="background-color: #ffff00"&gt;We will focus on the ‘no-magic’ workflow for using migrations&lt;/font&gt;&lt;/strong&gt;. In this workflow each change is written out to a code-based migration that resides in your project. There is a &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;separate &lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-with-magic-walkthrough-automatic-migrations.aspx"&gt;Code First Migrations: Beta 1 ‘With-Magic’ Walkthrough&lt;/a&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt; that shows how this same set of changes can be applied by making use of automatic migrations.&lt;/p&gt;  &lt;p&gt;This post assumes you have a basic understanding of the Code First functionality that is included in EF 4.2, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Building an Initial Model&lt;/h2&gt;  &lt;p&gt;Before we start using migrations we need a project and a Code First model to work with. For this walkthrough we are going to use the canonical &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; and &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; model.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new ‘Beta1Demo’ Console application      &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;Add the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework &lt;/span&gt;NuGet package to the project       &lt;ul&gt;       &lt;li&gt;Tools –&amp;gt; Library Package Manager –&amp;gt; Package Manager Console &lt;/li&gt;        &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework&lt;/span&gt;’ command           &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Add a &lt;span style="color: #c0504d" color="#c0504d"&gt;Model.cs&lt;/span&gt; class with the code shown below. This code defines a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class that makes up our domain model and a &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; class that is our EF Code First context.       &lt;br /&gt;&lt;em&gt;       &lt;br /&gt;Note that we are removing the &lt;span style="color: #c0504d" color="#c0504d"&gt;IncludeMetadataConvention&lt;/span&gt; to get rid of that &lt;span style="color: #c0504d" color="#c0504d"&gt;EdmMetadata&lt;/span&gt; table that Code First adds to our database. The &lt;span style="color: #c0504d" color="#c0504d"&gt;EdmMetadata&lt;/span&gt; table is used by Code First to check if the current model is compatible with the database, which is redundant now that we have the ability to migrate our schema. It isn’t mandatory to remove this convention when using migrations, but one less magic table in our database is a good thing right!&lt;/em&gt;       &lt;br /&gt;      &lt;br /&gt;      &lt;pre class="code"&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Infrastructure;

&lt;span style="color: blue"&gt;namespace &lt;font color="#000000"&gt;Beta1Demo&lt;/font&gt;&lt;/span&gt;
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: #2b91af"&gt;DbContext
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Blog&lt;/span&gt;&amp;gt; Blogs { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

        &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;OnModelCreating(&lt;span style="color: #2b91af"&gt;DbModelBuilder &lt;/span&gt;modelBuilder)
        {
            modelBuilder.Conventions.Remove&amp;lt;&lt;span style="color: #2b91af"&gt;IncludeMetadataConvention&lt;/span&gt;&amp;gt;();
        }
    }

    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Installing Migrations&lt;/h2&gt;

&lt;p&gt;Now that we have a Code First model let’s get Code First Migrations and configure it to work with our context.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Add the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework.Migrations &lt;/span&gt;NuGet package to the project 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework.Migrations&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework.Migrations&lt;/span&gt; package has added a &lt;span style="color: #c0504d" color="#c0504d"&gt;Migrations&lt;/span&gt; folder to our project. At the moment this folder just contains a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class, this class has also been opened for you to edit. This class allows you to configure how migrations behaves for your context. We’ll just edit the &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class to specify our &lt;span style="color: #0000ff" color="#0000ff"&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt;&lt;/span&gt;. 

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1Demo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;

    &lt;span style="color: blue"&gt;internal sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Configuration &lt;/span&gt;: DbMigrationsConfiguration&amp;lt;&lt;span style="color: #2b91af"&gt;&lt;font style="background-color: #ffff00"&gt;BlogContext&lt;/font&gt;&lt;/span&gt;&amp;gt;
    {
        &lt;span style="color: blue"&gt;public &lt;/span&gt;Configuration()
        {
            AutomaticMigrationsEnabled = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
           
&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;            &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;// Seed data: 
            //   Override the Seed method in this class to add seed data.
            //    - The Seed method will be called after migrating to the latest version.
            //    - You can use the DbContext.AddOrUpdate() helper extension method to avoid creating
            //      duplicate seed data. E.g.
            //
            //          myContext.AddOrUpdate(c =&amp;gt; c.FullName,
            //              new Customer { FullName = &amp;quot;Andrew Peters&amp;quot;, CustomerNumber = 123 },
            //              new Customer { FullName = &amp;quot;Brice Lambson&amp;quot;, CustomerNumber = 456 },
            //              new Customer { FullName = &amp;quot;Rowan Miller&amp;quot;, CustomerNumber = 789 }
            //          );
            //
&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;        &lt;/span&gt;}
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Our First Migration&lt;/h2&gt;

&lt;p&gt;Code First Migrations has two commands that you are going to become familiar with. &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; will scaffold the next migration based on changes you have made to your model. &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; will apply any pending changes to the database.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We haven’t generated any migrations yet so this will be our initial migration that creates the first set of tables (in our case that’s just the Blogs table). We can call the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; command and Code First Migrations will scaffold a migration for us with it’s best guess at what we should do to bring the database up-to-date with the current model. 

    &lt;br /&gt;

    &lt;br /&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command allows us to give these migrations a name, let’s just call ours ‘MyFirstMigration’. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration MyFirstMigration&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;In the Migrations folder we now have a new &lt;span style="color: #c0504d" color="#c0504d"&gt;MyFirstMigration&lt;/span&gt; migration. The migration is pre-fixed with a timestamp to help with ordering. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1Demo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyFirstMigration &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            CreateTable(
                &lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;,
                c =&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;new
                    &lt;/span&gt;{
                        BlogId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, identity: &lt;span style="color: blue"&gt;true&lt;/span&gt;),
                        Name = c.String(),
                    })
                .PrimaryKey(t =&amp;gt; t.BlogId);
            
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropTable(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt; &lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;We could now edit or add to this migration but everything looks pretty good. Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to apply this migration to the database. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations has now created a&lt;span style="color: #c0504d" color="#c0504d"&gt; Beta1Demo.BlogContext&lt;/span&gt; database on our local SQL Express instance. We could now write code that uses our &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; to perform data access against this database.&amp;#160; &lt;br /&gt;

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0825.BlogContextDatabase_5F00_55EE0A74.jpg"&gt;&lt;img style="display: inline" title="BlogContextDatabase" alt="BlogContextDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/8168.BlogContextDatabase_5F00_thumb_5F00_7C4FEDBF.jpg" width="356" height="201" /&gt;&lt;/a&gt;&amp;#160; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Customizing Migrations &lt;/h2&gt;

&lt;p&gt;So far we’ve generated and run a migration without making any changes. Now let’s look at editing the code that gets generated by default. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It’s time to make some more changes to our model, let’s introduce a &lt;font color="#c0504d"&gt;Blog.Rating&lt;/font&gt; property and a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }     
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;Rating { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;}

&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="background-color: #ffff00"&gt;  &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations scaffold it’s best guess at the migration for us. We’re going to call this migration ‘MySecondSetOfChanges’. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration MySecondSetOfChanges&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations did a pretty good job of scaffolding these changes, but there are some things we might want to change: 
    &lt;ul&gt;
      &lt;li&gt;First up, let’s add a unique index to &lt;span style="color: #c0504d" color="#c0504d"&gt;Posts.Title&lt;/span&gt; column. &lt;/li&gt;

      &lt;li&gt;We’re also adding a non-nullable&lt;span style="color: #c0504d" color="#c0504d"&gt; Blogs.Rating&lt;/span&gt; column, if there is any existing data in the table it will get assigned the CLR default of the data type for new column (&lt;font color="#c0504d"&gt;Rating&lt;/font&gt; is integer, so that would be 0). But we want to specify a default value of 3 so that existing rows in the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blogs&lt;/span&gt; table will start with a decent rating. 

        &lt;br /&gt;

        &lt;br /&gt;These changes to the scaffolded migration are highlighted below: 

        &lt;br /&gt;

        &lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1Demo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MySecondSetOfChanges &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            CreateTable(
                &lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;,
                c =&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;new
                    &lt;/span&gt;{
                        PostId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, identity: &lt;span style="color: blue"&gt;true&lt;/span&gt;),
                        Title = c.String(maxLength: 200),
                        Content = c.String(),
                        BlogId = c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;),
                    })
                .PrimaryKey(t =&amp;gt; t.PostId)
                .ForeignKey(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, t =&amp;gt; t.BlogId, cascadeDelete: &lt;span style="color: blue"&gt;true&lt;/span&gt;)&lt;br /&gt;                .Index(t =&amp;gt; t.BlogId)
                &lt;font style="background-color: #ffff00"&gt;.Index(p =&amp;gt; p.Title, unique: &lt;span style="color: blue"&gt;true&lt;/span&gt;)&lt;/font&gt;;
            
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;, c =&amp;gt; c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, &lt;font style="background-color: #ffff00"&gt;defaultValue: 3&lt;/font&gt;));
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropIndex(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;font color="#0000ff"&gt;new&lt;/font&gt;[] { &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot;&lt;/span&gt; }); &lt;br /&gt;            DropForeignKey(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;BlogId&amp;quot;&lt;/span&gt;);
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;);
            DropTable(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Our edited migration is looking pretty good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. This time let’s specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Verbose &lt;/span&gt;flag so that you can see the SQL that Code First Migrations is running. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;ol&gt;&lt;/ol&gt;

&lt;h2&gt;Data Motion / Custom SQL&lt;/h2&gt;

&lt;p&gt;So far we have just looked at migration operations that don’t change or move any data, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a &lt;font color="#c0504d"&gt;Post.Abstract&lt;/font&gt; property to our model. Later, we’re going to pre-populate the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; for existing posts using some text from the start of the &lt;font color="#c0504d"&gt;Content&lt;/font&gt; column. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Abstract { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;     

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
    &lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;/li&gt;

  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations scaffold it’s best guess at the migration for us. We’re going to call this migration ‘AddPostAbstract’. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration AddPostAbstract&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;The generated migration takes care of the schema changes but we also want to pre-populate the Abstract column using the first 100 characters of content for each post. We can do this by dropping down to SQL and running an UPDATE statement after the column is added. 
    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1Demo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AddPostAbstract &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Abstract&amp;quot;&lt;/span&gt;, c =&amp;gt; c.String());
            
            &lt;font style="background-color: #ffff00"&gt;Sql(&lt;span style="color: #a31515"&gt;&amp;quot;UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL&amp;quot;&lt;/span&gt;);&lt;/font&gt;
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Posts&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Abstract&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Our edited migration looks good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. We’ll specify the &lt;font color="#c0504d"&gt;–Verbose&lt;/font&gt; flag so that we can see the SQL being run against the database.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Migrate to a Specific Version (Including Downgrade)&lt;/h2&gt;

&lt;p&gt;So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s say we want to migrate our database to the state it was in after running our ‘MyFirstMigration’ migration. We can use the &lt;font color="#c0504d"&gt;–TargetMigration&lt;/font&gt; switch to downgrade to this migration. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;MyFirstMigration&amp;quot;&lt;/font&gt;’ command in Package Manager Console &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This command will run the Down script for our ‘AddBlogAbstract’ and ‘MySecondSetOfChanges’ migrations. If you want to roll all the way back to an empty database then you can use the &lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;0&amp;quot;&lt;/font&gt; command.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Getting a SQL Script&lt;/h2&gt;

&lt;p&gt;Now that we have performed a few iterations on our local database let’s look at applying those same changes to another database.&lt;/p&gt;

&lt;p&gt;If another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the &lt;font color="#c0504d"&gt;Update-Database&lt;/font&gt; command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We’re just going to create a script to deploy to a second database on the local SQL Express instance. Add an &lt;font color="#c0504d"&gt;App.config&lt;/font&gt; file to your project and include a ‘MySecondDatabase’ connection string. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml &lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;1.0&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;encoding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;utf-8&lt;/span&gt;&amp;quot; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MySecondDatabase&lt;/span&gt;&amp;quot;
         &lt;span style="color: red"&gt;providerName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Data.SqlClient&lt;/span&gt;&amp;quot;
         &lt;span style="color: red"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Server=.\SQLEXPRESS;Database=AnotherDatabase;Trusted_Connection=True;&lt;/span&gt;&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Now let’s run the &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; command but this time we’ll specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–TargetDatabase&lt;/span&gt; flag to use the connection string we just added to the configuration file. We’ll also specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Script &lt;/span&gt;flag so that changes are written to a script rather than applied. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –TargetDatabase:&amp;quot;MySecondDatabase&amp;quot; –Script&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough you saw how to scaffold, edit and run code-based migrations to upgrade and downgrade your database. You also saw how to get a SQL script that represents the pending changes to a database.&lt;/p&gt;

&lt;p&gt;As always, we really want your feedback on what we have so far, so please try it out and let us know what you like and what needs improving.&lt;/p&gt;

&lt;p&gt;Rowan Miller 
  &lt;br /&gt;Program Manager 

  &lt;br /&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10238626" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Code First Migrations: Beta 1 ‘With-Magic’ Walkthrough (Automatic Migrations)</title><link>http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-with-magic-walkthrough-automatic-migrations.aspx</link><pubDate>Tue, 29 Nov 2011 15:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10242718</guid><dc:creator>dpblogs</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10242718</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-with-magic-walkthrough-automatic-migrations.aspx#comments</comments><description>&lt;hr /&gt;    &lt;span style="color: #ff0000; font-size: x-large" color="#ff0000" size="6"&gt;&lt;font size="6"&gt;     &lt;p align="center"&gt;This preview is no longer current.&lt;/p&gt;   &lt;/font&gt;&lt;/span&gt;  &lt;p align="center"&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;Code First Migrations &lt;/span&gt;&lt;span style="color: #ff0000; font-size: large" color="#ff0000" size="5"&gt;is included in Entity Framework      &lt;br /&gt;starting with &lt;a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx" target="_blank"&gt;Entity Framework 4.3&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;We have released the fourth preview of our migrations story for Code First development; &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx"&gt;Code First Migrations Beta 1&lt;/a&gt;. This release includes a preview of the developer experience for incrementally evolving a database as your Code First model evolves over time.&lt;/p&gt;  &lt;p&gt;This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. &lt;strong&gt;&lt;font style="background-color: #ffff00"&gt;We will focus on the workflow that combines automatic and code-based migrations&lt;/font&gt;&lt;/strong&gt;. In this workflow most changes can be automatically calculated and applied. More complex changes are written out to code-based migrations that resides in your project. There is a &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx"&gt;Code First Migrations: Beta 1 ‘No-Magic’ Walkthrough&lt;/a&gt; that shows how this same set of changes can be applied using just code-based migrations without any automatic migrations.&lt;/p&gt;  &lt;p&gt;This post assumes you have a basic understanding of the Code First functionality that was included in EF 4.2, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Building an Initial Model&lt;/h2&gt;  &lt;p&gt;Before we start using migrations we need a project and a Code First model to work with. For this walkthrough we are going to use the canonical &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; and &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; model.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new ‘Beta1AutoDemo’ Console application      &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;Add the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework &lt;/span&gt;NuGet package to the project       &lt;ul&gt;       &lt;li&gt;Tools –&amp;gt; Library Package Manager –&amp;gt; Package Manager Console &lt;/li&gt;        &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework&lt;/span&gt;’ command           &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Add a &lt;span style="color: #c0504d" color="#c0504d"&gt;Model.cs&lt;/span&gt; class with the code shown below. This code defines a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Blog&lt;/span&gt; class that makes up our domain model and a &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; class that is our EF Code First context.       &lt;br /&gt;      &lt;br /&gt;&lt;em&gt;Note that we are removing the &lt;span style="color: #c0504d" color="#c0504d"&gt;IncludeMetadataConvention&lt;/span&gt; to get rid of that &lt;span style="color: #c0504d" color="#c0504d"&gt;EdmMetadata&lt;/span&gt; table that Code First adds to our database. The &lt;span style="color: #c0504d" color="#c0504d"&gt;EdmMetadata&lt;/span&gt; table is used by Code First to check if the current model is compatible with the database, which is redundant now that we have the ability to migrate our schema. It isn’t mandatory to remove this convention when using migrations, but one less magic table in our database is a good thing right!&lt;/em&gt;       &lt;br /&gt;      &lt;pre class="code"&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Infrastructure;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1AutoDemo
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BlogContext &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Calibri"&gt;&lt;span style="color: #2b91af"&gt;DbContext
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Blog&lt;/span&gt;&amp;gt; Blogs { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

        &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;OnModelCreating(&lt;span style="color: #2b91af"&gt;DbModelBuilder &lt;/span&gt;modelBuilder)
        {
            modelBuilder.Conventions.Remove&amp;lt;&lt;span style="color: #2b91af"&gt;IncludeMetadataConvention&lt;/span&gt;&amp;gt;();
        }
    }

    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Installing Migrations&lt;/h2&gt;

&lt;p&gt;Now that we have a Code First model let’s get Code First Migrations and configure it to work with our context.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Add the &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework.Migrations &lt;/span&gt;NuGet package to the project 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Install-Package EntityFramework.Migrations&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;EntityFramework.Migrations&lt;/span&gt; package has added a &lt;span style="color: #c0504d" color="#c0504d"&gt;Migrations&lt;/span&gt; folder to our project. At the moment this folder just contains a single &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class, this class has also been opened for you to edit. This class allows you to configure how migrations behaves for your context. 

    &lt;br /&gt;

    &lt;br /&gt;We’ll just edit the &lt;span style="color: #c0504d" color="#c0504d"&gt;Configuration &lt;/span&gt;class to specify our &lt;span style="color: #0000ff" color="#0000ff"&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; &lt;/span&gt;&lt;span style="color: #000000" color="#000000"&gt;and enable automatic migrations (highlighted below)&lt;/span&gt;. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1AutoDemo.Migrations 

        &lt;br /&gt;{ 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using &lt;/span&gt;System; 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations; 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations.Providers; 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.SqlClient; 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq; 

        &lt;br /&gt;

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;internal sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Configuration &lt;/span&gt;: DbMigrationsConfiguration&amp;lt;&lt;span style="color: #2b91af"&gt;&lt;font style="background-color: #ffff00"&gt;BlogContext&lt;/font&gt;&lt;/span&gt;&amp;gt; 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public &lt;/span&gt;Configuration() 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font style="background-color: #ffff00"&gt;AutomaticMigrationsEnabled = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/font&gt; 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;font style="background-color: #ff0000"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;// Seed data: 
          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160; Override the Seed method in this class to add seed data. 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160; - The Seed method will be called after migrating to the latest version. 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160; - You can use the DbContext.AddOrUpdate() helper extension method to avoid creating 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; duplicate seed data. E.g. 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myContext.AddOrUpdate(c =&amp;gt; c.FullName, 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer { FullName = &amp;quot;Andrew Peters&amp;quot;, CustomerNumber = 123 }, 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer { FullName = &amp;quot;Brice Lambson&amp;quot;, CustomerNumber = 456 }, 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer { FullName = &amp;quot;Rowan Miller&amp;quot;, CustomerNumber = 789 } 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ); 

          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;
          &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;} 

        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } 

        &lt;br /&gt;}&lt;/font&gt;&lt;/font&gt; 

    &lt;br /&gt;&lt;!--EndFragment--&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Our First Automatic Migration&lt;/h2&gt;

&lt;p&gt;Code First Migrations has two commands that you are going to become familiar with. &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration&lt;/span&gt; will scaffold a code-based migration based on changes you have made to your model. &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; will apply any pending changes to the database. We are going to avoid using &lt;font color="#c0504d"&gt;Add-Migration&lt;/font&gt; (unless we really need to) and focus on letting Code First Migrations automatically calculate and apply the changes.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to get Code First Migrations to push this our model to the database. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations has now created a&lt;span style="color: #c0504d" color="#c0504d"&gt; Beta1Demo.BlogContext&lt;/span&gt; database on our local SQL Express instance. We could now write code that uses our &lt;span style="color: #c0504d" color="#c0504d"&gt;BlogContext&lt;/span&gt; to perform data access against this database.&amp;#160; &lt;br /&gt;

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/6014.BlogContextDatabase_5F00_3EBA09FD.jpg"&gt;&lt;img style="display: inline" title="BlogContextDatabase" alt="BlogContextDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/7103.BlogContextDatabase_5F00_thumb_5F00_6E94ABBE.jpg" width="356" height="201" /&gt;&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Our Second Automatic Migration&lt;/h2&gt;

&lt;p&gt;Let’s make another change and let Code First Migrations automatically push the changes to the database for us.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s introduce a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Post&lt;/span&gt; class. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }     
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/font&gt;}

&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. This time let’s specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Verbose &lt;/span&gt;flag so that you can see the SQL that Code First Migrations is running. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Adding a Code Based Migration&lt;/h2&gt;

&lt;p&gt;Now let’s look at something we might want to use a code-based migration for.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a Blog.Rating property. 
    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Blog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;Rating { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Post&lt;/span&gt;&amp;gt; Posts { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;We could just run Update-Database to push these changes to the database. However, were adding a non-nullable&lt;span style="color: #c0504d" color="#c0504d"&gt; Blogs.Rating&lt;/span&gt; column, if there is any existing data in the table it will get assigned the CLR default of the data type for new column (&lt;font color="#c0504d"&gt;Rating&lt;/font&gt; is integer, so that would be 0). But we want to specify a default value of 3 so that existing rows in the &lt;span style="color: #c0504d" color="#c0504d"&gt;Blogs&lt;/span&gt; table will start with a decent rating. Let’s use the Add-Migration command to write this change out to a code-based migration so that we can edit it. 

    &lt;br /&gt;

    &lt;br /&gt;The &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command allows us to give these migrations a name, let’s just call ours ‘MyFirstCodeMigration’. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration MyFirstCodeMigration&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;In the Migrations folder we now have a new &lt;span style="color: #c0504d" color="#c0504d"&gt;MyFirstCodeMigration&lt;/span&gt; migration. The migration is pre-fixed with a timestamp to help with ordering. Let’s edit the generated code to specify a default value of 3 for Blog.Rating. 

    &lt;br /&gt;

    &lt;br /&gt;&lt;em&gt;The migration also has a code-behind file that captures some metadata. This metadata will allow Code First Migrations to replicate the automatic migrations we performed before this code-based migration. This is important if another developer wants to run our migrations or when it’s time to deploy our application. 
      &lt;br /&gt;&lt;/em&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1AutoDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyFirstCodeMigration &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            AddColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;, c =&amp;gt; c.Int(nullable: &lt;span style="color: blue"&gt;false&lt;/span&gt;, &lt;font style="background-color: #ffff00"&gt;defaultValue: 3&lt;/font&gt;));
        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
            DropColumn(&lt;span style="color: #a31515"&gt;&amp;quot;Blogs&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Rating&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Our edited migration is looking pretty good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Back to Automatic Migrations&lt;/h2&gt;

&lt;p&gt;Fortunately we don’t have to keep using code-based migrations now, we can switch back to automatic migrations for our simpler changes. Code First Migrations will take care of performing the automatic and code-based migrations in the correct order based on the metadata it is storing in the code-behind file for each code-based migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s add a Blog.Abstract property to our model. 
    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;public class &lt;/font&gt;&lt;/span&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;Post
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;PostId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    [&lt;span style="color: #2b91af"&gt;MaxLength&lt;/span&gt;(200)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Content { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Abstract { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/font&gt;     

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;BlogId { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Blog &lt;/span&gt;Blog { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database &lt;/span&gt;to get Code First Migrations to push this change to the database. &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;ul&gt;
    &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database&lt;/span&gt;’ command in Package Manager Console &lt;/li&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Data Motion / Custom SQL&lt;/h2&gt;

&lt;p&gt;So far we have just looked at migration operations that can leave all the data in place, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script. &lt;/p&gt;

&lt;p&gt;We just added the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column but it would be handy to pre-populate it for existing posts using some text from the start of the &lt;font color="#c0504d"&gt;Content&lt;/font&gt; column.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s use the &lt;span style="color: #c0504d" color="#c0504d"&gt;Add-Migration &lt;/span&gt;command to let Code First Migrations add an empty migration for us. We’re going to call this migration ‘PopulatePostAbstract’. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Add-Migration PopulatePostAbstract&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Update the migration to run some custom SQL that will populate the &lt;font color="#c0504d"&gt;Abstract&lt;/font&gt; column. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Beta1AutoDemo.Migrations
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations;
    
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PopulatePostAbstract &lt;/span&gt;: &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: #2b91af"&gt;DbMigration
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Up()
        {
            &lt;font style="background-color: #ffff00"&gt;Sql(&lt;span style="color: #a31515"&gt;&amp;quot;UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL&amp;quot;&lt;/span&gt;);
&lt;/font&gt;        }
        
        &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Down()
        {
        }
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
    &lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Our edited migration looks good, so let’s use &lt;span style="color: #c0504d" color="#c0504d"&gt;Up&lt;/span&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;date-Database&lt;/span&gt; to bring the database up-to-date. We’ll specify the &lt;font color="#c0504d"&gt;–Verbose&lt;/font&gt; flag so that we can see the SQL being run against the database.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –Verbose&lt;/span&gt;’ command in Package Manager Console. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Migrate to a Specific Version (Including Downgrade)&lt;/h2&gt;

&lt;p&gt;So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Let’s say we want to migrate our database to the state it was in after running our ‘MyFirstCodeMigration’ migration. We can use the &lt;font color="#c0504d"&gt;–TargetMigration&lt;/font&gt; switch to downgrade to this migration. This is going to cause some columns that were added as part of an automatic migration to be dropped automatically on the way down. Code First Migrations won’t let this happen without you knowing about it, so we need to specify the &lt;font color="#c0504d"&gt;–Force&lt;/font&gt; switch to acknowledge that we are OK with the potential data loss. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;font color="#c0504d"&gt;Update-Database –TargetMigration:&amp;quot;MyFirstCodeMigration&amp;quot; –Force&lt;/font&gt;’ command in Package Manager Console &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This command will run the Down script for our ‘PopulatePostAbstract’ migration, then use the automatic pipeline to revert the addition of the Abstract column. &lt;/p&gt;

&lt;p&gt;If you want to roll all the way back to an empty database then you can use the &lt;font color="#c0504d"&gt;Update-Database –TagetMigration:&amp;quot;0&amp;quot;&lt;/font&gt; command.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Getting a SQL Script&lt;/h2&gt;

&lt;p&gt;Now that we have performed a few iterations on our local database let’s look at applying those same changes to another database.&lt;/p&gt;

&lt;p&gt;If another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the Update-Database command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We’re just going to simulate deploying to a second database on the local SQL Express instance. Add an &lt;font color="#c0504d"&gt;App.config&lt;/font&gt; file to your project and include a ‘MySecondDatabase’ connection string. 

    &lt;br /&gt;

    &lt;pre class="code"&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml &lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;1.0&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;encoding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;utf-8&lt;/span&gt;&amp;quot; &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MySecondDatabase&lt;/span&gt;&amp;quot;
         &lt;span style="color: red"&gt;providerName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Data.SqlClient&lt;/span&gt;&amp;quot;
         &lt;span style="color: red"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Server=.\SQLEXPRESS;Database=AnotherDatabase;Trusted_Connection=True;&lt;/span&gt;&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font size="2" face="Calibri"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Now let’s run the &lt;span style="color: #c0504d" color="#c0504d"&gt;Update-Database&lt;/span&gt; command but this time we’ll specify the &lt;span style="color: #c0504d" color="#c0504d"&gt;–TargetDatabase&lt;/span&gt; flag to use the connection string we just added and the &lt;span style="color: #c0504d" color="#c0504d"&gt;–Script &lt;/span&gt;flag so that changes are written to a script rather than just applied. 

    &lt;ul&gt;
      &lt;li&gt;Run the ‘&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #c0504d" color="#c0504d" face="Courier New"&gt;Update-Database –TargetDatabase:&amp;quot;MySecondDatabase&amp;quot; –Script&lt;/span&gt;’ command in Package Manager Console 

        &lt;br /&gt;&lt;span style="color: #ffffff" color="#ffffff"&gt;.&lt;/span&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save. 
    &lt;br /&gt;Looking at the generated file you will see that Code First Migrations has generated SQL for all the automatic and code-based migrations we performed in the same order that we performed them locally. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough you saw how to use automatic migrations to push model changes to the database. You saw how to scaffold and run code-based migrations when you need more control. You also saw how to upgrade and downgrade your database. Finally we looked at how to get a SQL script that represents the pending changes to a database.&lt;/p&gt;

&lt;p&gt;As always, we really want your feedback on what we have so far, so please try it out and let us know what you like and what needs improving.&lt;/p&gt;

&lt;p&gt;Rowan Miller 
  &lt;br /&gt;Program Manager 

  &lt;br /&gt;ADO.NET Entity Framework&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10242718" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Minimizing Connection Pool errors in SQL Azure</title><link>http://blogs.msdn.com/b/adonet/archive/2011/11/05/minimizing-connection-pool-errors-in-sql-azure.aspx</link><pubDate>Sat, 05 Nov 2011 16:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10234292</guid><dc:creator>dpblogs</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10234292</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/11/05/minimizing-connection-pool-errors-in-sql-azure.aspx#comments</comments><description>&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;One of the most common errors observed when connecting to SQL Azure is &amp;ndash; &lt;/span&gt;&lt;span style="color: #ff0000;"&gt;A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An established connection was aborted by the software in your host machine.)&lt;/span&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;.&amp;nbsp; This issue happens when SqlClient grabs an invalid connection from the pool (connections in the pool become invalid due to throttling in the network or in SQL Azure itself) and returns it to the application.&amp;nbsp; As a consequence, when it tries to effectively use the connection (like executing a command, for example); a &lt;b&gt;SqlException&lt;/b&gt; exception is raised.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Let&amp;rsquo;s consider the code snippet below:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Let&amp;rsquo;s assume the connection string uses default connection pool set to true&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // and the connection pool was already previously created for this same&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // connection string&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (&lt;/span&gt;&lt;span style="color: #33cccc;"&gt;SqlConnection&lt;/span&gt; connection = &lt;span style="color: #0000ff;" color="#0000ff"&gt;new &lt;/span&gt;&lt;span style="color: #33cccc;"&gt;SqlConnection&lt;/span&gt;(&lt;span style="color: #800000;"&gt;"&amp;hellip;"&lt;/span&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // If the connection pool in not empty, &lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // even if the connection returned above is dead, &lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // the SqlConnection.Open command below executes succesfully.&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; connection.Open();&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #33cccc;"&gt;SqlCommand&lt;/span&gt; command = &lt;span style="color: #0000ff;" color="#0000ff"&gt;new &lt;/span&gt;&lt;span style="color: #33cccc;"&gt;SqlCommand&lt;/span&gt;(&lt;span style="color: #800000;"&gt;"select product_name from products"&lt;/span&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; command.Connection = connection;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;command.CommandTimeout = 3;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // The Sqlexception gets thrown here,&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="color: #339966; font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;when it tries to send the command to SQL Server.&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #33cccc;"&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while&lt;/span&gt; (reader.Read())&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;hellip;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;catch&lt;/span&gt; (&lt;span style="color: #33cccc;"&gt;SqlException&lt;/span&gt; ex)&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&lt;span style="color: #33cccc;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageBox&lt;/span&gt;.Show(ex.Message);&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Currently, the &lt;b&gt;Open&lt;/b&gt; method always succeeds, deferring any exception to the command execution itself.&amp;nbsp; To work around this situation, you must add retry-logic every time you connect to SQL Azure from your application.&amp;nbsp; There are plenty of articles and guidance related to this topic and my goal here is not to add one more, but to tell you how to minimize this particular issue.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;On August 9&lt;sup&gt;th&lt;/sup&gt;, 2011, Microsoft released the &lt;/span&gt;Reliability Update 1 for the .NET Framework 4&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt; (found at &lt;/span&gt;&lt;a href="http://support.microsoft.com/kb/2533523"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;http://support.microsoft.com/kb/2533523&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;), which includes a fix to this problem.&amp;nbsp; Basically, it forces SqlClient to check if the connection in the pool is dead before returning it to the application.&amp;nbsp; If the connection is dead, SqlClient simply reconnects before returning it to the application.&amp;nbsp; It&amp;rsquo;s important to note that that this fix does not add any additional roundtrip to the server.&amp;nbsp; Instead, it just checks the socket status in the TCP layer, which is very fast and effective.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Now, it&amp;rsquo;s very important to have in mind that this fix &lt;b&gt;&lt;i&gt;does not&lt;/i&gt;&lt;/b&gt; substitute the need for retry-logic.&amp;nbsp; This is still a recommended practice, especially when connecting to SQL Azure.&amp;nbsp; Our intent is to just minimize the failures in order to improve the overall connectivity experience to SQL Server and SQL Azure.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Luiz Fernando Santos&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;ADO.NET PM&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10234292" width="1" height="1"&gt;</description></item><item><title>EF 4.2 Released</title><link>http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx</link><pubDate>Tue, 01 Nov 2011 15:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10231719</guid><dc:creator>dpblogs</dc:creator><slash:comments>49</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10231719</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx#comments</comments><description>&lt;p&gt;We recently &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/09/ef-releases-amp-versioning-call-for-feedback.aspx"&gt;posted&lt;/a&gt; about our plans to rationalize how we name, distribute and talk about releases. We heard a resounding ‘Yes’ from you so then we &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/11/next-ef-release-plans.aspx"&gt;posted&lt;/a&gt; about our plans for releasing EF 4.2.&lt;/p&gt;  &lt;p&gt;We then shipped a Beta and a Release Candidate of EF 4.2. Today we are making the final release of EF 4.2 available.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;EF 4.2 = Bug Fixes + Semantic Versioning&lt;/h2&gt;  &lt;p&gt;When we released ‘EF 4.1 Update 1’ we introduced a bug that affects third party EF providers using a generic class for their provider factory implementation, things such as WrappingProviderFactory&amp;lt;TProvider&amp;gt;. We missed this during our testing and it was reported by some of our provider writers after we had shipped. If you hit this bug you will get a FileLoadException stating “&lt;em&gt;&lt;span style="color: #c0504d" color="#c0504d"&gt;The given assembly name or codebase was invalid&lt;/span&gt;&lt;/em&gt;”. This bug is blocking some third party providers from working with ‘EF 4.1 Update 1’ and the only workaround for folks using an affected provider is to ask them to remain on EF 4.1. Third party provider writers then identified some areas in EF where it was hard to get EF to work with their providers, so we decided to address these issues in the EF 4.2 release. These provider related changes will be the only changes between ‘EF 4.1 Update 1’ and ‘EF 4.2’.&lt;/p&gt;  &lt;p&gt;Obviously a single bug fix wouldn’t normally warrant bumping the minor version, but we also wanted to take the opportunity to get onto the semantic versioning path rather than calling the release ‘EF 4.1 Update 2’.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting Started&lt;/h2&gt;  &lt;p&gt;The following walkthroughs are available for EF 4.2:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;span style="background-color: #ffffff"&gt;&lt;/span&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx"&gt;Model First / Database First Walkthrough&lt;/a&gt;&lt;span style="background-color: #ffffff"&gt;&lt;/span&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Getting EF 4.2&lt;/h2&gt;  &lt;p&gt;EF 4.2 is available via &lt;a href="http://nuget.org"&gt;NuGet&lt;/a&gt; as the &lt;a href="http://nuget.org/List/Packages/EntityFramework"&gt;EntityFramework&lt;/a&gt; package. If you already have the EntityFramework package installed then updating to the latest version will give you EF 4.2.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/2604.NuGetInstallCommand_5F00_7E548EC5.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="NuGetInstallCommand" border="0" alt="NuGetInstallCommand" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/8055.NuGetInstallCommand_5F00_thumb_5F00_571A4590.png" width="766" height="90" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Code First Migrations&lt;/h2&gt;  &lt;p&gt;To use Code First Migrations with EF 4.2 you will need to upgrade to the latest version of the &lt;a href="http://nuget.org/List/Packages/EntityFramework.Migrations"&gt;EntityFramework.Migrations&lt;/a&gt; NuGet package.&lt;/p&gt;  &lt;p&gt;Failure to update to the latest version of Code First Migrations will result in an error stating “&lt;span style="color: #c0504d" color="#c0504d"&gt;&lt;em&gt;Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.&lt;/em&gt;&lt;/span&gt;“&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000"&gt;After updating to the latest Code First Migrations package you will need to close and re-open Visual Studio.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Model First &amp;amp; Database First Templates&lt;/h2&gt;  &lt;p&gt;The templates for using the DbContext API with Model First and Database First are now available under the “Online Templates” tab when “Right-Click –&amp;gt; Add Code Generation Item…” is selected on the EF Designer.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3223.AddDbContextTemplate_5F00_05892DA8.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="AddDbContextTemplate" border="0" alt="AddDbContextTemplate" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0083.AddDbContextTemplate_5F00_thumb_5F00_495D61FF.png" width="652" height="387" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Support&lt;/h2&gt;  &lt;p&gt;This release can be used in a live operating environment subject to the terms in the license terms. The &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads"&gt;ADO.NET Entity Framework Forum&lt;/a&gt; can be used for questions relating to this release.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What’s Not in This Release?&lt;/h2&gt;  &lt;p&gt;As covered earlier this release is just a small update to the DbContext &amp;amp; Code First runtime. The features that were included in &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx"&gt;EF June 2011 CTP&lt;/a&gt; require changes to the Core Entity Framework Libraries that are part of the .NET Framework and will ship at a later date.&lt;/p&gt;  &lt;p&gt;Our Code First Migrations work is continuing and we are working to get the next release in your hands soon.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;ADO.NET Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10231719" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-23-17-19/License.rtf" length="172480" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>How We Talk about EF and its Future Versions</title><link>http://blogs.msdn.com/b/adonet/archive/2011/10/18/how-we-talk-about-ef-and-its-future-versions.aspx</link><pubDate>Tue, 18 Oct 2011 22:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10227201</guid><dc:creator>dpblogs</dc:creator><slash:comments>29</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10227201</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/10/18/how-we-talk-about-ef-and-its-future-versions.aspx#comments</comments><description>&lt;p&gt;The EF team has been working to become more agile and get new features into your hands faster. Our first two releases shipped as part of the .NET Framework but, as the result of an effort to release more often, in recent times we have been shipping a slew of &amp;ldquo;out-of-band&amp;rdquo; features that build on top of the functionality included in .NET.&lt;/p&gt;
&lt;div class="WordSection1"&gt;
&lt;p&gt;These out-of-band releases caused some confusion because we didn&amp;rsquo;t have a consistent way of talking about versions. To avoid generating more of this confusion in the future we &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/09/ef-releases-amp-versioning-call-for-feedback.aspx"&gt;recently decided&lt;/a&gt; to adopt &lt;a href="http://semver.org/"&gt;semantic versioning&lt;/a&gt; principles.&lt;/p&gt;
&lt;p&gt;While consistent versioning is great we still have quite a few EF related packages that ship independently of each other. We need to rationalize how we talk about them and define what we are actually referring to when we talk about &amp;ldquo;Entity Framework&amp;rdquo; and future versions of it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The purpose of this post is to provide additional clarity about how we plan to talk about our releases in the future and to get your feedback on the approach we are taking.&lt;/p&gt;
&lt;h2&gt;The Confusion&lt;/h2&gt;
&lt;p&gt;In order to deliver features as quickly as possible, we want to keep as much as possible outside of the .NET Framework. With EF continuing to have features that ship both inside and outside the .NET Framework we need to rationalize how we talk about these.&lt;/p&gt;
&lt;p&gt;All the EF related libraries, packages and downloads we have today are:&lt;/p&gt;
&lt;ul type="disc"&gt;
&lt;li class="MsoNormal"&gt;&lt;strong&gt;Core EF libraries&lt;/strong&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt; that ship in the .NET Framework (i.e. System.Data.Entity.dll, System.Data.Entity.Design.dll, System.Web.Entity.dll, etc.) &lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal"&gt;&lt;strong&gt;EntityFramework NuGet package&lt;/strong&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt; which includes the DbContext API and Code First (i.e. EntityFramework.dll) &lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal"&gt;&lt;strong&gt;Entity Designer&lt;/strong&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt; that ships in Visual Studio &lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal"&gt;&lt;strong&gt;T4 Templates for using DbContext API&lt;/strong&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt; with Model First &amp;amp; Database First that ship on Visual Studio Gallery &lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal"&gt;&lt;strong&gt;EF Power Tools&lt;/strong&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt; (still in Alpha stage) that ship on Visual Studio Gallery &lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal"&gt;&lt;strong&gt;Code First Migrations &lt;/strong&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt;(still in Alpha stage) which ships via NuGet &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The Clarity&lt;/h2&gt;
&lt;p&gt;Obviously it&amp;rsquo;s going to get very confusing if we release different versions of those at different times. &lt;b&gt;To avoid this we are going to focus on the EntityFramework NuGet package as the primary &lt;span style="color: black;"&gt;deliverable &lt;/span&gt;that we refer to as &amp;ldquo;The Entity Framework&amp;rdquo; or &amp;ldquo;EF&amp;rdquo;. &lt;/b&gt;The version number of this package will now define the current version of EF (currently EF 4.1 and soon to be EF 4.2). This is the main point that will govern how we talk about EF, all of our blog posts, how-to videos, whitepapers etc. will guide you toward using the EntityFramework NuGet package.&lt;/p&gt;
&lt;p&gt;We will generally treat the core Entity Framework libraries in the .NET Framework as we do any other .NET libraries, such as System.dll, System.Data.dll etc. Therefore when the next release of the .NET Framework comes out we will release an updated version of EF that takes advantage of the new features in the Framework (including new functionality in System.Data.Entity.dll).&amp;nbsp; In situations when we need to refer explicitly to the EF specific assemblies in the .NET Framework we will call them the &lt;b&gt;EF Core Libraries&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;b&gt;Entity Designer&lt;/b&gt; will continue to be installed with Visual Studio and will work with the latest version of EF.&lt;/p&gt;
&lt;p&gt;In the future we hope to move the classes from System.Data.Entity.dll into the EntityFramework NuGet &lt;span style="color: black;"&gt;package so that we can release features such as Enums without having to wait for a .NET Framework update.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: black;"&gt;The &lt;b&gt;T4 Templates for DbContext&lt;/b&gt; will be available under the &amp;ldquo;Online&amp;rdquo; tab when you select &amp;ldquo;Add New Code Generation Item&amp;hellip;&amp;rdquo; from the EF designer. The templates are located on the Visual Studio Gallery but you don&amp;rsquo;t need to be aware of this or visit the gallery to use them. We are leaving the templates on VS Gallery so that we can quickly and easily respond to bug fixes, feature requests etc.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: black;"&gt;The &lt;b&gt;EF Power Tools&lt;/b&gt; are still in preview mode and we haven&amp;rsquo;t really decided where they belong yet. For the moment they will remain as a stand-alone download on Visual Studio Gallery as this is the easiest place to distribute them.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="color: black;"&gt;Code First Migrations&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black;"&gt; will remain as a separate NuGet package and when Code First detects that you need to migrate the database schema it will direct you to this NuGet package. Keeping Code First Migrations as a separate package allows us &lt;/span&gt;to quickly iterate on this new feature.&lt;/p&gt;
&lt;h2&gt;Some Features Waiting on .NET Framework 4.5&lt;/h2&gt;
&lt;p&gt;Since we shipped EF 4.0 as part of .NET 4.0, we started talking about upcoming features as &amp;ldquo;EF vNext&amp;rdquo; features. When we then released EF 4.1 and more recently published the EF 4.2 previews, many of you logically asked why those features weren&amp;rsquo;t included.&lt;/p&gt;
&lt;p&gt;While the out-of-band approach has worked well for the DbContext API, Code First and Code First Migrations, the fact is that there are some other features, such as enum type support, that inherently require us to update the core libraries that we previously shipped as part of&amp;nbsp; .NET.&lt;/p&gt;
&lt;p&gt;The obvious solution to this is to ship the entire Entity Framework independent of the .NET Framework. The &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx"&gt;June 2011 CTP&lt;/a&gt; was our first attempt at doing this. While we are still pursuing this option it has become clear that from a technical standpoint we are not ready to achieve this immediately. &lt;b&gt;Because of this our new features that require updates to our core libraries will need to wait for the next .NET Framework release. This includes support for Enum Types, Spatial Types, Table-Valued Functions&lt;span style="color: #1f497d;"&gt;, &lt;/span&gt;Stored &lt;span style="color: black;"&gt;Procedures with Multiple Results and Auto-Compiled LINQ Queries.&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: black;"&gt;These features will reappear in a preview of Entity Framework that we will ship alongside the next public preview of .NET 4.5.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;In summary, when we talk about &amp;ldquo;Entity Framework&amp;rdquo; we are now going to be talking mainly about the out-of-band features that we ship as the EntityFramework NuGet package. Entity Framework is built on the .NET Framework and will make use of the EF core libraries that ship with the .NET Framework, including System.Data.Entity. The EF specific tooling that ships with Visual Studio will always work with the latest version of EF.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Times New Roman','serif';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10227201" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET+vNext/">ADO.NET vNext</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/EDM/">EDM</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework+Futures/">Entity Framework Futures</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/DbContext+API/">DbContext API</category></item><item><title>Announcing Microsoft SQL Server ODBC Driver for Linux</title><link>http://blogs.msdn.com/b/adonet/archive/2011/10/13/announcing-microsoft-sql-server-odbc-driver-for-linux.aspx</link><pubDate>Thu, 13 Oct 2011 17:34:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10224703</guid><dc:creator>dpblogs</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10224703</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/10/13/announcing-microsoft-sql-server-odbc-driver-for-linux.aspx#comments</comments><description>&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;Greetings&lt;br /&gt;Developer community: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;We&lt;br /&gt;heard yesterday and today at the PASS conference about the exciting new areas that we are investing in bringing the power of SQL Server to our customers. Many of our developers who rely on native connectivity to SQL Server primarily use ODBC for their connectivity needs. We have been supporting ODBC as a part of the SQL Native Access Client (SNAC) libraries. In our continued commitment to interoperability, today we also announced that we will be releasing the Microsoft SQL Server ODBC Driver for Linux. We will be releasing first community technology preview (CTP) around mid-November and will be available along with SQL Server 2012 when it is released. Please look for announcement on our &lt;a href="http://msdn.microsoft.com/en-us/sqlserver/connectivity"&gt;SQL Connectivity&lt;/a&gt; home page and &lt;a href="http://blogs.technet.com/b/dataplatforminsider/"&gt;SQL Server&lt;/a&gt; blog page.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;We will be showcasing Microsoft SQL Server ODBC Driver for Linux along with our Java and PHP solutions for SQL Server and Azure at PASS conference session &lt;b&gt;&amp;ldquo;[AD-211-M] Developing Multi-Platform Applications for Microsoft SQL Server and Azure&amp;rdquo; &lt;/b&gt;on &lt;b&gt;Thursday October 13th at 5:00PM at Washington State Convention Center Room #4C4&lt;/b&gt;. Also, if you have any questions or feedback on our multi-platform strategy as well as the entire gamut of support we provide to the application developers, I would encourage you to attend the PASS Panel&amp;nbsp;Discussion with SQL Connectivity Leadership &lt;b&gt;&amp;ldquo;[AD-101-M] SQL Connectivity Leadership Unplugged&amp;rdquo;&lt;/b&gt; on &lt;b&gt;Friday, October 14, 2011, 2:30 PM - 3:45 PM at Washington State Convention Centre Room# 612&lt;/b&gt; where I will be hosting a panel along with the rest of the leadership team that drives the strategy for our application platform.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;Thanks,&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;Raghu Ram&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;PrincipalGroup Program Manager&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="background: white; line-height: 13.5pt;"&gt;&lt;span style="color: #333333; font-family: 'Segoe UI','sans-serif'; font-size: 9pt; mso-ansi-language: EN;" lang="EN"&gt;SQL Server&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10224703" width="1" height="1"&gt;</description></item><item><title>EF 4.2 Release Candidate Available</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-release-candidate-available.aspx</link><pubDate>Wed, 28 Sep 2011 15:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10215185</guid><dc:creator>dpblogs</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10215185</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-release-candidate-available.aspx#comments</comments><description>&lt;p&gt;We recently &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/09/ef-releases-amp-versioning-call-for-feedback.aspx"&gt;posted&lt;/a&gt; about our plans to rationalize how we name, distribute and talk about releases. We heard a resounding &amp;lsquo;Yes&amp;rsquo; from you so then we &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/11/next-ef-release-plans.aspx"&gt;posted&lt;/a&gt; about our plans for releasing EF 4.2. We then shipped EF 4.2 Beta 1.&lt;/p&gt;
&lt;p&gt;Third party EF provider writers tried out EF 4.2 Beta 1 and identified a couple more areas that were causing issues for them. We have been working to improve these areas and today we are making EF 4.2 Release Candidate available. The final release of EF 4.2 will be available in the near future.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;EF 4.2 = Bug Fixes + Semantic Versioning&lt;/h2&gt;
&lt;p&gt;When we released &amp;lsquo;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/07/25/ef-4-1-update-1-released.aspx"&gt;EF 4.1 Update 1&lt;/a&gt;&amp;rsquo; we introduced a bug that affects third party EF providers using a generic class for their provider factory implementation, things such as WrappingProviderFactory&amp;lt;TProvider&amp;gt;. We missed this during our testing and it was reported by some of our provider writers after we had shipped. If you hit this bug you will get a FileLoadException stating &amp;ldquo;&lt;em&gt;The given assembly name or codebase was invalid&lt;/em&gt;&amp;rdquo;. This bug is blocking some third party providers from working with &amp;lsquo;EF 4.1 Update 1&amp;rsquo; and the only workaround for folks using an affected provider is to ask them to remain on EF 4.1. Third party provider writers then identified some areas in EF where it was hard to get EF to work with their providers, so we decided to address these issues in the EF 4.2 release. These provider related changes will be the only changes between &amp;lsquo;EF 4.1 Update 1&amp;rsquo; and &amp;lsquo;EF 4.2&amp;rsquo;.&lt;/p&gt;
&lt;p&gt;Obviously a single bug fix wouldn&amp;rsquo;t normally warrant bumping the minor version, but we also wanted to take the opportunity to get onto the semantic versioning path rather than calling the release &amp;lsquo;EF 4.1 Update 2&amp;rsquo;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Getting Started&lt;/h2&gt;
&lt;p&gt;The following walkthroughs are available for EF 4.2:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="background-color: #ffffff;"&gt;&lt;/span&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx"&gt;Code First Walkthrough&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx"&gt;Model First / Database First Walkthrough&lt;/a&gt;&lt;span style="background-color: #ffffff;"&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Getting EF 4.2 Release Candidate&lt;/h2&gt;
&lt;p&gt;EF 4.2 Release Candidate is available via &lt;a href="http://nuget.org"&gt;NuGet&lt;/a&gt; as the &lt;a href="http://nuget.org/List/Packages/EntityFramework.Preview"&gt;EntityFramework.Preview&lt;/a&gt; package. If you already have the EntityFramework package installed then updating to the latest version will give you EF 4.2.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://nuget.org/List/Packages/EntityFramework.Preview"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="EntityFramework.Preview" border="0" alt="EntityFramework.Preview" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3201.EntityFramework.Preview_5F00_58CB23FD.jpg" width="762" height="86" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Model First &amp;amp; Database First Templates&lt;/h2&gt;
&lt;p&gt;The templates for using the DbContext API with Model First and Database First are now available under the &amp;ldquo;Online Templates&amp;rdquo; tab when &amp;ldquo;Right-Click &amp;ndash;&amp;gt; Add Code Generation Item&amp;hellip;&amp;rdquo; is selected on the EF Designer.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/3223.AddDbContextTemplate_5F00_05892DA8.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AddDbContextTemplate" border="0" alt="AddDbContextTemplate" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/0083.AddDbContextTemplate_5F00_thumb_5F00_495D61FF.png" width="652" height="387" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Support&lt;/h2&gt;
&lt;p&gt;This is a preview of features that will be available in future releases and is designed to allow you to provide feedback on the design of these features. It is not intended or licensed for use in production. If you need assistance we have an &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads"&gt;Entity Framework Pre-Release Forum&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;What&amp;rsquo;s Not in This Release?&lt;/h2&gt;
&lt;p&gt;As covered earlier this release is just a small update to the DbContext &amp;amp; Code First runtime. The features that were included in &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx"&gt;EF June 2011 CTP&lt;/a&gt; require changes to the Core Entity Framework Libraries that are part of the .NET Framework and will ship at a later date. Note that Code First Migrations is not compatible with the EntityFramework.Preview package.&amp;nbsp; Please continue to use the most recent EntityFramework package when working with Migrations.&amp;nbsp; Our Code First Migrations work is continuing and we are working to get the next release in your hands soon.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;ADO.NET Entity Framework Team&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10215185" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-21-51-85/Entity-Framework-4.2-RC-_2D00_-EULA.rtf" length="150019" type="application/msword" /><category domain="http://blogs.msdn.com/b/adonet/archive/tags/ADO-NET/">ADO.NET</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Entity+Framework/">Entity Framework</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/What_2700_s+New/">What's New</category><category domain="http://blogs.msdn.com/b/adonet/archive/tags/Code+First/">Code First</category></item></channel></rss>
