none
Migrating tables created by Entity Framework Code First to another (existing) database RRS feed

  • Question

  • My situation is this: I am working on an app that uses Entity Framework and a new database was created with Code First. This works fine in development, but for production I need to migrate all the tables created in development to an existing database. Is it possible to make the app I am working on to work with the existing database, and if it is, how can I make the app work with the existing database?

    Wednesday, May 11, 2016 6:27 PM

Answers

  • Hi Ale Carra,

    >>How do I migrate everything to database B, while keeping the classes I made to create database A?

    From the above link I providing, when you use database B, you could generate some model classes and a DbContext. Then merge two DbContexts.

    >>What do you mean by merge to DBContext?

    For example:

    DBBContext (For Database A)

     public partial class DbAContext : DbContext
        {
            public DbAContext()
                : base("name=DbAContext")
            {
            }
    
            public virtual DbSet<result> results { get; set; }
            public virtual DbSet<sport> sports { get; set; }
    
         }

    DBBContext (For Database B)

    public partial class DbBContext : DbContext
        {
            public DbBContext()
                : base("name=DbBContext")
            {
            }
    
            public virtual DbSet<athlete> athletes { get; set; }
           
         }

    After Merge.

    public partial class DbBContext : DbContext
        {
            public DbBContext()
                : base("name=DbBContext")
            {
            }
    
            public virtual DbSet<athlete> athletes { get; set; }
            public virtual DbSet<result> results { get; set; }
            public virtual DbSet<sport> sports { get; set; }
    
         }

    Then you need delete DBAContext and remove the connection string from your configuration file.

    Best regards,

    Cole Wu


    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.



    Thursday, May 12, 2016 5:00 AM
    Moderator

All replies

  • Hi Ale Carra,

    If your existing database doesn't have any tables, you could only change the connection string to the existing database, deleting the folder named Migrations on your project. and then using migrations command to migrate all the tables (enable-migrations --> add-migration yourmigrationname --> update-database).

    If your existing database has existing tables and you want to use the tables on your project,

    1.you need to create model class by using code first with existing tables (link as below).

    2.add an initial migration (link as below).

    3.merge to DBContext.

    4.run migrations command again.

    Code First to an Existing Database

    https://msdn.microsoft.com/en-sg/data/jj200620

    #add an initial migration

    https://msdn.microsoft.com/en-us/library/dn579398.aspx?f=255&MSPPError=-2147217396#step3

    Best regards,

    Cole Wu


    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.

    Thursday, May 12, 2016 1:36 AM
    Moderator
  • When I started the project I started by making the classes then using Entity Framework Code First to create a new database (database A), and the tables for those classes. But later I need to migrate all of the tables from database A to the existing database B, which has existing tables which I need. Currently I am using one DbContext for database A and another one for database B. How do I migrate everything to database B, while keeping the classes I made to create database A? What do you mean by merge to DBContext?
    Thursday, May 12, 2016 3:21 AM
  • Hi Ale Carra,

    >>How do I migrate everything to database B, while keeping the classes I made to create database A?

    From the above link I providing, when you use database B, you could generate some model classes and a DbContext. Then merge two DbContexts.

    >>What do you mean by merge to DBContext?

    For example:

    DBBContext (For Database A)

     public partial class DbAContext : DbContext
        {
            public DbAContext()
                : base("name=DbAContext")
            {
            }
    
            public virtual DbSet<result> results { get; set; }
            public virtual DbSet<sport> sports { get; set; }
    
         }

    DBBContext (For Database B)

    public partial class DbBContext : DbContext
        {
            public DbBContext()
                : base("name=DbBContext")
            {
            }
    
            public virtual DbSet<athlete> athletes { get; set; }
           
         }

    After Merge.

    public partial class DbBContext : DbContext
        {
            public DbBContext()
                : base("name=DbBContext")
            {
            }
    
            public virtual DbSet<athlete> athletes { get; set; }
            public virtual DbSet<result> results { get; set; }
            public virtual DbSet<sport> sports { get; set; }
    
         }

    Then you need delete DBAContext and remove the connection string from your configuration file.

    Best regards,

    Cole Wu


    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.



    Thursday, May 12, 2016 5:00 AM
    Moderator