Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two different databases simultaneously using Entity Framework

I'm using Entity Framework 4 in an MVC3 project. I'm trying to access two different databases (A and B):

  • Databases A and B are completely different and unrelated.
  • Database A is a 'code first' SQL compact database.
  • Database B is a 'database first' SQL Server database.
  • I have straightforward queries on either database, no linking or cross-database joins.
  • Either database connection works if I remove the other DbContext from the project.

Now, when I try to query from database A, I'm getting an Exception "Could not find the conceptual model type for ".

I can't imagine that EF doesn't let you use different databases, so my question is: do I need to do something extra when creating a new instance of the appropriate DbContext, in order for this to work?

like image 365
Tom de Waard Avatar asked May 01 '11 21:05

Tom de Waard


2 Answers

I stumbled across this thread while trying to resolve the "Could not find the conceptual model type for" exception.
I am using EF 4.2 with multiple models. I implemented DbContext with one of my models to "try it out". When I compiled and ran, I received the exception above for an entity in a completely different model on a completely different database!

After much hassle I finally implemented DbContext to the other two models and everything ran fine.

I know this doesn't necessarily answer the question, but I wanted to leave this here for anyone else that has the same issue and stumbles across this post.

like image 112
Airn5475 Avatar answered Jan 02 '23 09:01

Airn5475


Make sure you don't use the same entities name for both edmx files. You can change it manully opening a edmx with xml editor right clicking the file and open with.

Note that for some security reasons you can't nest the contexts. They can only be used separated.

like image 38
BrunoLM Avatar answered Jan 02 '23 11:01

BrunoLM