2

Technologies:

  • C#/MVC4
  • .Net 4.5
  • Sql Server 2008 R2
  • Entity Framework 6.1.0

Scenario: I have a several client databases that I need to interact with, and a common database that handles data that isn't specific to any one client. Most of these client databases are similar and have multiple tables with the same names and most of the same fields.

In my data tier I have several data managers who handle "targeted" data retrieval different sources. No data manager would access two client databases.

For each client database, I want to create an EF EDM to manage it. The problem I'm running into is a naming conflict that, as far as I can see, should never occur. Seeing as all DbContexts have their own namespaces and I never instantiate two client DbContext objects at the same time (ever); I'm not understanding why I am getting the following error.

Here's the error:

Schema specified is not valid. Errors: The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'itm'. Previously found CLR type 'MyData.OxiRecycle.itm', newly found CLR type 'MyData.OxiRx.itm'.

In the instantiation of the OxiRecycleDataManager, I instantiate the OxiRecycle DbContext, but not the OxiRx DbCotext.

public class OxiRecycleDataManager
{
  protected OxiRecycleEntities db;

  public OxiRecycleDataManager()
  {
    db = new OxiRecycleEntities();
  }
}

Question: If I only ever instantiate one client DbContext per application per application life cycle (request), why am I getting the conflict?

Difinity
  • 235
  • 2
  • 12
  • Easiest way to fix this is have each model in its own class library. – gunr2171 Jun 02 '14 at 16:56
  • While that would be extremely painful, it is doable. I would be looking for a better way, should one exist. – Difinity Jun 02 '14 at 17:00
  • Try using AutoMapper or perhaps inheritance/interfaces for each database. But really you want inheritance...which is going to be an utter pain using ModelFirst. – Aron Jun 02 '14 at 17:05
  • Actually come to think about it. How to you write code that can interface with different client databases? Surely your Domain layer must be the same on each client...in which case your ACTUAL problem isn't that you have multiple `Models`...as each MUST be the same...but actually its that you have different `Relational Mappings`. If that is the case....I suggest you abandon EDMX based EF, and use fluent Entity Mappings for each client. – Aron Jun 02 '14 at 17:09
  • So basically what you're saying is that I have to code it all by hand? If I wanted to do that I would use Nhibernate or just write a bunch of retarded tsql. Thank you Microsoft! – Difinity Jun 02 '14 at 17:37
  • I guess what I really don't understand is why I'm getting the error when the two DbContexts are NEVER instantiated, or exist, simultaneously. One never exists at the same time as another. I'm not understanding how EF can't differentiate between one specifically named class and another. Maybe a better question is, how has this framework made it through 6 major versions with this obvious flaw? – Difinity Jun 02 '14 at 17:40
  • in a hurry, i added **multiple .edmx** file which had same class names in my MVC Project and thus wasted time figuring this error , hope helps some one. – Shaiju T Sep 16 '15 at 11:09

0 Answers0