.NET Project created using Entity Framework Code-First should be able to support multiple databases without Code rebuild after changing the Web.config (Connection string) RRS feed

  • Question

  • I have created a project using Entity Framework Code-First Approach. During Development we point the database to development (Connecting string in web.config) and build the code. The same approach we follow it for UAT.

    What happens is after the UAT is successful and when moving to production , I again have to update web.config pointing to production DB and build the code which is time consuming. We usually will not build the code again when we use ADO.Net.

    My question how can I just change the web.config(pointing to production DB) and make the application work.

    Any help is appreciated.


    Monday, March 2, 2015 9:54 AM


  • Hello Abhilash,

    >>My question how can I just change the web.config(pointing to production DB) and make the application work.

    This could be, however, it has some restricts as:

    1.The structures of both your production database and your development database should be the same.

    2.The data stored in the “[Model]” column of [__MigrationHistory] should keep the same, because this column actually represent the .edmx file, for details, you could check this link.

    I do know how you deploy your production DB, my suggestion is that when generating the database, you could use the updata-database –script command used in migration to get the script and run both your development and production environment so that the database could be same even for the data in “Model” column.

    Of course, an additional constructor is needed for specifying which database is used as:

    public partial class MobileServiceContext : DbContext
            public MobileServiceContext()
            public MobileServiceContext(string connectionString)
                : base(connectionString)
            public DbSet<Blog> Blogs { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 3, 2015 3:00 AM