C
C#2y ago
Amos

❔ Database Models and Tracking Reference

I'm really new to EFCore and databases in general. I'm looking for a bit of guidance. I have 3 tables (see screenshot), I want the Infraction to track on with the User and the Server. How do I go about this? In the future, I'm going to need to search for all infractions made on a specific server and all the infractions a user has been given.
10 Replies
Amos
AmosOP2y ago
Due to a lack of keyword knowledge for this area, ChatGPT wasn't much help here. 🙂 Not exactly sure how to phrase the question either which doesn't help. 😄
Anton
Anton2y ago
are you asking for db structure? generally you connect these via foreign keys
Amos
AmosOP2y ago
I guess. Yeah a friend suggested foreign keys, but I don't get how to use them at all 😂 Do I put the foreigns to the User & Server from the Infraction Model?
public class InfractionModel
{
[Key] public int Id { get; set; }

// properties

public int UserId { get; set; }
[ForeignKey(nameof(UserId))] public UserModel User { get; set; } = null!;

public int ServerId { get; set; }
[ForeignKey(nameof(ServerId))] public ServerModel Server { get; set; } = null!;
}
public class InfractionModel
{
[Key] public int Id { get; set; }

// properties

public int UserId { get; set; }
[ForeignKey(nameof(UserId))] public UserModel User { get; set; } = null!;

public int ServerId { get; set; }
[ForeignKey(nameof(ServerId))] public ServerModel Server { get; set; } = null!;
}
Kinda like this? and have no reference in the User and Server model to the Infraction model? A user can have many infractions, which all need to have the sending server.
Anton
Anton2y ago
it's implied via the navigation properties that these are foreign keys
Amos
AmosOP2y ago
Right, but is this the correct usage? Should these FK's be in this model?
Anton
Anton2y ago
yes yes, but not necessarily you can also have collection a collection of infractions for the user and for the server as navigation properties
Amos
AmosOP2y ago
How would this be achieved?
Anton
Anton2y ago
read on navigation properties
Amos
AmosOP2y ago
Appreciate your time! 🙂 Will take a look!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server