locked
mixing EF databse first and code first data modelling RRS feed

  • Question

  • Hi,

    I am very new to entity framework and hence would expect some detail answer to my problem.

    I have a requirement in which i am developin a new ASP.net MVc application using entity framework code first approach so that it can be portable to any database.

    but for that application i have to reuse some of the tables from one of the already existing database which is used in other applications.

    For example :We have a student portal application already existing

    Tables which i look for reuse are like Program, courses,universities ..etc(which are already in use in student portal application)

    Now i am developing a Faculty portal application in which i would also need the same tables(Program, course, universites..etc) along with other tables specific to faculty portal application

    for new tables in faculty portal i can use Code first and for resuing the existing tables i have to use Database first

    my problem happens here:

    suppose i took the Program table and Faculty Table

    Program table is alraedy existing one- which i got using EF database modelling

    Faculty table i crated using EF code first modelling with fluentAPI.

    So now i have two model entities:

    ProgramModel- from edmx

    FacultyModel- code first

    I have to create an entity which will be join both the models and will create 
    Faculty_Program table with FacultyId and ProgramId as composite key(join table)

    How can i achieve that?

    I am able to do that if i am using code first for both the program and faculty model.

    But the problem happens when i try to reuse the program table using database first and tried to create the join table with faculty model

    Please help me with prompt response

    Monday, January 13, 2014 8:59 AM

Answers

  • Hi Deepali123,

    Do you mean that you want to use the code first work with two separate database? If it is, it is impossibe.


    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 Monday, January 20, 2014 10:09 AM
    Wednesday, January 15, 2014 9:51 AM

All replies

  • Hello,

    >>mixing EF databse first and code first data modelling

    This is impossible, we have to choice one model to be used that means only Code First or others.

    From your description, a workaround is that we can to use the Code First model with an existing database. For this, we need the Entity Framework Power Tools installed. These Power Tools provide some additional Visual Studio tooling to help with Code First development, including the ability to reverse engineer a Code First model from an existing database, for details, please refer to link below:
    http://msdn.microsoft.com/en-us/data/jj200620.aspx

    There is a video that has a step-by-step walkthrough provide an introduction to Code First development targeting an existing database.

    Then we use the migrations in Code First to create the new table we want.

    Since the Code First model is generated by an existing database, we need to delete these operations for existing tables in both Up() and Drop() methods  before we call the Update-Database command to save changed into database.

    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.

    Tuesday, January 14, 2014 8:52 AM
  • Hi Fred,

    thanks for the response.

    I could understand your point and I tried to do code first with existing database.

    But as you have mentioned "Since the Code First model is generated by an existing database, we need to delete these operations for existing tables in both Up() and Drop() methods  before we call the Update-Database command to save changed into database". The problem happens when I need to join and create an intermediate table using tables from existing database and new database. 

    As I have mentioned: "I have to create an entity which will be join both the models and will create Faculty_Program table with FacultyId and ProgramId as composite key(join table)". so if I am removing the already eisting tables from migrations Up() and Down() I am getting the below error:"

    Foreign key 'FK_dbo.Faculty_Programs_dbo._Programme_programId' references invalid table 'dbo._Programme'.

    Could not create constraint. See previous errors."

    Program table is existing in some other database, when I removed the program table from up() and down() in migration and commented the forengnkey with programid on Faculty_Program table(as I was getting the error stated above during migration)
    I got the below table structure where program id is not in FK.

    But I would need the foreignkey reference from Program table into the Faculty_program table.

    My Common Model Lib is the new tables which I am creating.

    My Existing model Lib is the code first with existing database tables. as I mentioned existing tables are in different database where I have readonly acess to tables.

    My Join Table Model Lib is the joining between existing and new ones and Migration is enabled on this project

    How can I achive that?

    Wednesday, January 15, 2014 3:09 AM
  • "entity framework code first approach" has nothing to do with "can be portable to any database".

    code first is mean to generate the database stuffs based on your code.

    find out why really need to adopt "code first". is the "primary" reason is "portable to any database", you are not limiting to code first.

    and you have to tell whoever asking for that, no "any" database is really portable from EF.

    and you may need to find out what he/she means by "portable".

    Wednesday, January 15, 2014 3:38 AM
  • Hi Deepali123,

    Do you mean that you want to use the code first work with two separate database? If it is, it is impossibe.


    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 Monday, January 20, 2014 10:09 AM
    Wednesday, January 15, 2014 9:51 AM