为 baseEntity 定义 EntityAttachment,以便将所有文件集成到数据库中的一个 table

define EntityAttachment for baseEntity in order to integrate all files in one table in database

我设计了一个基于 asp.net 样板模块零的新解决方案,我需要在单独的 table 中存储与每个实体(table 的每一行)相关的附件文件在数据库中。基本上我必须假设一个新实体与其他实体相关的导航 属性 并且这种方法意味着每次我在这个解决方案中设计新实体时我必须将它引用到附件实体所以我决定设计一个更好的解决方案这个特殊的问题。我想为名为 "IEntityAttachment" 或 "EntityAttachment" 的附件实体创建一个基础 Class 或接口,这意味着实体 class 必须派生自该接口或 class。 问题是 "is this approach possible?" 和 "what is the best approach for this problem?"

第一种方法:

第二种方法(我想要的):

是的,这种方法是可行的。

首先定义EntityAttachmentAttachment类.

public abstract class EntityAttachment : Entity {}

public class Attachment : EntityAttachment
{
    // ...
}

接下来定义BaseEntity 类参考上面类:

public abstract class BaseEntity<TEntityAttachment> : Entity
    where TEntityAttachment : EntityAttachment
{
    public virtual int AttachmentId { get; set; }

    public virtual TEntityAttachment Attachment { get; set; }
}

public abstract class BaseEntity : BaseEntity<Attachment> {}

然后,定义ArticleNewsSlider 类:

public class Article : BaseEntity
{
    // ...
}

public class News : BaseEntity
{
    // ...
}

public class Slider : BaseEntity
{
    // ...
}