<?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 Community 5.6.583.21163 (Build: 5.6.583.21163)</generator><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>34</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>16</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>14</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>3</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>1</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>9</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>45</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><item><title>EF 4.2 Code First Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx</link><pubDate>Wed, 28 Sep 2011 15:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10214236</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=10214236</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-release-candidate-available.aspx"&gt;Entity Framework 4.2 Release Candidate&lt;/a&gt; is now available and contains a small update to the DbContext API &amp;amp; Code First runtime that were released as part of EF 4.1.&lt;/p&gt;  &lt;p&gt;This post will provide an introduction to Code First development and how it can be used with the DbContext API surface. Code First allows you to define your model using C# or VB.Net classes, optionally additional configuration can be performed using attributes on your classes and properties or by using a Fluent API. Your model can be used to generate a database schema or to map to an existing database.&lt;/p&gt;  &lt;p&gt;You will need to have Visual Studio 2010 installed to complete this walkthrough.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;span style="text-decoration: underline"&gt;       &lt;br /&gt;Mapping to an Existing Database&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This walkthrough is going to demonstrate Code First generating the database schema but the same principals apply to mapping to an existing database, with the exception of ‘&lt;em&gt;6. Setting an Initialization Strategy&lt;/em&gt;’&lt;span style="background-color: #ffff00"&gt;&lt;/span&gt; which does not apply to existing databases. If Code First is pointed at an existing database that it did not create then it will just attempt to use the specified configuration to access the database. The easiest way to point Code First to an existing database is to add a App/Web.config connection string with the same name as your derived DbContext, for example;&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" 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;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;&amp;gt; &lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&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;MyProductContext&lt;/span&gt;&amp;quot; &lt;span style="color: #a31515"&gt; &lt;br /&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;br /&gt;    &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=Products;Trusted_Connection=true;&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" 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;connectionStrings&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;&amp;gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;1. Create the Application&lt;/h2&gt;

&lt;p&gt;To keep things simple we’re going to build up a basic console application that uses the Code First to perform data access:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Open Visual Studio 2010 &lt;/li&gt;

  &lt;li&gt;File -&amp;gt; New -&amp;gt; Project… &lt;/li&gt;

  &lt;li&gt;Select “Windows” from the left menu and “Console Application” &lt;/li&gt;

  &lt;li&gt;Enter “CodeFirstSample” as the name &lt;/li&gt;

  &lt;li&gt;Select “OK” &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;2. Create the Model&lt;/h2&gt;

&lt;p&gt;Let’s define a very simple model using classes. I’m just defining them in the Program.cs file but in a real world application you would split your classes out into separate files and potentially a separate project.&lt;/p&gt;

