Add test data in MVC for development

Sometimes it is easy to using sample data for MVC development.  Overriding Seed method and re-initializing database will save time for developer comparing with executing Code-First migration. Three steps can achieve the goal.

  1. Create a class “SampleDbContextInitializer” inheriting from  DropCreateDatabaseAlways<SampleDbContext>
  2. Override method Seed. e.g.

protected override void Seed(SampleDbContext context)

var addresses = new List<Address>
new Address() { …},
new Address() { …},
addresses.ForEach(a => context.Addresses.Add(a));



3. Add SampleDbContextInitializer to method Application_Start() in Global.asax.cs

   Database.SetInitializer(new SampleDbContextInitializer ());


Some Tips of Localization MVC5

Some tips of localization on UI of MVC5.

1.Create the resource file(.resx), e.g. “product.resx”, If it is chinese, the file name is “product.zh-CN.resx”.  The initialization of the culture can be called in the overridden method “Application_BeginRequest” of “Global.asax.cs”.

protected void Application_BeginRequest(object sender , EventArgs e)
//var culture = CultureInfo.CurrentUICulture;
var culture = new CultureInfo(“zh-CN”);

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

//Use the same culture for any newly created threads
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

It is not good to code the culture hard. It can be configured by using “CurrentUICulture”, user can switch the language in the browser settings.

2. For the module class, we can use DataAnnotation to simplize localization by using the annotation class. The display of the attribute of the module will be localized.

[Display(Name="Name", ResourceType = typeof(Resources.Products.Product))]
public string Name { get; set; }

3. For the content of HTML tag, it can be localized by calling Resource class.


If the UI in each action is complicated, the directory of resource files should be created as the same structure as the directory “Views” in order to retrieving the variable quickly for each action.


Warning in Add-Migration of EF6 to create a new DataBase

When I use Code-First to create table in a blank Database (without table) in EF6 on SQL Server 2014 with following commands,

PM> Enable-Migrations (somethimes with "-EnableAutomaticMigrations")
PM> Add-Migration
PM> Update-DataBase

in the step “Add-Migration”, the following warning will be displaying always at first time.

“The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running ‘Add-Migration test_db’ again.”

After  Command “Update-Database”, the SQL script can be generated correctly but tables are not created in DB yet.

Re-run “Add-Migration” can solve the problem, but I have not quite understood the problem with SQL Server 2014 by creating tables in a blank DB.

Data Annotation or Fluent API

Data Annotation:

  • Simple for basic scenarios;
  • Integrates with MVC validation;
  • It may support more features in the next version of EF.

Fluent API:

  • Flexibility. Things with Data Annotation is possible with Fluent API. The reverse is not true.
  • Domain class always in separate section;
  • Support  more operations (e.g. advanced inheritance mappings, switching off cascading deletes, etc.)


Create a new Database with Code First

Use an existing Database with Code First

Configuring/Mapping Properties and Types with the Fluent API

Tips: Some information about local DB:

  • (localdb)\ProjectsV12 instance is created by SQL Server Data Tools (SSDT)
  • (localdb)\MSSQLLocalDB is the SQL Server 2014 LocalDB default instance name
  • (localdb)\v11.0 is the SQL Server 2012 LocalDB default instance name