The type 'Company.Model.User' and the type

2019-01-25 02:06发布

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)

3条回答
Deceive 欺骗
2楼-- · 2019-01-25 02:21

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.

查看更多
疯言疯语
3楼-- · 2019-01-25 02:23

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.

查看更多
Bombasti
4楼-- · 2019-01-25 02:28

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.

查看更多
登录 后发表回答