Answer the question
In order to leave comments, you need to log in
How to correctly describe the structure of the database?
I'm doing a chat and I needed a database for a list of users. There will be in particular administrators and bans. I described it like this:
public class UserInfo
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required, MinLength(4), MaxLength(32)]
public string Username { get; set; }
[Required, MinLength(4), MaxLength(32)]
public string Password { get; set; }
}
public enum UserRole { User, Admin, SuperAdmin };
public class RoleInfo
{
public long Id { get; set; }
[Required]
public RoleInfo Role { get; set; }
}
public class BanInfo
{
public long Id { get; set; }
[Required]
public DateTime EndOfBan { get; set; }
[MaxLength(64)]
public string Reason { get; set; }
}
public class DbContainer : DbContext
{
public DbContainer() : base($"name={nameof(DbContainer)}") { }
public virtual DbSet<UserInfo> Users { get; set; }
public virtual DbSet<RoleInfo> Roles { get; set; }
public virtual DbSet<BanInfo> Bans { get; set; }
}
public class BanInfo
{
public long? UserId { get; set; }
[ForeignKey(nameof(UserId))]
public UserInfo User { get; set; }
[Required]
public DateTime EndOfBan { get; set; }
[MaxLength(64)]
public string Reason { get; set; }
}
public class RoleInfo
{
public long? UserId { get; set; }
[ForeignKey(nameof(UserId))]
public UserInfo User { get; set; }
[Required]
public RoleInfo Role { get; set; }
}
Answer the question
In order to leave comments, you need to log in
And try to normalize the structure. You can read a little about normalization here By the way, there is also something about database design.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question