The type 'Company.Model.User' and the type 'Company.Core.Model.User' both have the same simple name of 'User' and so cannot be used in the same model
Solution 1
This is a limitation of EF that I reported in 2012 https://entityframework.codeplex.com/workitem/483 that is still not implemented in 6.0.2. EF uses a flat internal architecture and does not recognize namespaces. Might be coming in EF7 but not before. For now the only solutions is to rename the two classes to unique class names irrespective of the namespace they are in. IMHO, this is an significant limitation within EF. Just consider a class named Category and how many different namespaces it could be used within across a domain.
Solution 2
First read Table type mappings
The hierarchical implementation model options need to be understood first.
Then look at the IGNORE option. You may or may not need depending on chosen approach.
requires ignore ???
modelBuilder.Ignore<BaseXYZ>()
Ef is currently trying to include your base class to support an included Type that inherits from a NON abstract class.
Brett Postin
Updated on June 27, 2022Comments
-
Brett Postin about 2 years
I have a base entity class
MyCompany.Core.Model.User
which is to be used for common properties of aUser
entity:public class User { public string Username { get; set; } public string Usercode { get; set; } }
I also have a base mapping class
MyCompany.Core.Model.UserMap
to setup the code first mappings for the baseUser
class:public class UserMap<TUser> : EntityMapBase<TUser> where TUser : User { public UserMap() { // Primary Key this.HasKey(t => t.Usercode); // Table & Column Mappings this.ToTable("Users"); this.Property(t => t.Username).HasColumnName("Username"); this.Property(t => t.Usercode).HasColumnName("UserCode"); } }
In a separate assembly I have a derived class
MyCompany.Model.User
that inherits from the baseUser
class and extends it with some additional properties:public class User : Core.User { public string Surname { get; set; } }
In addition I have a derived mapping class
MyCompany.Model.UserMap
to provide the additional configuration for the additional properties:public class UserMap : Core.UserMap<User> { public UserMap() { this.Property(t => t.Surname).HasColumnName("Surname"); } }
However when adding
MyCompany.Model.User
to the context and registering theMyCompany.Model.UserMap
I'm getting the following error:The type 'MyCompany.Model.User' and the type 'MyCompany.Core.Model.User' both have the same simple name of 'User' 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.
This link indicates that you can't have the same "simple name" in the model twice.
Why is the base class "simple name" being registered in the model, and is there a way around it in order to implement this sort of entity inheritance?
I suspect the simple solution would be to rename the derived class; however I would prefer to avoid this as there may be many derivations in multiple contexts.
Note: Using Entity Framework 6.0.0-rc1 (prerelease)
-
Brett Postin almost 11 yearsI have tried both of your suggestions. Ignoring the base class has no effect and I'm still getting the same error. Likewise, changing the base User class to be abstract has no impact.
-
Abhishek almost 10 yearsWow, just came accross this. It's a pretty huge limitation. Pretty much wipes out the whole DDD concept of Bounded Contexts
-
Panji almost 10 yearsVery annoying limitation for me. After years trying to convince myself to use EF instead NH, now stuck with this issue.
-
Yuck over 9 yearsHit this today myself. I have a system with two types of completely unrelated purchase orders, both called
PurchaseOrder
. Because of this, I need to rename one of my classes. Not pleased. -
Yuck over 9 yearsWhat's worse is that the table names can't be the same even if they are in different schemas.
OM.PurchaseOrder
andMM.PurchaseOrder
get confused by EF and it thinks they are trying to share the same table. Get it together, MS. -
Luis Filipe over 9 yearsThis issue can be less relevant if we use smaller db contexts
-
pookie almost 9 yearsFantastic. I have just run into this problem. Guess what? My class name is 'Category'.
-
Brett Postin over 8 yearsThe reason for having the derived class is to minimise the duplication of entity mappings.
-
Darrell Lloyd Harvey about 8 yearsThis was addressed in October of 2013. " this is something we want to tackle post EF6 such that EF can use any number of types with the same simple name so long as they have different namespaces or outer types". Seems to me this would have been a little more important to tackle prior to EF7? blog.oneunicorn.com/2013/03/11/…
-
Seabizkit almost 3 years2021 and this is still and issue!!