23

I am experiencing an issue with EF4 and Proxy Pocos.

I have 2 classes with the same name in the same assembly but different namespaces:

QuoteModels.CashPayment
OrderModels.CashPayment

This compiles fine but at runtime EF throws the following exception:

Schema specified is not valid. Errors: \r\nThe mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'CashPayment'. Previously found CLR type 'QuoteModels.CashPayment', newly found CLR type 'OrderModels.CashPayment'

Is there a workaround to have classes with the same name in the same assembly with different namespaces to work with Ef4?

Do I have to give them different names or move them into another assembly?

Ken Burkhardt
  • 3,528
  • 6
  • 33
  • 45
  • 1
    We have the same problem here. I posted a question about this "feature" on MSDN. Please, up vote (on MSDN) for quicker result: http://social.msdn.microsoft.com/Forums/hu-HU/adodotnetentityframework/thread/aa56de19-5cbb-4289-9f54-6a30a8d563c3 – W3Max Sep 21 '11 at 18:04
  • This should be a better place for up voting this feature: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/2266501-take-namespaces-into-account-for-pocos-mappings – W3Max Sep 21 '11 at 18:20

4 Answers4

12

I've found a work-around. It's a very obvious work-around that is non-ideal, but I think I'm going to call it good enough for us until EF5 comes out to fix this.

Short Answer: Just rename one or both of the ambigous entities Like: 2x Person are renamed to: Personal_Person and Work_Person based on a PersonalContext and a WorkContext.

Long Answer: In our scenario, we're using a DB-first approach (we're rewriting a legacy app with minimal DB changes). Our DB contains hundreds of tables, so rather than using a single EDMX/Context, I'm using multiple EDMX/Contexts (the EDMX has croaked every time I've attempted to add more than half of our tables). However, some tables need to exist in more than one EDMX/Context.

For discussion, let's pretend we have a simple database with the following tables:

  • Person
  • Family
  • Relationship
  • Address
  • Business
  • Employee

Also, for the sake of this discussion, let's assume that ANY table that exists in multiple contexts causes this problem (as I stated in comments to Devart's answer, this is not really true and I don't understand why it sometimes works).

Now let's say we want to create two contexts:

PersonalContext:

  • Person
  • Family
  • Relationship
  • Address

WorkContext:

  • Person
  • Business
  • Address
  • Employee

In this scenario, both Person and Address will cause our problem. So what we will do in our EDMX mapping is simply rename our entities to Personal_Person/Work_Person and Personal_Address/Work_Address.

As stated, this is very much an obvious work-around that is non-ideal but since EF doesn't take namespacing into account and goes strictly by name (not true identity, simply the name), one option is to put your namespacing inside your name.

Now I'm still debating if I'm going to do it that way or perhaps namespace the name for every entity (Personal_Person, Personal_Family, Personal_Relationship, Personal_Address and Work_Person, Work_Business, Work_Address, and Work_Employee) for both consistency and Intellisense-friendliness (keeping all entities in proper alphabetic order) since really, the namespace belongs before the name instead of after it, but that's a judgement call and not really important to providing a solution to the problem.

I hope this helps!!

Jaxidian
  • 13,081
  • 8
  • 83
  • 125
  • 1
    Just to follow-up on this, two-and-a-half years later, we still have this work-around in place. It has worked around the problem perfectly with no non-obvious side-effects. The ONLY gotcha is making sure the `Entity Set Name` is set correctly. In other words, when you rename `Person` to `Person_Work`, make sure the plural version is still named `People` instead of `Person_Works`. We also standardized on having the namespace portion **after** the entity name portion. This was so we could pull up `Person*` and simply *pick* the namespace rather than having to think about the namespace first. – Jaxidian Apr 02 '13 at 13:09
  • Years later update: This solution still holds up well for EF6. However, I've also been able to successfully get a very large database (400+ tables) into a single context with EF6 now and load without croaking, so my need for this solution has diminished tremendously. – Jaxidian Sep 22 '17 at 17:04
  • Does anyone know if this problem exists with LinqToSQL as well? – Riaan van Zyl Oct 08 '17 at 07:46
  • @Mfusiki: I recommend you ask a different question. That's a different product from Entity Framework. – Jaxidian Oct 09 '17 at 21:39
