How do I allow the EF4 CodeFirst database initializer to run under development, but not run in production

后端 未结 5 652
有刺的猬
有刺的猬 2021-01-01 07:06

I am attempting to deploy my first alpha version of a system online for a few people to start using. On development I make heavy use of the DropCreateDatabaseOnModelC

5条回答
  •  温柔的废话
    2021-01-01 07:18

    If you are using sql express in development and not on your production box you can filter by the connection.

    protected void Application_Start(object sender, EventArgs e)
    {
      using (var db = new MyDb())
      {
        if (db.Database.Connection.DataSource.IndexOf("sqlexpress", StringComparison.InvariantCultureIgnoreCase) > -1)
        {
            Database.SetInitializer(new MyDbInitializer());
        }
      }
    }
    

提交回复
热议问题