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

Time:01-09

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

CodePudding user response:

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.

  • Related