如何在C#中使用Entity Framework来操作通用图片模型的关系数据库?

huangapple go评论70阅读模式
英文:

How to use general picture model relational database in C# with Entity Framework?

问题

我想使用一个通用的“Picture”模型,并使用枚举类型 - 就像这样:

public enum PictureType
{
   Message = 0,
   User = 1,
   Mentor = 2,
   Group = 3,
   Tier = 4
}

然后在通用图片模型中使用这个枚举,如下所示:

public class Picture : BaseEntity, IEntity
{
   public string FileName { get; set; }
   public string FilePath { get; set; }

   // 枚举图片类型 (0: 用户)(1: 导师)(2: 消息)(3: 群组)
   public PictureType PictureType { get; set; }

   // 用户图片
   public Guid UserId { get; set; }
   public User User { get; set; }

   // 导师图片
   public Guid MentorId { get; set; }
   public Mentor Mentor { get; set; }

   // 消息图片
   public Guid MessageId { get; set; }
   public Message Message { get; set; }

   // 群组图片
   public Guid GroupId { get; set; }
   public Group Group { get; set; }
}

但这是否是将图片保存到服务器的正确方式?还是图片保存方法的另一种版本?

将所有图片类和模型分离:

public class MessagePicture : BaseEntity, IEntity
{
   public string FilePath { get; set; }    
   public string FileName { get; set; }   
   public Guid MessageId { get; set; }
   public Message Message { get; set; }
}

这是否是一对一关联表的正确方式?

对于最佳实践,我需要做什么?

  1. 使用通用图片模型
  2. 将所有图片模型分开

我还在使用存储库模式。

我希望在项目和业务代码中使用最佳实践。

英文:

I want the use a general Picture model with an enum type - like this:

 public enum PictureType
 {
    Message = 0,
    User = 1,
    Mentor = 2,
    Group = 3,
    Tier = 4
 }

And I use this enum in general picture model like this:

 public class Picture : BaseEntity, IEntity
 {
    public string FileName { get; set; }
    public string FilePath { get; set; }

    //Enum Picture Type (0:User)(1:Mentor)(2:Message)(3:Group)
    public PictureType PictureType { get; set; }

    // User pictures
    public Guid UserId { get; set; }
    public User User { get; set; }

    // Mentor pictures
    public Guid MentorId { get; set; }
    public Mentor Mentor { get; set; }

    // Message pictures
    public Guid MessageId { get; set; }
    public Message Message { get; set; }

    // Group picture
    public Guid GroupId { get; set; }
    public Group Group { get; set; }
}

But this is the right way for the save picture to server? Or another version of the picture save method?

Separate all picture classes and models

public class MessagePicture : BaseEntity, IEntity
{
    public string FilePath { get; set; }    
    public string FileName { get; set; }   
    public Guid MessageId { get; set; }
    public Message Message { get; set; }
}

Is this the right way for the one to one relational table?

What I have to do for the best practice?

  1. Use a generic picture model
  2. Separate all models for the pictures

I am also using the repository pattern.

I want the use best practice for the project and business code

答案1

得分: 0

您可以两种方式使用它。通用或专用实体来存储数据,对于您的情况是图片。这真的取决于一些使用情况,首先提出以下问题,

  1. 每个类别将存储多少数据?
  2. 数据将被查询多少次?是特定类别还是全部?

仅举一个例子,如果您说这个表可以在所有类别中填充超过20/30+万的数据,最好将它们分开,但如果答案是否定的,或者它们不会均匀分布,那么使用相同的实体(通用)并进行更好的索引也没问题。

根据业务需求,还可以提出许多其他问题。

但如果您对任何这些问题都不确定,可以从通用开始,然后在必要时进行更改。

英文:

You can use it either way. The generic or specialized entity to store the data, in your case pictures. It really depends on some use cases, Just ask these questions first,

  1. How much data will be stored in each category?
  2. How many times, data will be queried? Is it a specific category or all?

Just for example, If you say this table can populate over 20/30+ million of data over all the categories equally its better to separate them but if the answer is no, or it will not be equally distributed then the same entity (generic) with better indexing is just fine.

Many more questions can be raised depending on the business requirements.

But If you are not sure about any of this, start with the generic, and change when it is necessary.

huangapple
  • 本文由 发表于 2023年1月8日 23:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定