Entity framework - Code first approach RRS feed

  • Question

  • We have started creating a Project for splitting our current Database in two parts. Currently all the data are stored under one DB and employs code-first approach for create/update DB.

    There are 2 approaches to implement this database change in the visual studio solution.

    1.    Using the existing datacontext class and passing the connection string when instantiating a new object for updating the records moved to new table.

     Merits  : There will be less number of code change and only way of instantiating the (with or without arguments) may get changed.

    Demerits    : As we follow code first migration we are using the DataContext class for updating the database changes\schema, hence keeping the entity classes of both the databases in a single datacontext class which may cause issue.

     2.    Create new datacontext class and move the entity classes(for the particular list of tables moved to new DB).

    Merits    : It will help to keep two different object instantiation and straight forward in updating the records.

    Demerits  : This would need more code change. Need to confirm if a two datacontext classes can be used in a same project. Hence may require overall more effort.

    Please suggest which approach should i go with and why


    • Edited by Bhaskar Sharma Thursday, December 4, 2014 3:31 AM spell mistake
    Thursday, December 4, 2014 3:31 AM


  • Hello Bhaskar,

    >>We have started creating a Project for splitting our current Database in two parts

    If you mean you originally have a DB named A contains tables T1 and T2, and now you want to move the T2 table to another DB named B, if it is, I am wondering it is practicable because as far as I know, if using the migration to create the second database by using the existing datacontext, it would try to add a migration to the existing database although providing a different connection string.

    I do not know if you have tried to executed the first approach or you could use some ways to make it work, however, I do not recommend the first approach if it is possible in your side because, it is easy to get confused since one entity does not exists in the connected database, while it exists in the code model.


    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.

    • Marked as answer by Fred Bao Friday, December 12, 2014 9:01 AM
    Friday, December 5, 2014 5:07 AM