D
D
DarkByte20152016-05-10 12:22:07
Database design
DarkByte2015, 2016-05-10 12:22:07

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; }
}

But I'm confused with the attributes from DataAnnotations. I am reading this article. How do I specify a ForeignKey? Somehow it is not clearly described there ... How does the Key attribute differ from ForeignKey? Where which one to use in my case?
PS In general, I'm not sure about the structure of the database either ... Is it necessary to spread it so much into different tables? I had everything in one table, but I was advised to space it out. How do they really do in such cases?
Once again I re-read the option considered in the article, something began to clear up. If I'm not mistaken it should be like this:
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; }
}

But this way it knocks out the following errors:
One or more validation errors were detected during model generation:
dbtest.Database.BanInfo: : EntityType 'BanInfo' has no key defined. Define the key for this EntityType.
dbtest.Database.RoleInfo: : EntityType 'RoleInfo' has no key defined. Define the key for this EntityType.
Bans: EntityType: EntitySet 'Bans' is based on type 'BanInfo' that has no keys defined.
Roles: EntityType: EntitySet 'Roles' is based on type 'RoleInfo' that has no keys defined.
And all figured out. You just had to add the keys to them too.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Skorzhinsky, 2016-05-10
@AndyKorg

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 question

Ask a Question

731 491 924 answers to any question