<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Code-based_Migration</id>
		<title>Code-based Migration - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Code-based_Migration"/>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Code-based_Migration&amp;action=history"/>
		<updated>2026-04-17T19:33:22Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Code-based_Migration&amp;diff=131&amp;oldid=prev</id>
		<title>Dt1nh6: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Code-based_Migration&amp;diff=131&amp;oldid=prev"/>
				<updated>2016-05-09T13:27:21Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 13:27, 9 May 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan='2' style='text-align: center;' lang='en'&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Dt1nh6</name></author>	</entry>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Code-based_Migration&amp;diff=130&amp;oldid=prev</id>
		<title>Macrop at 10:51, 30 December 2014</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Code-based_Migration&amp;diff=130&amp;oldid=prev"/>
				<updated>2014-12-30T10:51:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:MVC]]&lt;br /&gt;
[[Category:SQL]]&lt;br /&gt;
[[Category:ASP.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Entity Framework]]&lt;br /&gt;
[[Category:CodeFirst]]&lt;br /&gt;
&lt;br /&gt;
Code based migration is useful when you want more control on migration i.e. set default value of the column etc.&lt;br /&gt;
&lt;br /&gt;
=== Code-First has two commands for code based migration: ===&lt;br /&gt;
&lt;br /&gt;
Add-migration: It will scaffold the next migration for the changes you have made to your domain classes&lt;br /&gt;
Update-database: It will apply pending changes to the database based on latest scaffolding code file you create using &amp;quot;Add-Migration&amp;quot; command&lt;br /&gt;
Assume that you have Student and Course entity classes initially and you want to use code based migration for your application. So before running commands above, you must enable migration for your application, by using the enable-migrations commands. These are in package manger that we used previously for automatic migration. This will create a configuration file, as was the case with automated migration. Also, you need to set the database initializer in the context class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:c-sharp;&amp;quot;&amp;gt;     &lt;br /&gt;
public class SchoolDBContext: DbContext &lt;br /&gt;
    {&lt;br /&gt;
        public SchoolDBContext(): base(&amp;quot;SchoolDBConnectionString&amp;quot;) &lt;br /&gt;
        {&lt;br /&gt;
            Database.SetInitializer(new MigrateDatabaseToLatestVersion&amp;lt;SchoolDBContext, SchoolDataLayer.Migrations.Configuration&amp;gt;(&amp;quot;SchoolDBConnectionString&amp;quot;));&lt;br /&gt;
            &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;Student&amp;gt; Students { get; set; }&lt;br /&gt;
        public DbSet&amp;lt;Course&amp;gt; Courses { get; set; }&lt;br /&gt;
        &lt;br /&gt;
        protected override void OnModelCreating(DbModelBuilder modelBuilder)&lt;br /&gt;
        {&lt;br /&gt;
&lt;br /&gt;
            base.OnModelCreating(modelBuilder);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, you have to create a scaffold code file which consists of your database requirement from your existing domain classes. You can do this by running the “add-migration&amp;quot; command in the package manger. (from Tools → Library Package Manager → Package Manager Console). You will have to pass the name parameter, which will be part of code file name.&lt;br /&gt;
&lt;br /&gt;
=== Add-Migration command Syntax: ===&lt;br /&gt;
    &lt;br /&gt;
    Add-Migration [-Name] &amp;lt;String&amp;gt; [-Force]&lt;br /&gt;
      [-ProjectName &amp;lt;String&amp;gt;] [-StartUpProjectName &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-ConfigurationTypeName &amp;lt;String&amp;gt;] [-ConnectionStringName &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-IgnoreChanges] [&amp;lt;CommonParameters&amp;gt;]&lt;br /&gt;
 &lt;br /&gt;
    Add-Migration [-Name] &amp;lt;String&amp;gt; [-Force]&lt;br /&gt;
      [-ProjectName &amp;lt;String&amp;gt;] [-StartUpProjectName &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-ConfigurationTypeName &amp;lt;String&amp;gt;] -ConnectionString &amp;lt;String&amp;gt;&lt;br /&gt;
      -ConnectionProviderName &amp;lt;String&amp;gt; [-IgnoreChanges] [&amp;lt;Common Parameters&amp;gt;]&lt;br /&gt;
        &lt;br /&gt;
You can see that this command has created a new file in the Migration folder with the name parameter you passed to the command with a timestamp prefix:&lt;br /&gt;
&lt;br /&gt;
[[File:Codebased-fg2.PNG]]&lt;br /&gt;
&lt;br /&gt;
After creating the file above using the add-migration command, you have to update the database. You can create or update the database using the “update-database” command. You can use –verbose to see what’s going on in the database:&lt;br /&gt;
&lt;br /&gt;
=== Update-Database command syntax: ===&lt;br /&gt;
    &lt;br /&gt;
    Update-Database [-SourceMigration &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-TargetMigration &amp;lt;String&amp;gt;] [-Script] [-Force] [-ProjectName &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-StartUpProjectName &amp;lt;String&amp;gt;] [-ConfigurationTypeName &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-ConnectionStringName &amp;lt;String&amp;gt;] [&amp;lt;CommonParameters&amp;gt;]&lt;br /&gt;
 &lt;br /&gt;
    Update-Database [-SourceMigration &amp;lt;String&amp;gt;] [-TargetMigration &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-Script] [-Force] [-ProjectName &amp;lt;String&amp;gt;] [-StartUpProjectName &amp;lt;String&amp;gt;]&lt;br /&gt;
      [-ConfigurationTypeName &amp;lt;String&amp;gt;] -ConnectionString &amp;lt;String&amp;gt;&lt;br /&gt;
      -ConnectionProviderName &amp;lt;String&amp;gt; [&amp;lt;CommonParameters&amp;gt;]&lt;br /&gt;
        &lt;br /&gt;
At this point, the database will be created or updated.&lt;br /&gt;
&lt;br /&gt;
Now suppose you added more domain classes. So before running the application, you have to create a scaffold file for new classes, by executing the &amp;quot;Add-Migration&amp;quot; command. Once it creates the file, update the database using the Update-Database command. So in this way you have to repeat the Add-Migration and Update-Database command each time you make any changes in your domain classes.&lt;br /&gt;
&lt;br /&gt;
=== Rollback Database change: ===&lt;br /&gt;
Suppose you want to roll back the database schema to any of the previous states, then you can use Update-database command with –TargetMigration parameter as shown below: &lt;br /&gt;
&lt;br /&gt;
update-database -TargetMigration:&amp;quot;First School DB schema&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;quot;get-migration&amp;quot; command to see what migration has been applied.&lt;br /&gt;
&lt;br /&gt;
Note: Use the &amp;quot;get-help&amp;quot; command for add-migration and update-database command in order to see what are the parameters we can pass with this command.&lt;/div&gt;</summary>
		<author><name>Macrop</name></author>	</entry>

	</feed>