Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework conflict with same table name from different databases

I am using Entity Framework 4 with MVC 3 in Visual Studio 2012 (C#).

I am using database first; there are two separate databases each with its own namespace and with two separate edmx files. Each database has a table with the same name and fields (but different content). When I added the second table I started to get compile errors.

Ambiguity between 'Interface.CodeFormStatus.FormStatusCodeID' 
and 'Interface.CodeFormStatus.FormStatusCodeID' 

There seem to be some complex workarounds or I could rename one of the tables. Is there not a straightforward solution as this must be a fairly common issue.

like image 678
Peter Smith Avatar asked Sep 26 '12 10:09

Peter Smith


1 Answers

I ran into a situation where I had two databases (one an older version of the other) and I needed to integrate both into a single project. Naturally, almost every name conflicted.

I created two separated edmx files for each database, and put each in its own namespace for clarity. I then edited each entity name to reflect which database it was coming from - (e.g. "Activities", which was in both, became "v13Activities" and "v14Activities").

For operations which were to be mirrored between both databases, I wrote a wrapper that included both contexts. This made my code was much less repetitive, and it had less synchronization issues.

Hope this approach helps someone else - it seems like this is an obscure question, and this answer was one of the top results on Google!

Update: In EF 6.1+, there is another solution. You can have "conflicting" names, and separate them with simple namespacing when using the "Code First From Database" option. I would advocate for this solution going forward, as the old XML .edmx style is going to be phased out starting in EF Core.

like image 117
Corey Larson Avatar answered Sep 27 '22 22:09

Corey Larson