类型“Company.Model.User”和类型“Company.Core.Model.User”都具有相同的简单名称“User”,因此不能在同一模型中使用 [英] 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
问题描述
我有一个基本实体类 MyCompany.Core.Model.User
,用于用户$ c的常用属性$ c>实体:
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; }
}
Core.Model.UserMap 为基础设置代码第一个映射用户
class:
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");
}
}
在单独的程序集中,我有一个派生类 MyCompany.Model.User
继承自基础 User
类,并将其扩展为一些其他属性:
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; }
}
另外我有一个派生映射类 MyCompany .Model.UserMap
为附加属性提供额外的配置:
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");
}
}
然而添加 MyCompany时。 Model.User
到上下文并注册 MyCompany.Model.UserMap
我收到以下错误:
However when adding MyCompany.Model.User
to the context and registering the MyCompany.Model.UserMap
I'm getting the following error:
类型MyCompany.Model.User和类型MyCompany.Core.Model.User都具有相同的简单名称User,因此不能在同一模型中使用。给定模型中的所有类型必须具有唯一的简单名称。使用NotMappedAttribute或者在代码第一流利的API中调用Ignore来明确排除模型中的一个属性或类型。
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.
为什么基类简单名称在模型中注册,有没有办法为了实现这种实体继承?
我怀疑简单的解决方案是重命名派生类;但是我宁愿避免这种情况,因为在多个上下文中可能有许多派生。
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.
注意:使用实体框架6.0.0-rc1(prerelease) em>
Note: Using Entity Framework 6.0.0-rc1 (prerelease)
推荐答案
这是我在2012年报告的EF限制 https://entityframework.codeplex.com/workitem/483 在6.0.2中仍然没有实现。 EF使用平面的内部架构,不能识别命名空间。可能会来EF7,但不是以前。现在唯一的解决方案是将两个类重命名为唯一的类名,而不管它们的命名空间如何。嗯,这是EF内的一个重大限制。只需考虑一个名为Category的类,以及可以在多个域中使用多少个不同的命名空间。
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.
这篇关于类型“Company.Model.User”和类型“Company.Core.Model.User”都具有相同的简单名称“User”,因此不能在同一模型中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!