如何避免重复的数据结构中的code首先? [英] How to avoid repeated data structures in Code First?

查看:234
本文介绍了如何避免重复的数据结构中的code首先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己有一大堆的类别重新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 map BaseEntity - only derived classes are mapped
  • Your context doesn't contain DbSet for BaseEntity, only for derived types
  • You don't nave EntityTypeConfiguration registered for BaseEntity

如果你遵循这些规则的继承将只有在你的应用程序,而不是在你的数据库。

If you follow these rules the inheritance will be only in your application, not in your database.

这篇关于如何避免重复的数据结构中的code首先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