17
Vote

Can't map two classes with same name from different namespace

description

EF doesn't allow two classes with the same name, but different namespaces, to be mapped.

This is complex to change so we also have an item to provide a better exception message until this is supported - http://entityframework.codeplex.com/workitem/589

namespace Test.Security
{
public class Question
{
  public Guid Id { get; set; }
}
}

namespace Test.Forms
{
public class Question
{
  public Guid Id { get; set; }
}
}

comments

aszora wrote Sep 12, 2012 at 9:36 AM

The problem with EF codefirst that it uses a flat namespace internally.

For examaple if you have a Question class which has an Answer navigation property the internal modelmetada will contain a reference called QUESTION_ANSWER.

So if occasionally you have a view in the database named QUESTION_ANSWER, you cant use it, because of duplicate name:)
(I did this when I tried to specify a view for many-to-many rel for example.)

mousedoc wrote Sep 12, 2012 at 1:51 PM

Yes, but that is a significant problem with EF. It will allow the following to compile for the above.

public class MyTextContext : DbContext
{
public DbSet<Test.Security.Question> Security_Question { get; set; }
public DbSet<Test.Forms.Question> Forms_Question { get; set; }
}

Took me a long time to track it down because the error message was something like "Question not mapped". When I would at the data annotations on classes and the fluent API it was clear I had things configured properly.

RoMiller wrote Oct 16, 2012 at 6:08 PM

Updating title and description to reflect that this is a general EF issue and not specific to Code First.

This is a limitation of EF that we plan to fix but it is complicated and not something we are planning to address in EF6. Marking to be fixed in a future release.

glide wrote Nov 21, 2012 at 5:05 PM

Just ran into this problem when trying to create entities that are separated by schema in the database and using custom T4 templates to generate the entities into different namespaces.

moozzyk wrote Jan 7 at 12:44 AM

Adding exception message to make it easier to find:

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'ABC'. Previously found CLR type 'Ns1.ABC', newly found CLR type 'Ns2.ABC."}

ajcvickers wrote Mar 1 at 10:58 PM

This now works for EF6 but only when using Code First See: http://entityframework.codeplex.com/workitem/911

This item remains open and in futures for Database First and Model First.

RoMiller wrote Mar 8 at 6:11 PM

To add to what Arthur said - we've fixed (for Code First only) the issue that prevented you having a Question class in your model and then another Question class that isn't in your model and lived in a different namespace. In EF6 we have not enabled having two Question classes included in your model.

mousedoc wrote Mar 8 at 7:03 PM

Both were in my model, so this doesn't help my situation. I could have just applied an Ignore attribute to the class if I wanted this fix.