Entity Framework conflicts with the same table name from different databases

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

First I use a database; There are two separate databases, each with its own namespace and two separate edmx files. Each database has a table with the same name and fields (but with different content). When I added the second table, I started getting compilation errors.

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

It seems like some complicated workarounds or I can rename one of the tables. Is there a simple solution, since this should be a fairly common problem.

+4
source share
2 answers

I was faced with a situation where I had two databases (one older version of the other), and I needed to integrate both into one project. Naturally, almost every name contradicted.

I created two separate edmx files for each database and put them in my own namespace for clarity. Then I edited each entity name to reflect which database it came from - (for example, the "Actions" that were in both became "v13Activities" and "v14Activities").

For operations that need to be mirrored between two databases, I wrote a wrapper containing both contexts. This made my code much less repetitive, and it had fewer synchronization issues.

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

Update: There is another solution in EF 6.1+. You may have "conflicting" names and separate them with a simple namespace when using the "Code First From Database" parameter. I would defend this decision in the future, as the old XML.edmx style will be phased out starting from EF Core .

+4
source

If you do not have a large number of tables with the same name, you can edit the name of the object in the designer (your .edmx file). So, just double-click the name of one of your CodeFormStatus objects and make it different (e.g. change it to CodeFormStatusOther )

0
source

Source: https://habr.com/ru/post/1436347/


All Articles