'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

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)



Solution 1:[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:[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.

Solution 3:[3]

To keep same class name I suggest to use different interfaces. An interface for the Core.Entity defining the common properties and an other interface for the extra properties. So instead of using a derived class you use a class implementing the two interfaces.

Solution 4:[4]

if you have 2 or more classes with a relationship between them like this:

public class A{
public X attribute1 {get;set;}
public B b {get;set;}
}

public class B{
public X attribute1 {get;set;}
}

sometimes in this situation it raises this error,

Solution : change X name in one class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brad Williams
Solution 2 phil soady
Solution 3 Guillaume Raymond
Solution 4 Ali Borjian