C#
C#

help

Root Question Message

Ayymoss
Ayymoss12/14/2022
❔ 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.
Ayymoss
Ayymoss12/14/2022
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. 😄
AntonC
AntonC12/14/2022
are you asking for db structure?
AntonC
AntonC12/14/2022
generally you connect these via foreign keys
Ayymoss
Ayymoss12/14/2022
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?
Ayymoss
Ayymoss12/14/2022
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!;
}
Ayymoss
Ayymoss12/14/2022
Kinda like this? and have no reference in the User and Server model to the Infraction model?
Ayymoss
Ayymoss12/14/2022
A user can have many infractions, which all need to have the sending server.
AntonC
AntonC12/14/2022
it's implied via the navigation properties that these are foreign keys
Ayymoss
Ayymoss12/14/2022
Right, but is this the correct usage?
Should these FK's be in this model?
AntonC
AntonC12/14/2022
yes
yes, but not necessarily
AntonC
AntonC12/14/2022
you can also have collection a collection of infractions for the user and for the server as navigation properties
Ayymoss
Ayymoss12/14/2022
How would this be achieved?
AntonC
AntonC12/14/2022
read on navigation properties
Ayymoss
Ayymoss12/14/2022
Appreciate your time! 🙂 Will take a look!
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy