【问题标题】:Correct way to map IS-A Relationships in C#在 C# 中映射 IS-A 关系的正确方法
【发布时间】:2012-03-19 11:46:50
【问题描述】:

让我们想象一个简单的 IS-A 关系,如下例所示:

create table EntityAbstract(
  IDEntityAbstract int identity primary key,
  Name nvarchar(50) not null,
)


create table OneOfConcreteEntity(
  EntityAbstract int,
  constraint PK_Image primary key (EntityAbstract),
  constraint FK_Image foreign key (EntityAbstract) references EntityAbstract(IDEntityAbstract)  
)

当我从数据库映射实体时,我应该使它们成为单独且唯一的对象,还是应该从抽象实体扩展具体类?

例如:

  public class EntityAbstract
    {
        public int EntityAbstractID { get; set; }
        public string Name { get; set; }
        public EntityAbstract(int entityID, string name)
        {
            this.EntityAbstractID = entityID;
            this.Name = name;
        }
    }

   public class OneOfConcreatEntity : EntityAbstract
    {
        public OneOfConcreatEntity(int entityID, string name) : base(entityID, name){ }
    }

什么是最好的选择?考虑多个具体实体和更复杂的实体。

【问题讨论】:

    标签: c# sql orm


    【解决方案1】:

    有几个design patterns 涉及IS-A 关系,通常这正是继承的用途。

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2011-01-18
      相关资源
      最近更新 更多