1

Closed

Code First: Error when class with same name as entity class but in a different namespace

description

This is a metabug since this issue (which is tracked by 483) is now fixed when using Code First. The fix for this when using Database First or Model First will be post-EF6.

A canonical example of this bug is having a Product entity that is included in your model and then a separate Product DTO that is defined in a different namespace. In EF5 and earlier we would throw because EF couldn't work out which Product class to use... but in the case of Code First we know which one to use because you define you model in terms of your CLR classes.

See item 483 for more details: http://entityframework.codeplex.com/workitem/483
Closed Mar 5, 2013 at 9:07 PM by RoMiller
Fixed. Note that this doesn't enable having two entities with the same name in your model (although you can now have two entities with the same name if one or both of them are nested in another class and therefore have a different CLR type name). The main scenario enabled is having an entity in your model and then a separate class with the same name in another namespace that isn't part of your model.

comments

ajcvickers wrote Mar 1, 2013 at 11:13 PM

Fixed in 139501a407de,

lukew wrote Mar 4, 2013 at 11:42 PM

Note:
Currently all the entities in a model must have unique simple names because the c-space namespace is the same for all entities. We are currently working on a change that will provide a better exception in these cases as part of the nested types work. We are also looking to lift this restriction but it is unlikely that this will be done for EF6

lukew wrote Mar 4, 2013 at 11:45 PM

Fixed, note that there are still restrictions to types with the same name as mentioned in previous comment

** Closed by lukew 03/04/2013 4:45PM

RoMiller wrote Mar 5, 2013 at 9:04 PM

Updated title and description to reflect that this isn't about having two entities with the same name in your model, but about having an entity in your model and then a separate class with the same name in another namespace that isn't part of your model.

raimondkuipers wrote Nov 21, 2013 at 12:49 PM

I'm getting this error: The type 'NamespaceA.Client' and the type 'ExternalNamespace.Dto.Client' both have the same simple name of 'Client' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model.

I don't think this should happen.

Togakangaroo wrote Jul 31 at 4:43 AM

So I'm using Code First with Entity Framework 6.0.2 but trying to generate a migration I get
The type 'MyApp.Viewer.Models.SlideSet' and the type 'MyApp.Editor.Models.SlideSet' both have the same simple name of 'SlideSet' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model.
What does this mean? How do I create unique simple names?

ajcvickers wrote Aug 11 at 11:12 PM

@Togakangaroo This was fixed for most Code First scenarios in EF6. However, on thinking through how all this works I can see that there may have still been some issues some issues with Migrations. We did some additional work in this area for 6.1 so if you update to the latest EF version (currently 6.1.1) then it should work with Migrations as well. (You may have to re-generate your last Migration with 6.1.1.)

Thanks,
Arthur