3

Take a look at this post. The Derek's comment seems to deal with the same issue, and he did not receive any answer from Microsoft.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Devart
  • 119,203
  • 23
  • 166
  • 186
  • 2
    He did: @Derek This is intentional. You can put your POCO classes in whatever namespace you'd like. The Entity Framework's by convention mechanism for detecting which properties on the entity match the properties of entities in your model does not use Namespace. What matters is that the type name (without namespace) matches the EntityType name in your model (edmx/csdl file). One area to watch out for is if you have multiple types with the same name but in different namespaces. Because we don't account for namespace, we detect that we've found multiple types and we throw an exception. Jeff – Jaxidian Jul 30 '10 at 13:14
  • 2
    Thanks for that clarification. Any idea why they did this by design? Is this going to change in the future? I'd like to have the ability to have POCOs in the same assembly but different namespaces have the same names, you know? – Ken Burkhardt Jul 30 '10 at 16:11
  • 3
    None whatsoever. I'm struggling with this right now and am needing to find a solution. My problem is that not ALL of my classes that match this cause the problem. It seems to only be caused by tables that act as many-to-many relationships. For example, in your scenario, if `CashPayment` is a standard table storing financial information, it works as expected for me! However, if `CashPayment` is simply a many-to-many table pointing to 0-to-many `CashTransaction` entities (hence making `CashPayment` a collection of type `CashTransaction`), then it blows up on the `CashPayment` table. Make sense? – Jaxidian Aug 02 '10 at 13:40
  • 1
    Correction: It sometimes works on non many-to-many tables and sometimes doesn't. I can't find a pattern but I definitely have it working in some scenarios - strange! – Jaxidian Aug 02 '10 at 13:52
  • 4
    Bumped into this today, using code first. The issue is plain stupid in this context, as the code itself is very clear on which type to use. Seems like a case of the Leaky Abstraction where the code first API is slammed on top of the older EF. – Thomas Eyde Nov 22 '11 at 13:24
  • 2
    Does anyone know if this has been reported as a bug on MSDN Connect? None of my searches turned up anything, but I could have missed it. Whether the EF team considers this a bug or not, it's a bug. Why would anyone want EF snooping randomly through namespaces other than the one specified in the DbContext class? It violates everything I know about C#. – devuxer Dec 21 '11 at 08:49
0

What I did was to delete one of the repeated entities and for the other entity create a partial class that has the properties of the deleted entity

Example:

namespace Namespace1
{
 class Talk
 {
  public int TalkId {get; set;}
 }
}
namespace Namespace2
{
 class Talk
 {
  public int TalkId {get; set;}
  public string SomeExtraInfo {get; set;}
 }
}

Result:

namespace Namespace1
{
 class Talk
 {
  public int TalkId {get; set;}
 }
}
namespace Namespace1
{
 partial class Talk
 {
  public string SomeExtraInfo {get; set;}
 }
}

Hope it helps someone

Irving r
  • 108
  • 1
  • 7
0

I just ran into a similar problem. It took me a while to pinpoint the pattern but in my context problems occured when I had two models which both had two of the same entities.

On one of my model I had the following entities ("->" denotes relationship with direction): Users -> Authentication -> Authorization (a user gets authenticated then authorized).

On the other one I had these entities: Authentication -> Authorization -> InvoiceGeneration (an authenticated and authorized user generates an invoice).

Upon running my app with these two models I got the same error as the original poster.

It turned out that, that my problem dissapeared when I removed the Authentication entity from the second model so it seems that EDM to CLR mapper cannot cope with entities who have same kind of "parent" relationships (on both of my models Authorization was "preceded" by Authentication. Sorry for the non-scientific relationship descriptions :)

juhan_h
  • 3,965
  • 4
  • 29
  • 35