2013年5月19日 星期日

Database Initialization/Migration Strategies

[Topic 1]

When we run the application in second time with class change in Visual Studio, we will encounter an error saying that database has been changed.

 

There are four strategies in IDatabaseInitializer

image

Database.SetInitializer()

image

Actually, we can put the code in Application_Start() method of Global.asax file.

 

We have to change the Configuration class to become public since we use it in different project above.

image

 

[Topic 2]

When removing a field in class, we sometimes encounter following error message:

Automatic migration was not applied because it would result in data loss.

We can then add AutomaticMigrationDataLossAllowed = true in Configuration.cs file

image

 

[Topic 3]

Avoid Seed method to run everytime when we run the application. There are couple workaround ways to avoid it, but none of it are perfect.

1. Disalbe DatabaseInitializer by comment out Database.SetInitializer

2. Using complier #if & #endif directive, it will compile the code between the directives only if true or the specified symbol is defined

image

[Ref] http://msdn.microsoft.com/zh-tw/library/4y6tbswk.aspx

 image

沒有留言:

張貼留言