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

10,209

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.

Share:
10,209
Brett Postin
Author by

Brett Postin

Updated on June 27, 2022

Comments

  • Brett Postin
    Brett Postin about 2 years

    I have a base entity class MyCompany.Core.Model.User which is to be used for common properties of a User 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 base User 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 base User 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 the MyCompany.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
    Brett Postin almost 11 years
    I 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
    Abhishek almost 10 years
    Wow, just came accross this. It's a pretty huge limitation. Pretty much wipes out the whole DDD concept of Bounded Contexts
  • Panji
    Panji almost 10 years
    Very annoying limitation for me. After years trying to convince myself to use EF instead NH, now stuck with this issue.
  • Yuck
    Yuck over 9 years
    Hit 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
    Yuck over 9 years
    What's worse is that the table names can't be the same even if they are in different schemas. OM.PurchaseOrder and MM.PurchaseOrder get confused by EF and it thinks they are trying to share the same table. Get it together, MS.
  • Luis Filipe
    Luis Filipe over 9 years
    This issue can be less relevant if we use smaller db contexts
  • pookie
    pookie almost 9 years
    Fantastic. I have just run into this problem. Guess what? My class name is 'Category'.
  • Brett Postin
    Brett Postin over 8 years
    The reason for having the derived class is to minimise the duplication of entity mappings.
  • Darrell Lloyd Harvey
    Darrell Lloyd Harvey about 8 years
    This 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
    Seabizkit almost 3 years
    2021 and this is still and issue!!