&lt;p&gt;Below the Program class definition in Program.cs I am defining the following two classes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;public class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Category &lt;br /&gt;&lt;/span&gt;{ &lt;br /&gt;&lt;span style="color: blue"&gt;  public string &lt;/span&gt;CategoryId { &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;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 virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Product&lt;/span&gt;&amp;gt; Products { &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&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Product &lt;br /&gt;&lt;/span&gt;{ &lt;br /&gt;&lt;span style="color: blue"&gt;  public int &lt;/span&gt;ProductId { &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;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;CategoryId { &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 virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Category &lt;/span&gt;Category { &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&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;3. Create a Context&lt;/h2&gt;

&lt;p&gt;The simplest way to start using the classes for data access is to define a context that derives from System.Data.Entity.DbContext and exposes a typed DbSet&amp;lt;TEntity&amp;gt; for each class in my model.&lt;/p&gt;

&lt;p&gt;We’re now starting to use types from the Entity Framework so we need to add the EntityFramework NuGet package:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Project –&amp;gt; Add Library Package Reference… &lt;/li&gt;

  &lt;li&gt;Select the “Online” tab &lt;/li&gt;

  &lt;li&gt;Search for “EntityFramework.Preview” and select the package &lt;/li&gt;

  &lt;li&gt;Click “Install” &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add a using statement for System.Data.Entity at the top of Program.cs&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add a derived context below the existing classes that we’ve defined&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProductContext &lt;/span&gt;: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;DbContext &lt;br /&gt;&lt;/span&gt;{ &lt;br /&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;Category&lt;/span&gt;&amp;gt; Categories { &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 &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Product&lt;/span&gt;&amp;gt; Products { &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&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is all the code we need to write to start storing and retrieving data. Obviously there is quite a bit going on behind the scenes and we’ll take a look at that in a moment but first let’s see it in action.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;4. Reading &amp;amp; Writing Data&lt;/h2&gt;

&lt;p&gt;I’m padding out the Main method in my program class as follows:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Program &lt;br /&gt;&lt;/span&gt;{ &lt;br /&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;br /&gt;  { &lt;br /&gt;&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;ProductContext&lt;/span&gt;()) &lt;br /&gt;    { &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Add a food category &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;      var &lt;/span&gt;food = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Category &lt;/span&gt;{ CategoryId = &lt;span style="color: #a31515"&gt;&amp;quot;FOOD&amp;quot;&lt;/span&gt;, Name = &lt;span style="color: #a31515"&gt;&amp;quot;Foods&amp;quot; &lt;/span&gt;}; &lt;br /&gt;      db.Categories.Add(food); &lt;br /&gt;&lt;span style="color: blue"&gt;      int &lt;/span&gt;recordsAffected = db.SaveChanges(); &lt;br /&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.WriteLine( &lt;span style="color: #a31515"&gt;&amp;quot;Saved {0} entities to the database, press any key to exit.&amp;quot;&lt;/span&gt;, recordsAffected); &lt;br /&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.ReadKey(); &lt;br /&gt;    } &lt;br /&gt;  } &lt;br /&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can now run the application and see that the new category is inserted.&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;strong&gt;&lt;span style="text-decoration: underline"&gt;Where’s My Data?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By convention DbContext has created a database for you on localhost\SQLEXPRESS. The database is named after the fully qualified name of your derived context, in our case that is “CodeFirstSample.ProductContext”. We’ll look at ways to change this later in the walkthrough.&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;strong&gt;&lt;span style="text-decoration: underline"&gt;Model Discovery&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DbContext worked out what classes to include in the model by looking at the DbSet properties that we defined. It then uses the default Code First conventions to find primary keys, foreign keys etc.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;5. Reading &amp;amp; Writing More Data&lt;/h2&gt;

&lt;p&gt;Let’s pad out the program we just wrote to show a bit more functionality. We are going to make use of the Find method on DbSet that will locate an entity based on primary key. If no match is found then Find will return null. We’re also making use of LINQ to query for all products in the Food category ordered alphabetically by name.&lt;/p&gt;

&lt;p&gt;I’m replacing the Main we wrote above with the following:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Program &lt;br /&gt;&lt;/span&gt;{ &lt;br /&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;br /&gt;  { &lt;br /&gt;&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;ProductContext&lt;/span&gt;()) &lt;br /&gt;    { &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Use Find to locate the Food category &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;      var &lt;/span&gt;food = db.Categories.Find(&lt;span style="color: #a31515"&gt;&amp;quot;FOOD&amp;quot;&lt;/span&gt;); &lt;br /&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;      if &lt;/span&gt;(food == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;br /&gt;      { &lt;br /&gt;        food = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Category &lt;/span&gt;{ CategoryId = &lt;span style="color: #a31515"&gt;&amp;quot;FOOD&amp;quot;&lt;/span&gt;, Name = &lt;span style="color: #a31515"&gt;&amp;quot;Foods&amp;quot; &lt;/span&gt;}; &lt;br /&gt;        db.Categories.Add(food); &lt;br /&gt;      } &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Create a new Food product &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.Write(&lt;span style="color: #a31515"&gt;&amp;quot;Please enter a name for a new food: &amp;quot;&lt;/span&gt;); &lt;br /&gt;&lt;span style="color: blue"&gt;      var &lt;/span&gt;productName = &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine(); &lt;br /&gt;&lt;span style="color: blue"&gt;      var &lt;/span&gt;product = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Product &lt;/span&gt;{ Name = productName, Category = food }; &lt;br /&gt;      db.Products.Add(product); &lt;br /&gt;&lt;span style="color: blue"&gt;      int &lt;/span&gt;recordsAffected = db.SaveChanges(); &lt;br /&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.WriteLine( &lt;span style="color: #a31515"&gt;&amp;quot;Saved {0} entities to the database.&amp;quot;&lt;/span&gt;, recordsAffected); &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Query for all Food products using LINQ &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;      var &lt;/span&gt;allFoods = &lt;span style="color: blue"&gt;from &lt;/span&gt;p &lt;span style="color: blue"&gt;in &lt;/span&gt;db.Products &lt;br /&gt;        &lt;span style="color: blue"&gt;where &lt;/span&gt;p.CategoryId == &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #a31515"&gt;&amp;quot;FOOD&amp;quot; &lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;orderby &lt;/span&gt;p.Name &lt;span style="color: blue"&gt;select &lt;/span&gt;p; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;All foods in database:&amp;quot;&lt;/span&gt;); &lt;br /&gt;&lt;span style="color: blue"&gt;      foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;allFoods) &lt;br /&gt;      { &lt;br /&gt;&lt;span style="color: #2b91af"&gt;        Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot; - {0}&amp;quot;&lt;/span&gt;, item.Name); &lt;br /&gt;      } &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;      &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Press any key to exit.&amp;quot;&lt;/span&gt;); &lt;br /&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.ReadKey(); &lt;br /&gt;    } &lt;br /&gt;  } &lt;br /&gt;}&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;6. Setting an Initialization Strategy&lt;/h2&gt;

&lt;p&gt;In the next section we are going to start changing our model which in turn means the database schema needs to change as well. Currently there is no ‘out of the box’ solution to evolve your existing schema in place. Database evolution is something we are currently working on, a preview of our work in this space is available as Code First Migrations Alpha 3.&lt;/p&gt;

&lt;p&gt;While there is no RTM migrations solution there is the opportunity to run some custom logic to initialize the database the first time a context is used in an AppDomain. This is handy if you want to insert seed data for test runs but it’s also useful to re-create the database if the model has changed. In EF we include a couple of strategies you can plug in but you can also write custom ones.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;For the walkthrough we just want to drop and re-create the database whenever the model has changed, so at the top of the Main method in my Program class I’ve added the following code&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Database&lt;/span&gt;.SetInitializer&amp;lt;&lt;span style="color: #2b91af"&gt;ProductContext&lt;/span&gt;&amp;gt;(&lt;br /&gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DropCreateDatabaseIfModelChanges&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ProductContext&lt;/span&gt;&amp;gt;());&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;7. Data Annotations&lt;/h2&gt;

&lt;p&gt;So far we’ve just let EF discover the model using its default conventions but there are going to be times when our classes don’t follow the conventions and we need to be able to perform further configuration. There are two options for this; we’ll look at Data Annotations in this section and then the fluent API in the next section.&lt;/p&gt;

&lt;p&gt;Let’s add a supplier class to our model:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;public class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Supplier &lt;br /&gt;&lt;/span&gt;{ &lt;br /&gt;    &lt;span style="color: blue"&gt;public string &lt;/span&gt;SupplierCode { &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;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&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;And we also need to add a set to our derived context&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProductContext &lt;/span&gt;: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;DbContext &lt;br /&gt;&lt;/span&gt;{ &lt;br /&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;Category&lt;/span&gt;&amp;gt; Categories { &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 &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Product&lt;/span&gt;&amp;gt; Products { &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="background-color: #ffff00"&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;Supplier&lt;/span&gt;&amp;gt; Suppliers { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt; &lt;br /&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now if we ran our application we’d get an InvalidOperationException saying “&lt;em&gt;EntityType 'Supplier' has no key defined. Define the key for this EntityType.”&lt;/em&gt; because EF has no way of knowing that SupplierCode should be the primary key for Supplier.&lt;/p&gt;

&lt;p&gt;We’re going to use Data Annotations now so we need to a using statement at the top of Program.cs:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.DataAnnotations;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we can annotate the SupplierCode property to identify that it is the primary key:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;public class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Supplier &lt;br /&gt;&lt;/span&gt;{ &lt;br /&gt;  &lt;span style="background-color: #ffff00"&gt;[&lt;span style="color: #2b91af"&gt;Key&lt;/span&gt;]&lt;/span&gt; &lt;br /&gt;  &lt;span style="color: blue"&gt;public string &lt;/span&gt;SupplierCode { &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;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&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The full list of annotations supported in EF is;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;KeyAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;StringLengthAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;MaxLengthAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;ConcurrencyCheckAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;RequiredAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;TimestampAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;ComplexTypeAttribute &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;ColumnAttribute 
      &lt;br /&gt;Placed on a property to specify the column name, ordinal &amp;amp; data type &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;TableAttribute 
      &lt;br /&gt;Placed on a class to specify the table name and schema &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;InversePropertyAttribute 
      &lt;br /&gt;Placed on a navigation property to specify the property that represents the other end of a relationship &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;ForeignKeyAttribute 
      &lt;br /&gt;Placed on a navigation property to specify the property that represents the foreign key of the relationship &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;DatabaseGeneratedAttribute 
      &lt;br /&gt;Placed on a property to specify how the database generates a value for the property (Identity, Computed or None) &lt;/span&gt;&lt;/li&gt;

  &lt;li&gt;&lt;span style="font-size: x-small" size="2"&gt;NotMappedAttribute 
      &lt;br /&gt;Placed on a property or class to exclude it from the database&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;8. Fluent API&lt;/h2&gt;

&lt;p&gt;In the previous section we looked at using Data Annotations to supplement or override what was detected by conventions. The other way to further configure the model is via the Code First fluent API. The fluent API is considered a more advanced feature and we would recommend using Data Annotations unless your requirements require you to use the fluent API.&lt;/p&gt;

&lt;p&gt;To access the fluent API we override the OnModelCreating method in DbContext, in the following code we are using the fluent API to configure the Name property on Supplier to be required.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: xx-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProductContext &lt;/span&gt;: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: xx-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;DbContext &lt;br /&gt;&lt;/span&gt;{ &lt;br /&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;Category&lt;/span&gt;&amp;gt; Categories { &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 &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Product&lt;/span&gt;&amp;gt; Products { &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 &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Supplier&lt;/span&gt;&amp;gt; Suppliers { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: xx-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: xx-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;font style="background-color: #ffff00"&gt;  &lt;span style="background-color: #ffff00"&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)&lt;/span&gt; &lt;br /&gt;  &lt;span style="background-color: #ffff00"&gt;{&lt;/span&gt; &lt;/font&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="background-color: #ffff00"&gt; &lt;br /&gt;    modelBuilder.Entity&amp;lt;&lt;span style="color: #2b91af"&gt;Supplier&lt;/span&gt;&amp;gt;()&lt;/span&gt; &lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="background-color: #ffff00"&gt; &lt;br /&gt;      .Property(s =&amp;gt; s.Name) &lt;/span&gt; &lt;/font&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="background-color: #ffff00"&gt; &lt;br /&gt;      .IsRequired();&lt;/span&gt; &lt;br /&gt;  &lt;span style="background-color: #ffff00"&gt;}&lt;/span&gt;&lt;/font&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: xx-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough we looked at Code First development using EF. We looked at defining and configuring a model, storing and retrieving data, configuring the database connection, updating the database schema as our model evolved and validating data before it is written to the database.&lt;/p&gt;

&lt;h6&gt;&amp;#160;&lt;/h6&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=10214236" width="1" height="1"&gt;</description></item><item><title>EF 4.2 Model &amp; Database First Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx</link><pubDate>Wed, 28 Sep 2011 14:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10214235</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=10214235</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-release-candidate-available.aspx"&gt;Entity Framework 4.2 Release Candidate&lt;/a&gt; is now available and contains a small update to the DbContext API &amp;amp; Code First runtime that were released as part of EF 4.1.&lt;/p&gt;  &lt;p&gt;This post will provide an introduction to Model First and Database First development with the DbContext API, using the Entity Data Model Designer in Visual Studio.&lt;/p&gt;  &lt;p&gt;You will need to have Visual Studio 2010 installed to complete this walkthrough.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;1. Create the Application&lt;/h2&gt;  &lt;p&gt;To keep things simple we’re going to build up a basic console application that uses the DbContext to perform data access:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Open Visual Studio 2010 &lt;/li&gt;    &lt;li&gt;File -&amp;gt; New -&amp;gt; Project… &lt;/li&gt;    &lt;li&gt;Select “Windows” from the left menu and “Console Application” &lt;/li&gt;    &lt;li&gt;Enter “ModelFirstSample” as the name &lt;/li&gt;    &lt;li&gt;Select “OK” &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;2. Create the Model&lt;/h2&gt;  &lt;p&gt;Let’s go ahead and add an Entity Data Model to our project;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Project –&amp;gt; Add New Item… &lt;/li&gt;    &lt;li&gt;Select ‘Data’ from the left menu &lt;/li&gt;    &lt;li&gt;Select ‘ADO.NET Entity Data Model’ from the list of available items &lt;/li&gt;    &lt;li&gt;Name the model ‘PersonModel.edmx’ &lt;/li&gt;    &lt;li&gt;Click ‘Add’ &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We are going to use Model First for this walkthrough but if you are mapping to an existing database you would now select ‘Generate from database’, follow the prompts and then skip to step 4.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Select ‘Empty model’ &lt;/li&gt;    &lt;li&gt;Click ‘Finish’ &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Let’s add a Person entity to our model:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;On the design surface; Right Click –&amp;gt; Add –&amp;gt; Entity &lt;/li&gt;    &lt;li&gt;Name the entity ‘Person’ &lt;/li&gt;    &lt;li&gt;Click ‘OK’ &lt;/li&gt;    &lt;li&gt;On the Person entity; Right Click –&amp;gt; Add –&amp;gt; Scalar Property &lt;/li&gt;    &lt;li&gt;Name the property ‘Full Name’ &lt;/li&gt; &lt;/ul&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/0878.image_5F00_0677F887.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 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="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-68-19-metablogapi/3515.image_5F00_thumb_5F00_742F31C4.png" width="235" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;3. Create the Database&lt;/h2&gt;  &lt;p&gt;Now that we’ve defined the model we can generate a database schema to store our data:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;On the design surface; Right Click –&amp;gt; Generate Database from Model &lt;/li&gt;    &lt;li&gt;Click ‘New Connection…’ &lt;/li&gt;    &lt;li&gt;Specify the details of the database you wish to create &lt;/li&gt;    &lt;li&gt;Click ‘OK’ &lt;/li&gt;    &lt;li&gt;If prompted to create the database; click ‘Yes’ &lt;/li&gt;    &lt;li&gt;Click ‘Next’ then ‘Finish’ &lt;/li&gt;    &lt;li&gt;On the generated script; Right Click –&amp;gt; Execute SQL… &lt;/li&gt;    &lt;li&gt;Specify your database server and click ‘Connect’ &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;4. Swap to DbContext Code Generation&lt;/h2&gt;  &lt;p&gt;The PersonModel is currently generating a derived ObjectContext and entity classes that derive from EntityObject, we want to make use of the simplified DbContext API.&lt;/p&gt;  &lt;p&gt;To use DbContext we need to install the EntityFramework NuGet package:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Project –&amp;gt; Add Library Package Reference… &lt;/li&gt;    &lt;li&gt;Select the “Online” tab &lt;/li&gt;    &lt;li&gt;Search for “EntityFramework.Preview” and select the package &lt;/li&gt;    &lt;li&gt;Click “Install” &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now we can swap to using DbContext code generation templates:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;On the design surface; Right Click –&amp;gt; Add Code Generation Item… &lt;/li&gt;    &lt;li&gt;Select ‘Online Templates’ from the left menu &lt;/li&gt;    &lt;li&gt;Search for ‘DbContext’ &lt;/li&gt;    &lt;li&gt;Select ‘ADO.NET C# DbContext Generator’ &lt;/li&gt;    &lt;li&gt;Name the item ‘PersonModel.tt’ &lt;/li&gt;    &lt;li&gt;Click ‘Add’ &lt;/li&gt; &lt;/ul&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/6355.AddDbContextTemplate_5F00_701405EF.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/7848.AddDbContextTemplate_5F00_thumb_5F00_7AD15D44.png" width="648" height="383" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You’ll notice that two items are added to your project:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;PersonModel.tt      &lt;br /&gt;This template generates very simple POCO classes for each entity in your model &lt;/li&gt;    &lt;li&gt;PersonModel.Context.tt      &lt;br /&gt;This template generates a derived DbContext to use for querying and persisting data &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;5. Read &amp;amp; Write Data&lt;/h2&gt;  &lt;p&gt;It’s time to access some data, I’m padding out the Main method in Program.cs file as follows;&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;span style="font-family: consolas; font-size: x-small"&gt;&lt;span style="font-family: calibri; font-size: x-small" face="Calibri" size="2"&gt;class &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;Program &lt;br /&gt;&lt;/span&gt;{ &lt;br /&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;br /&gt;  { &lt;br /&gt;&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;PersonModelContainer&lt;/span&gt;()) &lt;br /&gt;    { &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Save some data &lt;br /&gt;&lt;/span&gt;      db.People.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Person &lt;/span&gt;{ FullName = &lt;span style="color: #a31515"&gt;&amp;quot;Bob&amp;quot; &lt;/span&gt;}); &lt;br /&gt;      db.People.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Person &lt;/span&gt;{ FullName = &lt;span style="color: #a31515"&gt;&amp;quot;Ted&amp;quot; &lt;/span&gt;}); &lt;br /&gt;      db.People.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Person &lt;/span&gt;{ FullName = &lt;span style="color: #a31515"&gt;&amp;quot;Jane&amp;quot; &lt;/span&gt;}); &lt;br /&gt;      db.SaveChanges(); &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Use LINQ to access data &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;      var &lt;/span&gt;people = &lt;span style="color: blue"&gt;from &lt;/span&gt;p &lt;span style="color: blue"&gt;in &lt;/span&gt;db.People &lt;br /&gt;&lt;span style="color: blue"&gt;        orderby &lt;/span&gt;p.FullName &lt;br /&gt;&lt;span style="color: blue"&gt;        select &lt;/span&gt;p; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: #2b91af"&gt;      Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;All People:&amp;quot;&lt;/span&gt;); &lt;br /&gt;&lt;span style="color: blue"&gt;      foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;person &lt;span style="color: blue"&gt;in &lt;/span&gt;people) &lt;br /&gt;      { &lt;br /&gt;&lt;span style="color: #2b91af"&gt;        Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;- {0}&amp;quot;&lt;/span&gt;, person.FullName); &lt;br /&gt;      } &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;&lt;span style="color: green"&gt;      // Change someones name &lt;br /&gt;&lt;/span&gt;      db.People.First().FullName = &lt;span style="color: #a31515"&gt;&amp;quot;Janet&amp;quot;&lt;/span&gt;; &lt;br /&gt;      db.SaveChanges(); &lt;br /&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="font-family: consolas"&gt;&lt;span style="font-size: x-small"&gt;&lt;span style="font-family: calibri" face="Calibri"&gt;&lt;span style="font-size: x-small" size="2"&gt;    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Press any key to exit...&amp;quot;&lt;/span&gt;); &lt;br /&gt;&lt;span style="color: #2b91af"&gt;    Console&lt;/span&gt;.ReadKey(); &lt;br /&gt;  }&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;} &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this walkthrough we looked at Model First development using the DbContext API. We looked at building a model, generating a database, swapping to DbContext code generation and then saving and querying data.&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=10214235" width="1" height="1"&gt;</description></item><item><title>Code First Migrations: Alpha 3 Released</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-released.aspx</link><pubDate>Thu, 22 Sep 2011 06:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10215177</guid><dc:creator>dpblogs</dc:creator><slash:comments>43</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10215177</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-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;A couple of weeks ago we released Alpha 2 of Code First Migrations. Today we are happy to make Alpha 3 available to you.&lt;/p&gt;  &lt;p&gt;The overall feedback we got on Alpha 2 was that you like the direction we are headed. We heard that there is definitely a use case for automatic migrations, and while not everyone will use them, we should continue to invest in them. You are asking us to look at the story for data migration that doesn’t involve dropping down to SQL. Alpha 2 also had some commonly encountered bugs that were annoying and we needed to fix&lt;/p&gt;  &lt;p&gt;Alpha 3 is still is primarily focused on the developer experience inside of Visual Studio. This release adds most of development time features we plan to include in the first RTM. Your feedback will guide any changes we make to the overall design. Apart from responding to your feedback our team is starting to focus on improving quality. We are also working on completing the deployment and team-build stories, including a command line tool and an MSDeploy provider.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed&lt;/h2&gt;  &lt;p&gt;Alpha 3 is focused on adding the remaining features that you have been asking for around development time migrations. The primary changes in Alpha 3 are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;VB.NET migration generation&lt;/strong&gt; is now available. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Upgrade/Downgrade to any migration&lt;/strong&gt; is now available via the –TargetMigration switch. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Seed data&lt;/strong&gt; can now be added by overriding the Seed method of the Settings class. Data is expressed in terms of the current model and the Seed method is called after upgrading to the latest migration. &lt;/li&gt;    &lt;li&gt;We &lt;strong&gt;removed TargetDatabase from the Settings class&lt;/strong&gt;. Instead you can use the –TargetDatabase switch to specify the name of a connection string from your App/Web.config file. &lt;/li&gt;    &lt;li&gt;You can now start using &lt;strong&gt;Code First Migrations against an existing database that was created by Code First&lt;/strong&gt;. To do this issue an Update-Database command when you model is the same as the model that created the database. To use this feature the EdmMetadata table must be present in the database. This original creation of the database and tables will be counted as an automatic migration, if you want a code-based migration for the initial create of the database and tables you must drop the database and begin using migrations against an empty or non-existent database. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Improved error logging&lt;/strong&gt; when an exception is encountered. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;The bugs that required SQL Compact and SQL Express&lt;/strong&gt; to be available are now resolved. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;&lt;span style="color: #ff0000" color="#ff0000"&gt;&lt;span style="text-decoration: underline"&gt;&lt;span style="font-weight: bold"&gt;Known Issues&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;  &lt;p&gt;Some known issues, and ‘yet to be implemented’ features include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;&lt;span style="background-color: #ffff00"&gt;Code-based migrations and databases generated with Alpha 2 cannot be used with Alpha 3.&lt;/span&gt; &lt;/strong&gt;Because Alpha 2 was such an early release we needed to make some changes to the table we use to store migrations and the way we generate and identify code-based migrations. If you were using Alpha 2 you will need to regenerate migrations and run them to create your database. We realize this is painful but it is one of the trade-offs associated with getting previews into your hands early in the development process. We are making an effort to reduce such changes in future previews. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;span style="background-color: #ffff00"&gt;If you are updating the EntityFramework.Migrations package you will need to close and re-open Visual Studio after updating.&lt;/span&gt;&lt;/strong&gt; This is required to reload the updated command assemblies. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;No outside-of-Visual-Studio experience&lt;/strong&gt;. Alpha 2 only includes the Visual Studio integrated experience. We also plan to deliver a command line tool and an MSDeploy provider for Code First Migrations. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Feedback&lt;/h2&gt;  &lt;p&gt;We really want your feedback on Code First Migrations. Is the solution we are delivering what you want? Are there features we should add? Please comment on this post with any feedback you might have.&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 Alpha 3, 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&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-no-magic-walkthrough.aspx"&gt;Code First Migrations: Alpha 3 ‘No-Magic’ Walkthrough&lt;/a&gt;&lt;/span&gt; &lt;/li&gt;    &lt;li&gt;&lt;span&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-with-magic-walkthrough-automatic-migrations.aspx"&gt;Code First Migrations: Alpha 3 ‘With-Magic’ Walkthrough (Automatic Migrations)&lt;/a&gt;&lt;/span&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Code First Migrations Alpha 3 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;h2&gt;Prerequisites &amp;amp; Incompatibilities&lt;/h2&gt;  &lt;p&gt;Migrations is dependent on &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;, this updated release of EF 4.1 will be automatically installed when you install the EntityFramework.Migrations package.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Important: &lt;/strong&gt;&lt;em&gt;If you have previously run the EF 4.1 stand alone installer you will need to &lt;/em&gt;&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=26825"&gt;&lt;em&gt;upgrade&lt;/em&gt;&lt;/a&gt;&lt;em&gt; or remove the installation before using migrations. This is required because the installer adds the EF 4.1 assembly to the Global Assembly Cache (GAC) causing the original RTM version to be used at runtime rather than Update 1.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;This release is incompatible with “Microsoft Data Services, Entity Framework, and SQL Server Tools for Data Framework June 2011 CTP”.&lt;/p&gt;  &lt;p&gt;&amp;#160;&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;#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=10215177" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-21-51-77/License.rtf" length="151060" 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><item><title>Code First Migrations: Alpha 3 ‘No-Magic’ Walkthrough</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-no-magic-walkthrough.aspx</link><pubDate>Wed, 21 Sep 2011 21:25:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10215011</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=10215011</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-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 third preview of our migrations story for Code First development; &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-released.aspx"&gt;Code First Migrations Alpha 3&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 ‘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;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-with-magic-walkthrough-automatic-migrations.aspx"&gt;Code First Migrations: Alpha 3 ‘With-Magic’ Walkthrough&lt;/a&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 was included in EF 4.1, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-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 ‘Alpha3Demo’ 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;/span&gt;Alpha3Demo
{
    &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;Settings&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. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Settings&lt;/span&gt; class also exposes the provider model for code generation and SQL generation. We’ll just edit the settings 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;br /&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.Data.Entity.Migrations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations.Providers;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.SqlClient;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Alpha3Demo.Migrations
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Settings &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DbMigrationContext&lt;/span&gt;&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;Settings()
        {
            AutomaticMigrationsEnabled = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
            SetCodeGenerator&amp;lt;&lt;span style="color: #2b91af"&gt;CSharpMigrationCodeGenerator&lt;/span&gt;&amp;gt;();
            AddSqlGenerator&amp;lt;&lt;span style="color: #2b91af"&gt;SqlConnection&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;SqlServerMigrationSqlGenerator&lt;/span&gt;&amp;gt;();

            &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;// Uncomment the following line if you are using SQL Server Compact 
            // SQL Server Compact is available as the SqlServerCompact NuGet package
            // AddSqlGenerator&amp;lt;System.Data.SqlServerCe.SqlCeConnection, SqlCeMigrationSqlGenerator&amp;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.
            //    - The method should be written defensively in order that duplicate data is not created. E.g:
            //
            //        if (!context.Countries.Any())
            //        {
            //            context.Countries.Add(new Country { Name = &amp;quot;Australia&amp;quot; });
            //            context.Countries.Add(new Country { Name = &amp;quot;New Zealand&amp;quot; });
            //        }
            //
        &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. Once it has calculated what needs to change in the database, Code First Migrations will use the &lt;span style="color: #c0504d" color="#c0504d"&gt;CSharpMigrationCodeGenerator&lt;/span&gt; that was configured in our &lt;span style="color: #c0504d" color="#c0504d"&gt;Settings&lt;/span&gt; class to create the migration. 

    &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;Alpha3Demo.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; Alpha3Demo.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/5543.Alpha3DemoDatabase_5F00_521D39C8.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="Alpha3DemoDatabase" border="0" alt="Alpha3DemoDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/5556.Alpha3DemoDatabase_5F00_thumb_5F00_50003AFF.jpg" width="353" height="204" /&gt;&lt;/a&gt; 

    &lt;br /&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 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;Alpha3Demo.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)
                &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()
        {
            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;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;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 in Alpha 3, 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 Blog.Abstract 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.&amp;#160; &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;Alpha3Demo.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;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt; &lt;!--EndFragment--&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&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.&lt;/p&gt;

&lt;p&gt;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. In this preview we need to generate a script by pointing to a database to migrate, but in the future you will be able to generate a script between two named versions without pointing to a database.&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 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=10215011" 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></item><item><title>Code First Migrations: Alpha 3 ‘With-Magic’ Walkthrough (Automatic Migrations)</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-with-magic-walkthrough-automatic-migrations.aspx</link><pubDate>Wed, 21 Sep 2011 21:24:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10215010</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=10215010</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-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 third preview of our migrations story for Code First development; &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-released.aspx"&gt;Code First Migrations Alpha 3&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/09/21/code-first-migrations-alpha-3-no-magic-walkthrough.aspx"&gt;separate 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.1, if you are not familiar with Code First then please complete the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-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 ‘Alpha3AutoDemo’ 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;Alpha3AutoDemo
{
    &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;Settings&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. The &lt;span style="color: #c0504d" color="#c0504d"&gt;Settings&lt;/span&gt; class also exposes the provider model for code generation and SQL generation.&amp;#160; &lt;br /&gt;

    &lt;br /&gt;We’ll just edit the setting 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;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.Data.Entity.Migrations;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.Entity.Migrations.Providers;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Data.SqlClient;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Alpha3AutoDemo.Migrations
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Settings &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DbMigrationContext&lt;/span&gt;&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;Settings()
        {
            &lt;font style="background-color: #ffff00"&gt;AutomaticMigrationsEnabled = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/font&gt;
            SetCodeGenerator&amp;lt;&lt;span style="color: #2b91af"&gt;CSharpMigrationCodeGenerator&lt;/span&gt;&amp;gt;();
            AddSqlGenerator&amp;lt;&lt;span style="color: #2b91af"&gt;SqlConnection&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;SqlServerMigrationSqlGenerator&lt;/span&gt;&amp;gt;();

            &lt;/font&gt;&lt;/font&gt;&lt;font face="Calibri"&gt;&lt;font size="2"&gt;&lt;span style="color: green"&gt;// Uncomment the following line if you are using SQL Server Compact 
            // SQL Server Compact is available as the SqlServerCompact NuGet package
            // AddSqlGenerator&amp;lt;System.Data.SqlServerCe.SqlCeConnection, SqlCeMigrationSqlGenerator&amp;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.
            //    - The method should be written defensively in order that duplicate data is not created. E.g:
            //
            //        if (!context.Countries.Any())
            //        {
            //            context.Countries.Add(new Country { Name = &amp;quot;Australia&amp;quot; });
            //            context.Countries.Add(new Country { Name = &amp;quot;New Zealand&amp;quot; });
            //        }
            //
        &lt;/span&gt;}
    }
}&lt;/font&gt;&lt;/font&gt;&lt;/pre&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; Alpha3Demo.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/2318.Alpha3AutoDemoDatabase_5F00_5E4CC0BB.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="Alpha3AutoDemoDatabase" border="0" alt="Alpha3AutoDemoDatabase" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-68-19-metablogapi/7827.Alpha3AutoDemoDatabase_5F00_thumb_5F00_2BE8ED3C.jpg" width="357" height="205" /&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;Alpha3AutoDemo.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 in Alpha 3, 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;!--EndFragment--&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;Alpha3AutoDemo.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;br /&gt;&lt;font color="#ffffff"&gt;.&lt;/font&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&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.&amp;#160; &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.&lt;/p&gt;

&lt;p&gt;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. In this preview we need to generate a script by pointing to a database to migrate, but in the future you will be able to generate a script between two named versions without pointing to a database.&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=10215010" 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></item><item><title>Microsoft SQL Server OLEDB Provider Deprecation Announcement</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/13/microsoft-sql-server-oledb-provider-deprecation-announcement.aspx</link><pubDate>Tue, 13 Sep 2011 23:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10210323</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=10210323</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/13/microsoft-sql-server-oledb-provider-deprecation-announcement.aspx#comments</comments><description>&lt;p&gt;The commercial release of Microsoft SQL Server, codename &amp;ldquo;Denali,&amp;rdquo; will be the last release to support OLE DB.&amp;nbsp; Support details for the release version of SQL Server &amp;ldquo;Denali&amp;rdquo; will be made available within 90 days of release to manufacturing. For more information on Microsoft Support Lifecycle Policies for Microsoft Business and Developer products, please see &lt;a href="http://support.microsoft.com/gp/lifepolicy"&gt;Microsoft Support Lifecycle Policy FAQ&lt;/a&gt;. This deprecation applies to the Microsoft SQL Server OLE DB provider only. Other OLE DB providers as well as the OLE DB standard will continue to be supported until explicitly announced.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s important to notice that this announcement does not affect ADO&amp;rsquo;s and ADO.NET&amp;rsquo;s roadmaps.&amp;nbsp; In addition, while&amp;nbsp;Managed OLEDB classes will continue to be supported, if you are using it to connect to SQL Server through the SNAC OLEDB Provider, you will be impacted.&lt;/p&gt;
&lt;p&gt;If you use SQL Server as your RDBMS, we encourage you to use SqlClient as your .NET Provider.&amp;nbsp;&amp;nbsp;In case&amp;nbsp;you use&amp;nbsp;other database technologies, we would recommend that you adopt their native .NET Providers or Managed ODBC in the development of new and future versions of your applications. &amp;nbsp;You don&amp;rsquo;t need to change your existing applications using OLE DB, as they will continue to be supported, but you may want to consider migrating them as a part of your future roadmap. Microsoft&lt;br /&gt;is fully committed to making this transition as smooth and easy as possible.&amp;nbsp;&amp;nbsp;In order to prepare and help our developer community, we will be providing assistance throughout this process. This will include providing guidance via technical documentation as well as tools to jump start the migration process and being available right away to answer questions on the &lt;span style="text-decoration: underline;"&gt;&lt;a href="http://social.technet.microsoft.com/Forums/en/sqldataaccess/threads"&gt;SQL Server Data Access forum&lt;/a&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Luiz Santos&lt;/p&gt;
&lt;p&gt;ADO.NET Program Manager&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10210323" width="1" height="1"&gt;</description></item><item><title>EF Code First Fluent API with VB.NET</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/13/ef-code-first-fluent-api-with-vb-net.aspx</link><pubDate>Tue, 13 Sep 2011 19:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10210159</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=10210159</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/13/ef-code-first-fluent-api-with-vb-net.aspx#comments</comments><description>&lt;p&gt;&lt;meta name="Generator" content="Microsoft Word 12 (filtered)" /&gt;&lt;/p&gt;
&lt;div class="WordSection1"&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;Entity Framework Code First is a development methodology available beginning with the Entity Framework 4.1. When using Code First development you usually begin by writing C# or Visual Basic .NET classes that define your conceptual (domain) model. Your model can then be used to generate a database schema or be mapped to an existing database. You can then load data from and persist data to database &lt;/span&gt;&lt;span style="font-size: 10.5pt; font-family: 'Segoe UI','sans-serif'; color: black;"&gt;using objects that are instances of the types defined in the conceptual model.&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;By default, Code First conventions are used to automatically configure mapping between the conceptual model and database schema. Code First conventions will work in most mapping scenarios, however, you can override these conventions with data annotations or fluent API. For more information about conventions supported by Code First, see &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh161541%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;http://msdn.microsoft.com/en-us/library/hh161541(VS.103).aspx&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt;. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;This post shows how to override Code First conventions using the fluent API with VB.NET. The post also demonstrates the following:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: Symbol; color: black;"&gt;&lt;span style="font: 7pt 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: 'Verdana','sans-serif'; color: black;"&gt;Configuring database creation and initialization strategy. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: Symbol; color: black;"&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: Symbol; color: black;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: 'Verdana','sans-serif'; color: black;"&gt;Retrieving and persisting data.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: Symbol; color: black;"&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: 'Verdana','sans-serif'; color: black;"&gt;Overriding the default Code First conventions to set the database name.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;Prerequisites &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 9pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;To complete this walkthrough you must have the following installed: &lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: Symbol; color: black;"&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: 'Verdana','sans-serif'; color: black;"&gt;Visual Studio 2010&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: Symbol; color: black;"&gt;&lt;/span&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkID=207115" target="blank"&gt;&lt;span style="font-size: 9pt; line-height: 140%; font-family: 'Verdana','sans-serif'; color: #0033cc; text-decoration: none;"&gt;Entity Framework 4.1&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;Defining the Model and overriding Code First conventions &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;In this step you will define VB.NET POCO entity types that represent the conceptual model. The classes do not need to derive from any base classes or implement any interfaces. To &lt;/span&gt;&lt;span style="font-size: 12pt; color: black;"&gt;interact with data as objects that are instances of entity types&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt; for data access you would define a class that derives from DbContext and exposes &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg696460%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;System.Data.Entity.DbSet&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; properties for the entity types defined in the model. &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg696460%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;System.Data.Entity.DbSet&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; r&lt;/span&gt;&lt;span style="font-size: 10.5pt; font-family: 'Segoe UI','sans-serif'; color: black;"&gt;epresents an entity set that is used to perform create, read, update, and delete operations.&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;When you run your application, DbContext checks to see whether a model for the type of the derived context and the given connection string is already cached in the application domain. If it is, then that model is used. If not, DbContext works out what classes to include in the model based on what DbSet properties were defined. It then uses the default Code First conventions and additional configuration that you specified through annotations or fluent API to build the model (as shown later in this post, the &lt;/span&gt;&lt;span style="font-size: 12pt; color: black;"&gt;protected &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.onmodelcreating%28v=vs.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;OnModelCreating(DbModelBuilder)&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt; color: black;"&gt; method on DbContext is overridden to modify the model using fluent API.) The &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg696460%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;System.Data.Entity.DbSet&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; properties &lt;/span&gt;&lt;span style="font-size: 12pt; color: black;"&gt;are automatically initialized when the instance of the derived DbContext class is created.&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;To create a class library project&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Open Visual Studio 2010. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;On the File menu, point to New, and then click Project.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;In the left pane, click Other Languages -&amp;gt; Visual Basic, and then select the Console Application template.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Enter CodeFirstVBSample as the project and solution name. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Add references for the EntityFramework.dll and System.ComponentModel.DataAnnotations.dll assemblies. These assemblies contain types that will be used in this walkthrough.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;To define the School model using Code First development&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Add a new class to the CodeFirstVBSample project. Enter SchoolModel for the class name.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Replace the code generated by default with the following code that defines the model. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Me&lt;/span&gt;.Courses = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Primary key&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; DepartmentID() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Name() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Budget() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Decimal&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; StartDate() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Date&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Administrator() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;?&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Courses() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ICollection&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Me&lt;/span&gt;.Instructors = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;HashSet&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt;)()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Primary key&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; CourseID() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Title() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Credits() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: #2b91af;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: #2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: green;"&gt;' Foreign&amp;nbsp; key that does not follow the Code First convention. &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: green;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' The fluent API will be used to configure DepartmentID_FK&amp;nbsp; to be the foreign key for this entity. &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; DepartmentID_FK() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Navigation properties&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Department() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Instructors() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ICollection&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnlineCourse&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; URL() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Partial&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourse&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Details = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourseDetails&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Details() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourseDetails&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: green;"&gt;' Complex type&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourseDetails&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Time() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Date&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Location() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Days() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Primary key&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; PersonID() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; LastName() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; FirstName() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Me&lt;/span&gt;.Courses = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; HireDate() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Date&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Navigation properties&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Private&lt;/span&gt; privateCourses &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ICollection&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Courses() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ICollection&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; OfficeAssignment() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OfficeAssignment&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OfficeAssignment&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: #2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: green;"&gt;' Primary key that does not follow the Code First convention. &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: green;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' The HasKey method is used later to configure the primary key for the entity. &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; InstructorID() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Location() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Timestamp() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Byte&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Navigation property&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Overridable&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Instructor() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;To define a derived DbContext class that supports the School model&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Add a new class to the CodeFirstVBSample project. Enter SchoolContext for the class name.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Copy the following code that defines the context class and replace the SchoolContext class definition. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Imports&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; System.Data.Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Imports&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; System.Data.Entity.Infrastructure&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Imports&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; System.Data.Entity.ModelConfiguration.Conventions&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: green;"&gt;' add a reference to System.ComponentModel.DataAnnotations DLL&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Imports&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; System.ComponentModel.DataAnnotations&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContext&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DbContext&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; OfficeAssignments() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DbSet&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OfficeAssignment&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Instructors() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DbSet&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Courses() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DbSet&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Property&lt;/span&gt; Departments() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DbSet&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Protected&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; OnModelCreating(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; modelBuilder &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DbModelBuilder&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;To override the default Code First conventions using the fluent API&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; line-height: 115%;"&gt;This section demonstrates how to use the fluent APIs to configure types to tables mapping, properties to columns mapping, and relationships between tables\type in your model.&amp;nbsp; The fluent API is exposed through the &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbmodelbuilder%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; line-height: 115%; color: windowtext; text-decoration: none;"&gt;DbModelBuilder&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt; line-height: 115%;"&gt; type and is most commonly accessed by overriding the &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.onmodelcreating%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; line-height: 115%; color: windowtext; text-decoration: none;"&gt;OnModelCreating&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt; line-height: 115%;"&gt; method on &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; line-height: 115%; color: windowtext; text-decoration: none;"&gt;DbContext&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt; line-height: 115%;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Copy the following code and add it to the OnModelCreating method defined on the SchoolContext class. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin-left: 0.5in; line-height: 140%; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; line-height: 140%;"&gt;Comments explain what each mapping does.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Configure Code First to ignore PluralizingTableName convention&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If you keep this convention then the generated tables &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' will have pluralized names.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Conventions.Remove(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;PluralizingTableNameConvention&lt;/span&gt;)()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Specifying that a Class is a Complex Type&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The model defined in this topic defines a type OnsiteCourseDetails. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' By convention, a type that has no primary key specified &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' is treated as a complex type. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' There are some scenarios where Code First will not &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' detect a complex type (for example, if you do have a property &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' called ID, but you do not mean for it to be a primary key). &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' In such cases, you would use the fluent API to &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' explicitly specify that a type is a complex type. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.ComplexType(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourseDetails&lt;/span&gt;)()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Mapping a CLR Entity Type to a Specific Table in the Database.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' All properties of OfficeAssignment will be mapped &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' to columns&amp;nbsp; in a table called t_OfficeAssignment.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OfficeAssignment&lt;/span&gt;)().ToTable(&lt;span style="color: #a31515;"&gt;"t_OfficeAssignment"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Mapping the Table-Per-Hierarchy (TPH) Inheritance&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' In the TPH mapping scenario, all types in an inheritance hierarchy &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' are mapped to a single table. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' A discriminator column is used to identify the type of each row. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' When creating your model with Code First,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' TPH is the default strategy for the types that &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' participate in the inheritance hierarchy. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' By default, the discriminator column is added &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' to the table with the name &amp;ldquo;Discriminator&amp;rdquo; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' and the CLR type name of each type in the hierarchy &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' is used for the discriminator values. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' You can modify the default behavior by using the fluent API.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;)(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Requires(&lt;span style="color: #a31515;"&gt;"Type"&lt;/span&gt;).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasValue(&lt;span style="color: #a31515;"&gt;"Person"&lt;/span&gt;)).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt;)(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Requires(&lt;span style="color: #a31515;"&gt;"Type"&lt;/span&gt;).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasValue(&lt;span style="color: #a31515;"&gt;"Instructor"&lt;/span&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Mapping the Table-Per-Type (TPT) Inheritance&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' In the TPT mapping scenario, all types are mapped to individual tables. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Properties that belong solely to a base type or derived type are stored &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' in a table that maps to that type. Tables that map to derived types &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' also store a foreign key that joins the derived table with the base table. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)().ToTable(&lt;span style="color: #a31515;"&gt;"Course"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourse&lt;/span&gt;)().ToTable(&lt;span style="color: #a31515;"&gt;"OnsiteCourse"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnlineCourse&lt;/span&gt;)().ToTable(&lt;span style="color: #a31515;"&gt;"OnlineCourse"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Configuring a Primary Key&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If your class defines a property whose name is &amp;ldquo;ID&amp;rdquo; or &amp;ldquo;Id&amp;rdquo;, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' or a class name followed by &amp;ldquo;ID&amp;rdquo; or &amp;ldquo;Id&amp;rdquo;, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' the Entity Framework treats this property as a primary key by convention. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If your property name does not follow this pattern, use the HasKey method &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' to configure the primary key for the entity. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OfficeAssignment&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HasKey(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.InstructorID)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Specifying the Maximum Length on a Property&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' In the following example, the Name property &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' should be no longer than 50 characters. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If you make the value longer than 50 characters, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' you will get a DbEntityValidationException exception.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Name).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasMaxLength(60)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Configuring the Property to be Required&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' In the following example, the Name property is required. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If you do not specify the Name, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' you will get a DbEntityValidationException exception. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The database column used to store this property will be non-nullable.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Name).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;IsRequired()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Switching off Identity for Numeric Primary Keys&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The following example sets the DepartmentID property to &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' the value will not be generated by the database.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.CourseID).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasDatabaseGeneratedOption(&lt;span style="color: #2b91af;"&gt;DatabaseGeneratedOption&lt;/span&gt;.None)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;'Specifying NOT to Map a CLR Property to a Column in the Database&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ignore(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Administrator)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;'Mapping a CLR Property to a Specific Column in the Database&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Budget).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasColumnName(&lt;span style="color: #a31515;"&gt;"DepartmentBudget"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;'Configuring the Data Type of a Database Column&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Name).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasColumnType(&lt;span style="color: #a31515;"&gt;"varchar"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;'Configuring Properties on a Complex Type&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourse&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Details.Days).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasColumnName(&lt;span style="color: #a31515;"&gt;"Days"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourse&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Details.Location).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasColumnName(&lt;span style="color: #a31515;"&gt;"Location"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourse&lt;/span&gt;)().Property(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Details.Time).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasColumnName(&lt;span style="color: #a31515;"&gt;"Time"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Map one-to-zero or one relationship&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The OfficeAssignment has the InstructorID &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' property that is a primary key and a foreign key.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OfficeAssignment&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasRequired(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Instructor).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WithOptional(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.OfficeAssignment)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Configuring a Many-to-Many Relationship&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The following code configures a many-to-many relationship &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' between the Course&amp;nbsp; and Instructor types. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' In the following example, the default Code First conventions &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' are used&amp;nbsp; to create a join table.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' As a result the CourseInstructor table is created with &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Course_CourseID&amp;nbsp; and Instructor_InstructorID columns. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasMany(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Instructors).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WithMany(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Courses)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Configuring a Many-to-Many Relationship and specifying the names &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' of the columns in the join table&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If you want to specify the join table name &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' and the names of the columns in the table &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' you need to do additional configuration by using the Map method. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The following code generates the CourseInstructor &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' table with CourseID and InstructorID columns.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasMany(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Instructors).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WithMany(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Courses).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Map(&lt;span style="color: blue;"&gt;Sub&lt;/span&gt;(m)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m.ToTable(&lt;span style="color: #a31515;"&gt;"CourseInstructor"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m.MapLeftKey(&lt;span style="color: #a31515;"&gt;"CourseID"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m.MapRightKey(&lt;span style="color: #a31515;"&gt;"InstructorID"&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Configuring a foreign key name that does not follow the Code First convention&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The foreign key property on the Course class is called DepartmentID_FK&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' since that does not follow Code First conventions you need to explicitly specify&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' that you want DepartmentID_FK to be the foreign key.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasRequired(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Department).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WithMany(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Courses).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasForeignKey(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.DepartmentID_FK)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Enabling Cascade Delete&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' By default, if a foreign key on the dependent entity is not nullable, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' then Code First sets cascade delete on the relationship. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' If a foreign key on the dependent entity is nullable, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Code First does not set cascade delete on the relationship, &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' and when the principal is deleted the foreign key will be set to null. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The following code configures cascade delete on the relationship.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' You can also remove the cascade delete conventions by using: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' modelBuilder.Conventions.Remove&amp;lt;OneToManyCascadeDeleteConvention&amp;gt;() &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' and modelBuilder.Conventions.Remove&amp;lt;ManyToManyCascadeDeleteConvention&amp;gt;().&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modelBuilder.Entity(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Course&lt;/span&gt;)().&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasRequired(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Department).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WithMany(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(t) t.Courses).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HasForeignKey(&lt;span style="color: blue;"&gt;Function&lt;/span&gt;(d) d.DepartmentID_FK).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WillCascadeOnDelete(&lt;span style="color: blue;"&gt;False&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 9pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;To configure database creation and initialization strategy&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;In this step you will define a class (we will call it SchoolContextInitializer) that handles database creation and initialization strategy. The database initialization strategy is specified for the derived context type. This occurs only the first time the context is used in the application domain. To set the database initialization strategy call&lt;/span&gt;&lt;span style="font-size: 12pt; color: black;"&gt; &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg679461%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;Database.SetInitializer&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt; color: black;"&gt; with an &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg696323%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;IDatabaseinitializer&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt; color: black;"&gt; instance.&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;The SchoolContextInitializer class derives from System.Data.Entity.DropCreateDatabaseIfModelChanges, an implementation of &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg696323%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;IDatabaseInitializer(Of TContext)&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt;. When using this strategy, the database will be re-created and re-seeded with data only when the model changes (for example, if a property is added to an entity, or a fluent API is used to change configuration). Override the &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg679410%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;Seed&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; method to add data to the context and then persist this data to the database during the initialization. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;To instantiate this class in your program use:&lt;/span&gt;&lt;span style="font-size: 9pt; font-family: 'Verdana','sans-serif'; color: black;"&gt; &lt;/span&gt;&lt;code&gt;&lt;span style="color: black;"&gt;Database.SetInitializer&amp;lt;SchoolContext&amp;gt;(new SchoolContextInitializer()))&lt;/span&gt;&lt;/code&gt;&lt;span style="font-size: 9pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;Other initialization strategies available in the Entity Framework 4.1 are &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg679221%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;CreateDatabaseIfNotExists&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; and &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg679506%28v=VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt;"&gt;DropDatabaseAlways&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt;. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Add a new class to the CodeFirstVBSample project. Enter SchoolContextInitializer for the class name.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Copy and paste the following SchoolContextInitializer class definition. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Imports&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; System.Data.Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContextInitializer&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DropCreateDatabaseIfModelChanges&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContext&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Protected&lt;/span&gt; &lt;span style="color: blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Seed(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; context &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContext&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; english = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Department&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt; {&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Name = &lt;span style="color: #a31515;"&gt;"English"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Budget = 120000D,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .StartDate = &lt;span style="color: blue;"&gt;Date&lt;/span&gt;.Parse(&lt;span style="color: #a31515;"&gt;"2007-09-01"&lt;/span&gt;)}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.Departments.Add(english)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; onlineCourses = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnlineCourse&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt; {&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CourseID = 1,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Title = &lt;span style="color: #a31515;"&gt;"Composition"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Credits = 4,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Department = english,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .URL = &lt;span style="color: #a31515;"&gt;"http://www.fineartschool.net/Composition"&lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.Courses.Add(onlineCourses)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; onsiteCourses = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourse&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt; {&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CourseID = 3,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Title = &lt;span style="color: #a31515;"&gt;"Poetry"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Credits = 4,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Department = english,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Details = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;OnsiteCourseDetails&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt; {&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Days = &lt;span style="color: #a31515;"&gt;"MTW"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Location = &lt;span style="color: #a31515;"&gt;"123 Smith"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Time = &lt;span style="color: blue;"&gt;Date&lt;/span&gt;.Parse(&lt;span style="color: #a31515;"&gt;"11:30"&lt;/span&gt;)}}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.Courses.Add(onsiteCourses)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.SaveChanges()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;To retrieve and persist data&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;In this section you will add the code that queries the School model, updates entities, and saves the data to the database.&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;The DbSet properties on the DbContext derived class represent a starting query that returns all entities of the specified type. &lt;/span&gt;&lt;span style="font-size: 10.5pt; font-family: 'Segoe UI','sans-serif'; color: black;"&gt;This query can be further refined by using LINQ to Entities methods (f&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;or example,&lt;/span&gt;&lt;span style="font-size: 9pt; font-family: Consolas;"&gt; &lt;/span&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas; color: blue;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 10.5pt; font-family: Consolas;"&gt; query = &lt;span style="color: blue;"&gt;From&lt;/span&gt; d &lt;span style="color: blue;"&gt;In&lt;/span&gt; context.Departments &lt;span style="color: blue;"&gt;Where&lt;/span&gt; d.Name = &lt;span style="color: #a31515;"&gt;"English"&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; d). &lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;A query is executed when: it is enumerated by a &lt;b&gt;For Each&lt;/b&gt; statement; it is enumerated by a collection operation such as &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb298736%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;ToArray&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt;, &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.todictionary%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;ToDictionary&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt;, or &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb342261%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;ToList&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt;; LINQ operators such as &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb291976%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;First&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; or &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb337697%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;Any&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; are specified in the outermost part of the query; the &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbextensions.load%28VS.103%29.aspx"&gt;&lt;span style="font-size: 12pt; color: windowtext; text-decoration: none;"&gt;Load&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 12pt;"&gt; extension method is called on DbSet.&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Open the Module1.vb file where the Main function is defined.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Copy and paste the following Module1 definition. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Imports&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; System.Data.Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;Module&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: #2b91af;"&gt;Module1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Main()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="color: green;"&gt;' Configure School database creation and initialization Strategy.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' The initialization strategy is called the first time &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' the context is used in the application domain. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Database&lt;/span&gt;.SetInitializer(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContext&lt;/span&gt;)(&lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContextInitializer&lt;/span&gt;())&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Using&lt;/span&gt; context &lt;span style="color: blue;"&gt;As New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SchoolContext&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Add instructors to courses.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; englishInstructor = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Instructor&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt; {&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PersonID = 1,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.LastName = &lt;span style="color: #a31515;"&gt;"Abercrombie"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .FirstName = &lt;span style="color: #a31515;"&gt;"Kim"&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .HireDate = &lt;span style="color: blue;"&gt;Date&lt;/span&gt;.Parse(&lt;span style="color: #a31515;"&gt;"1995-03-11"&lt;/span&gt;)}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; englishDepartment = ( &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;From&lt;/span&gt; d &lt;span style="color: blue;"&gt;In&lt;/span&gt; context.Departments &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Where&lt;/span&gt; d.Name = &lt;span style="color: #a31515;"&gt;"English"&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; d).FirstOrDefault()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;For&lt;/span&gt; &lt;span style="color: blue;"&gt;Each&lt;/span&gt; c &lt;span style="color: blue;"&gt;In&lt;/span&gt; englishDepartment.Courses()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c.Instructors.Add(englishInstructor)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Next&lt;/span&gt; c&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;' Save new entities.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; recordsAffected &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt; = context.SaveChanges()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;'Get the courses where the instructor is Abercrombie Kim&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; instructor = ( &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;From&lt;/span&gt; i &lt;span style="color: blue;"&gt;In&lt;/span&gt; context.Instructors &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;Where&lt;/span&gt; i.FirstName = &lt;span style="color: #a31515;"&gt;"Kim"&lt;/span&gt; &lt;span style="color: blue;"&gt;AndAlso&lt;/span&gt; i.LastName = &lt;span style="color: #a31515;"&gt;"Abercrombie"&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Select&lt;/span&gt; i).FirstOrDefault()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;For&lt;/span&gt; &lt;span style="color: blue;"&gt;Each&lt;/span&gt; c &lt;span style="color: blue;"&gt;In&lt;/span&gt; instructor.Courses()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(c.Title)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Using&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New';"&gt; &lt;span style="color: blue;"&gt;Module&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 9pt; font-family: Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;Compile and run the application.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;When you ran the application, the CodeFirstVBSample.SchoolContext database was created and you saw the data in the console window because SchoolContextInitializer seeded the database with data. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;The Entity Framework used the default conventions to create the database on the localhost\SQLEXPRESS instance and names it after the fully qualified type name of the derived context (CodeFirstVBSample.SchoolContext). When the application is run subsequent times, unless the model changes, the existing database will be used. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;If the model changes and you do not set the initializer, you will get the following exception: &amp;ldquo;The model backing the '&lt;i&gt;ContextName&lt;/i&gt;&amp;rsquo; context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;To override the default Code First conventions to set the database name&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;One way to specify the database name for a new or existing database is to add an App.config or Web.config file that contains the connection string with the same name as your context type. The .config file should be added to the project that contains the executable assembly.&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Add a new app.config file to the CodeFirstVBSample project. To do this: right-click the project, click Add, and then click New Item; select General and then Application Configuration File.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;Copy and paste the following connectionStrings element as a child of configuration element in the app.config file that you just added. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;&amp;lt;connectionStrings&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;&amp;nbsp; &amp;lt;add&amp;nbsp;&amp;nbsp; name="SchoolContext" providerName="System.Data.SqlClient"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; connectionString="Server=.\SQLEXPRESS;Database=School; Trusted_Connection=true;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Integrated Security=True;MultipleActiveResultSets=True"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 0.0001pt; line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;&amp;lt;/connectionStrings&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;br /&gt;
&lt;p style="margin-left: 0.75in; text-indent: -0.25in; line-height: normal; vertical-align: top;" class="MsoListParagraph"&gt;&lt;span style="font-size: 8pt; font-family: 'Courier New'; color: blue;"&gt;&lt;/span&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="font-size: 12pt;"&gt;Compile and run the application.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.75in; text-indent: -0.25in; line-height: normal; vertical-align: top;" class="MsoListParagraph"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;br style="font-size: 12pt;" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.75in; text-indent: -0.25in; line-height: normal; vertical-align: top;" class="MsoListParagraph"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.75in; text-indent: -0.25in; line-height: normal; vertical-align: top;" class="MsoListParagraph"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;/p&gt;
&lt;p style="margin-left: 0.25in; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;When you ran the application, the School database was created. Unless you change your model the subsequent execution of this application will be mapping to the existing School database. &lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-size: 12pt; font-family: 'Verdana','sans-serif'; color: black;"&gt;Summary&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt;"&gt;In this post we demonstrated how to create a model using Code First development using VB.NET. It also shows how to configure mapping between the conceptual model and database schema.&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 12pt; font-family: 'Times New Roman','serif';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 9pt; font-family: 'Segoe UI','sans-serif'; color: #333333;"&gt;Julia Kornich&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal;" class="MsoNormal"&gt;&lt;span style="font-size: 9pt; font-family: 'Segoe UI','sans-serif'; color: #333333;"&gt;Programming Writer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 7.5pt; line-height: normal; vertical-align: top;" class="MsoNormal"&gt;&amp;nbsp;&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=10210159" width="1" height="1"&gt;</description><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/Code+First/">Code First</category></item><item><title>Annual SQL Connectivity Customer Survey [Date extended]</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/07/annual-sql-connectivity-customer-survey-date-extended.aspx</link><pubDate>Thu, 08 Sep 2011 05:19:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10207656</guid><dc:creator>dpblogs</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10207656</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/07/annual-sql-connectivity-customer-survey-date-extended.aspx#comments</comments><description>&lt;p&gt;Greetings to the Developer community:&lt;/p&gt;
&lt;p&gt;Thank you very much for an overwhelming response on our survey. Feedback is continue to pour. We are extending the deadline to complete the survey to September 16&lt;sup&gt;th&lt;/sup&gt;, 2011 to allow everyone to provide input. Your feedback helps us on the broad SQL Connectivity components that address major development platforms, including ODBC, ADO.NET, JDBC and PHP. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;We view your organization as a key stakeholder in the process that we have to identify areas for future investments. The feedback you provide is valuable and each response will be read and will be treated with utmost confidence. The survey can be found in the link below and will be available until 16&lt;sup&gt;th&lt;/sup&gt; &amp;nbsp;September, 2011 5:00 PM PST.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.zoomerang.com/Survey/WEB22CS45XT9FE/"&gt;http://www.zoomerang.com/Survey/WEB22CS45XT9FE/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Luiz Santos&lt;/p&gt;
&lt;p&gt;ADO.NET Program Manager&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #1f497d; line-height: 115%; font-family: 'Calibri','sans-serif'; font-size: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&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=10207656" width="1" height="1"&gt;</description></item><item><title>Code First Migrations: Alpha 2 Released</title><link>http://blogs.msdn.com/b/adonet/archive/2011/09/06/code-first-migrations-alpha-2-released.aspx</link><pubDate>Tue, 06 Sep 2011 16:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10206826</guid><dc:creator>dpblogs</dc:creator><slash:comments>17</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/adonet/rsscomments.aspx?WeblogPostID=10206826</wfw:commentRss><comments>http://blogs.msdn.com/b/adonet/archive/2011/09/06/code-first-migrations-alpha-2-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;Six weeks back we released the first preview of Code First Migrations. This preview was called ‘Code First Migrations August 2011 CTP’. However, after some &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/09/ef-releases-amp-versioning-call-for-feedback.aspx"&gt;changes to how we name our releases&lt;/a&gt; it is best to think of that release as ‘Code First Migrations Alpha 1’. Today we are happy to announce that ‘Code First Migrations Alpha 2’ is available.&lt;/p&gt;  &lt;p&gt;Alpha 2 is still is primarily focused on the developer experience inside of Visual Studio. We realize this isn’t the only area in which migrations is important and our team is also working on scenarios including team build, deployment and invoking migrations from outside of Visual Studio as well as from custom code.&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;em&gt; If you have received the following error when trying to use Code First Migrations Alpha 2 it is most likely due to bugs that were causing a dependency on SQL Compact and a dependency on a local SQL Express instance.&lt;strong&gt; We have released an updated package that resolves both these issues&lt;/strong&gt;, please ensure you have updated the EntityFramework.Migrations package to version 0.6.2. You will need to close and re-open Visual Studio after updating the package, failure to do so will result in an error stating that “&lt;font color="#ff0000"&gt;The project 'Demo' does not contain any migration contexts&lt;/font&gt;”. If you continue to get errors after updating to the latest package and re-starting Visual Studio then please start a thread in our &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads"&gt;Pre-Release Forum&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;Add-Migration : Exception has been thrown by the target of an invocation.        &lt;br /&gt;At line:1 char:14         &lt;br /&gt;+ Add-Migration &amp;lt;&amp;lt;&amp;lt;&amp;lt; Init2         &lt;br /&gt;+ CategoryInfo : NotSpecified: (:) [Add-Migration], TargetInvocationException         &lt;br /&gt;+ FullyQualifiedErrorId : System.Reflection.TargetInvocationException,System.Data.Entity.Migrations.AddMigrationCommand&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;hr /&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What Changed&lt;/h2&gt;  &lt;p&gt;The short answer… a lot! You gave us a lot of feedback on our first alpha and we summarized it in the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/08/04/code-first-migrations-your-feedback.aspx"&gt;'Code First Migrations: Your Feedback'&lt;/a&gt; blog post. The changes are pretty widespread so we would recommend reading the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/06/code-first-migrations-alpha-2-walkthrough.aspx"&gt;Code First Migrations Alpha 2 Walkthrough&lt;/a&gt; to get an overview of the new functionality. Here are some of the more notable changes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;We did a lot of work to &lt;strong&gt;improve the ‘no-magic’ workflow&lt;/strong&gt;, including a code based format for expressing migrations. Our walkthrough also starts with the no-magic approach and then shows how to transition to a more-magic approach for folks who want to use it. &lt;/li&gt;    &lt;li&gt;A &lt;strong&gt;provider model is now available&lt;/strong&gt;. Providers need to inherit from the &lt;span style="color: #c0504d" color="#c0504d"&gt;System.Data.Entity.Migrations.Providers.DbMigrationSqlGenerator&lt;/span&gt; base class and are registered in a new &lt;span style="color: #c0504d" color="#c0504d"&gt;Settings&lt;/span&gt; class that is added when you install the NuGet package. Alpha 2 includes a provider for SQL Server (including SQL Azure) and SQL Compact. &lt;/li&gt;    &lt;li&gt;We’ve taken a &lt;strong&gt;NuGet package name change&lt;/strong&gt;. Alpha 1 was released as EntityFramework.SqlMigrations, but given the design changes that name makes less sense. We have now dropped the ‘Sql’ and are now just &lt;a href="http://nuget.org/List/Packages/EntityFramework.Migrations"&gt;EntityFramework.Migrations&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;We’ve &lt;strong&gt;improved how we do type resolution&lt;/strong&gt; to find your context etc. In the first preview there were a number of issues around how we discovered your context, especially in solutions with multiple projects. This would result in various errors when trying to run either of the migration commands. We’ve done some work to make this more robust in Alpha 2. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Feedback&lt;/h2&gt;  &lt;p&gt;We really want your feedback on this work so please download it, use it and let us know what you like and what you would like to see change. You can provide feedback by commenting on this blog post.&lt;/p&gt;  &lt;p&gt;The key areas we really want your feedback on are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Shape of the API for expressing migrations&lt;/strong&gt;. The API for migrations isn’t complete yet but Alpha 2 contains a large part of what we expect the final API to look like. We’d love your feedback on whether we are heading down the right path. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Will you use the automatic migrations functionality?&lt;/strong&gt; We support a purely imperative workflow where all changes are explicitly expressed in code. A lot of folks have told us that is how they want to work. We want to know if we should continue to invest in the option of having sections of the migrations process handled automatically. &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;The &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/09/06/code-first-migrations-alpha-2-walkthrough.aspx"&gt;Code First Migrations Alpha 2 Walkthrough&lt;/a&gt; gives an overview of migrations and how to it for development.&lt;/p&gt;  &lt;p&gt;Code First Migrations Alpha 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.Migrations"&gt;EntityFramework.Migrations&lt;/a&gt; package.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Known Issues &amp;amp; Limitations&lt;/h2&gt;  &lt;p&gt;We really wanted to get your feedback on these design changes so Alpha 2 hasn’t been polished and isn’t a complete set of the features we intend to ship.&lt;/p&gt;  &lt;p&gt;Some known issues, limitations and ‘yet to be implemented’ features include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;You must start using migrations targeting an empty database that contains no tables or targeting a database that does not yet exist. &lt;/strong&gt;Code First Migrations assumes that it is starting against an empty database. If you target a database that already contains some/all of the tables for your model then you will get an error when migrations tries to recreate the same tables. We are currently working on a boot strapping feature that will allow you to start using migrations against an existing schema. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;VB.Net is not yet supported. &lt;/strong&gt;Migrations can currently only be scaffolded in C#, we are adding VB.Net code generation at the moment and will release another Alpha shortly that includes VB.Net support. You can, however, use a C# project to work with migrations that reference a domain model and context that are defined in a separate VB.Net project(s). &lt;/li&gt;    &lt;li&gt;There is an &lt;strong&gt;issue with using Code First Migrations in a team environment&lt;/strong&gt; that we are currently working through. Code First Migrations generates some additional metadata alongside each migration that captures the source and target model for the migration. The metadata is used to replicate any automatic migrations between each code-based migration. If the source model of a migration matches the target model of the previous migration then the automatic pipeline is skipped. The issue arises when two developers add migrations locally and then check-in. Because the metadata of their migrations are captured during scaffolding they don’t reflect the model changes made by the other developer, even if the second developer to check-in sync’s before committing their changes. This can cause the automatic pipeline to kick in when the migrations are run. We realize this is the key team development scenario and are working to resolve it at the moment. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;No outside-of-Visual-Studio experience&lt;/strong&gt;. Alpha 2 only includes the Visual Studio integrated experience. We also plan to deliver a command line tool and an MSDeploy provider for Code First Migrations. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Downgrade is currently not supported&lt;/strong&gt;. When generating migrations you will notice that the Down method is empty, we will support down migration soon. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Migrate to a specific version is not supported.&lt;/strong&gt; You can currently only migrate to the latest version, we will support migrations up/down to any version soon. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Prerequisites &amp;amp; Incompatibilities&lt;/h2&gt;  &lt;p&gt;Migrations is dependent on &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;, this updated release of EF 4.1 will be automatically installed when you install the EntityFramework.Migrations package.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Important: &lt;/strong&gt;&lt;em&gt;If you have previously run the EF 4.1 stand alone installer you will need to &lt;/em&gt;&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=26825"&gt;&lt;em&gt;upgrade&lt;/em&gt;&lt;/a&gt;&lt;em&gt; or remove the installation before using migrations. This is required because the installer adds the EF 4.1 assembly to the Global Assembly Cache (GAC) causing the original RTM version to be used at runtime rather than Update 1.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;This release is incompatible with “Microsoft Data Services, Entity Framework, and SQL Server Tools for Data Framework June 2011 CTP”.&lt;/p&gt;  &lt;p&gt;&amp;#160;&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;#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=10206826" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-20-68-26/License.rtf" length="151623" 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>
