none
Using EF Code-First migrations to update multiple databases with the same DbContext RRS feed

  • Question

  • I have multiple databases that should have the same schema since each is treated as a separate shard of the overall database architecture. Now, I want to define the schema using EF Code-First Migrations. It seems there is no good out of the box support for this.

    Initially, I created a DbContext with a constructor that takes a connection string as a parameter, but I cannot enable migrations unless I define a parameterless constructor. The default constructor is useless in my case as I programmatically decide which db to hit. Ideally, I'd like to update my model once and deploy changes to all databases with ease. How can I accomplish this? Is there any sample code out there?

     
    This seems to be a major roadblock for me right now as I cannot proceed unless I figure out how to keep all dbs in sync.
    Tuesday, May 6, 2014 1:55 AM

Answers

  • Hello,

    >>Ideally, I'd like to update my model once and deploy changes to all databases with ease. How can I accomplish this? Is there any sample code out there?

    Unfortunately, by using the PMC, the code first migration will work for one context and for one database at the same time.

    If you want the code first migration has such a function, you can post your wish to:

    http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions

    You can consider about the automatically migrations, here I made an example, however it needs us to know these database names firstly:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Text;
    
    namespace CF
    {
        class Sample07
        {
            internal void Exectue()
            {
                try
                {
                    string[] DBNames = { "DB1", "DB2" };
    
                    foreach (string DBName in DBNames)
                    {
                        using (Sample07Context db = new Sample07Context(DBName))
                        {
                            var result = db.Blogs.FirstOrDefault();
                        }
                    }
                }
                catch (Exception)
                {
    
                    throw;
                }
            }
        }
    
        public class Sample07Context : DbContext
        {
            private static bool _flag; 
            private static string _connectionStringName;
            public Sample07Context()
                : base(_connectionStringName)
            {
                if (!_flag)
                {
                    Database.SetInitializer<Sample07Context>(new MigrateDatabaseToLatestVersion<Sample07Context, Configuration>());
                    _flag = true;
                }
            }
            public Sample07Context(string connectionStringName)
                : base(connectionStringName)
            {
                _flag = false;
                _connectionStringName = connectionStringName;
                Database.SetInitializer<Sample07Context>(new MigrateDatabaseToLatestVersion<Sample07Context, Configuration>());
            }
    
            public DbSet<Blog> Blogs { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Blog>().HasKey(b => b.BlogID).Property(b => b.BlogID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            }
        }
    
        public class Blog
        {
            public int BlogID { get; set; }
            public string BlogName { get; set; }
            public string URL { get; set; }
        }
    
        public class Configuration : DbMigrationsConfiguration<Sample07Context>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = true;
                AutomaticMigrationDataLossAllowed = true;
            }
        }
    }
    

    In the example, if I add or remove the URL field, both my DB1 and DB2 database will be changed. You can have a try.

    Regards.


    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.



    Wednesday, May 7, 2014 3:45 AM
    Moderator