none
EF 5 & MVC RRS feed

  • Question

  • I would like to use Entity Framework 5 (Database First method) from which I can use one MVC Model that can span two or more databases. 

    Is this possible?

    Bill Yeager

    In other words, have one EF model that can use two or more databases. Because with MVC, you can only use 1 model in a View. Some of the data with some of the Views can come from different databases. In order to use the model binder in MVC and map it to EF 5 columns, I would need to accomplish this.
    • Edited by Bill_Yeager Thursday, October 11, 2012 8:51 PM
    Thursday, October 11, 2012 8:44 PM

Answers

  • Hi Bill,

    It is a bit hard to give advice without a bit more data. But if you have, for example, a user table in two different databases and some users are in one and some in another then you should be able to do that with a single model. As long as the tables are identical.

    If you want to combine information from two databases, like users in one database and user photos in another then that isn't possible with EF at the moment. You would need two contexts, one for users and one for photos. You could create a UserPhoto class that you bind to your view that contains the user and photo though. or alternatively have the photo on as an ignored property on your user context and populate it with a separate query with the other context.

    Does that all make sense? Let me know if I am way off the mark or you don't understand or anything.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Bill_Yeager Saturday, October 20, 2012 12:17 AM
    Thursday, October 18, 2012 8:38 PM
    Moderator

All replies

  • Hi Bill Yeager,

    Welcome to MSDN Forum.

    Do you mean you want Entity Framework5 to work with multiple databases? If yes, please refer to this thread: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/f13379ac-bc0c-4def-8b11-1af3ded1154b/. If I misunderstand, please feel free to let me know. : )

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 12, 2012 2:07 AM
    Moderator
  • Hi Bill,

    It is a bit hard to give advice without a bit more data. But if you have, for example, a user table in two different databases and some users are in one and some in another then you should be able to do that with a single model. As long as the tables are identical.

    If you want to combine information from two databases, like users in one database and user photos in another then that isn't possible with EF at the moment. You would need two contexts, one for users and one for photos. You could create a UserPhoto class that you bind to your view that contains the user and photo though. or alternatively have the photo on as an ignored property on your user context and populate it with a separate query with the other context.

    Does that all make sense? Let me know if I am way off the mark or you don't understand or anything.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Bill_Yeager Saturday, October 20, 2012 12:17 AM
    Thursday, October 18, 2012 8:38 PM
    Moderator