C
C#3y 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
AmosOP3y 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
Anton3y ago
are you asking for db structure? generally you connect these via foreign keys
Amos
AmosOP3y 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
Anton3y ago
it's implied via the navigation properties that these are foreign keys
Amos
AmosOP3y ago
Right, but is this the correct usage? Should these FK's be in this model?
Anton
Anton3y 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
AmosOP3y ago
How would this be achieved?
Anton
Anton3y ago
read on navigation properties
Amos
AmosOP3y ago
Appreciate your time! 🙂 Will take a look!
Accord
Accord3y 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.

Did you find this page helpful?