“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
用于 User
实体的公共属性:
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; }
}
我还有一个基础映射类 MyCompany.Core.Model.UserMap
来设置基础 User
类的代码优先映射:
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",因此不能在同一模型中使用.给定模型中的所有类型都必须具有唯一的简单名称.在 Code First fluent API 中使用NotMappedAttribute"或调用 Ignore 以从模型中显式排除属性或类型.
此链接 表示你不能在模型中使用相同的简单名称"两次.
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(预发布)
推荐答案
这是我在 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屋!