问题描述
我发现自己有一大堆的类别重新presenting查找表,如JOBTITLE和语言,都承担着相同的结构,即
I find myself with a whole lot of classes representing lookup tables, such as JobTitle and Language, that all share the same structure, i.e.
public Guid Id { get; set; }
public string Name { get; set; }
我怎样才能避免重复这样的结构,没有引入额外的属性,例如:复杂类型IdName的,具有相同的结构如上述,例如
How can I avoid repeating this structure, without introducing an extra property, e.g. a complex type of IdName, with the same structure as above, e.g.
public class Gender
{
public IdName Inner { get; set }
}
我想避免引用Gender.Inner.Name,而是指刚Gender.Name。
I want to avoid having to reference Gender.Inner.Name and instead refer just to Gender.Name.
推荐答案
在这种情况下,创建一个包含一个基类编号
和名称
。
In such case create a base class containing Id
and Name
.
public class BaseEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
}
和您的性别
将变成:
public class Gender : BaseEntity
{
...
}
要避免您不想总是遵循这些规则映射继承:
To avoid mapped inheritance which you don't want always follow these rules:
-
OnModelCreating
不映射BaseEntity
- 只有派生类映射 - 您的上下文不包含
DbSet
为BaseEntity
,只为派生类型 - 您不中堂
EntityTypeConfiguration
注册BaseEntity
OnModelCreating
doesn't mapBaseEntity
- only derived classes are mapped- Your context doesn't contain
DbSet
forBaseEntity
, only for derived types - You don't nave
EntityTypeConfiguration
registered forBaseEntity
如果你遵循这些规则的继承将只有在你的应用程序,而不是在你的数据库。
If you follow these rules the inheritance will be only in your application, not in your database.
这篇关于如何避免重复的数据结构中的code首先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!