Code First Migrations & DB2 RRS feed

  • Question

  • I've recently been playing around with EF 4.3 Code First and DB2 (9.7.5), and everything has been going pretty well.

    I then started to play with the Migrations, and that's where the fun really began.

    I've implemented my own MigrationSqlGenerator for DB2, and I was able to get the tables created on the database.

    DB2 has an issue though with the __MigrationHistory table, namely the underscores at the beginning of the name.  I had a stroke of genius, and I decided that since I'm controlling the DDL, I'll just rename it (#MigrationHistory).   Then I ran it and I realized that there is a 'hard-coded' "dbo.__MigrationHistory" reference in the DbMigrator (and its deeper layers).

    Is there a way to change the name (and schema) of the __MigrationHistory either in EF 4.3 or EF5?

    Are there any hidden tricks that I could use (I'm not above using reflection and changing internal values)?

    Thursday, July 12, 2012 1:18 AM


All replies

  • AFAIK, there's no way to modify the name, it is by design.

    Go go Doraemon!

    Monday, July 16, 2012 2:48 AM
  • Schema name can be changed at run time while creating the DBContext object in EF 4.3.

    In the OnModelCreating() event - we need to write this line to change the schema name.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
                //Below line of code is used to ignore the PluralizingTableName convention. It means the generated tables will have pluralized names.

               //In this line I have treated Employee as my entity

       modelBuilder.Entity<Employee>().ToTable("Employee", "<your schema name>");


    This has worked for me!!

    • Proposed as answer by Sharath K H Monday, July 30, 2012 12:14 PM
    • Unproposed as answer by ChrisThomas Monday, July 30, 2012 1:26 PM
    Monday, July 30, 2012 12:14 PM
  • For tables that you create you can change the schema with the method you suggested.

    However the __MigrationHistory table is internal to Entity Framework.  I was talking to the team and they said they are working towards a solution to enable the name / schema of this table to be changed.

    Monday, July 30, 2012 1:26 PM