Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to create a many-to-many relationship between users in a table?

How to create a many-to-many relationship between users in a table?

Scheduled Pinned Locked Moved C#
asp-netquestioncsharpdatabasesqlite
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Wright 2022
    wrote on last edited by
    #1

    I'm working on an ASP.NET Core web API and SQLite database. I have created a table for users of my application. The rule is that each user can send a message to some other users (creating White-List). I use the following model for Users:

    public class UserModel
    {
    [Key]
    public int UserID { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string Role { get; set; }

    //Navigation property
    public Department Department { get; set; }
    

    }

    My desired table is:

    User-SubUser table

    UserID ReceiverUserID


    23 11
    42 11
    19 -
    34 23

    Note that the IDs mentioned above are the UserIDs from the User table. This table says that users 23 and 42 can send a message to user 11, user 34 can send a message to user 23, and user 19 cannot send message to anyone. How can I make a model for creating such a table?

    L R 2 Replies Last reply
    0
    • A Alex Wright 2022

      I'm working on an ASP.NET Core web API and SQLite database. I have created a table for users of my application. The rule is that each user can send a message to some other users (creating White-List). I use the following model for Users:

      public class UserModel
      {
      [Key]
      public int UserID { get; set; }
      [Required]
      public string Username { get; set; }
      [Required]
      public string Password { get; set; }
      [Required]
      public string FirstName { get; set; }
      [Required]
      public string LastName { get; set; }
      [Required]
      public string Role { get; set; }

      //Navigation property
      public Department Department { get; set; }
      

      }

      My desired table is:

      User-SubUser table

      UserID ReceiverUserID


      23 11
      42 11
      19 -
      34 23

      Note that the IDs mentioned above are the UserIDs from the User table. This table says that users 23 and 42 can send a message to user 11, user 34 can send a message to user 23, and user 19 cannot send message to anyone. How can I make a model for creating such a table?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Having "19" in the table is pointless and just complicates the situation. The simplest "model" is to simply join "Users" and "User-SubUser" when you need to; e.g. who can x send to (join on UserID), or who might x receive from (join on ReceiverUserID). If you want the (database) server to maintain "referential integrity", that's another story.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      A 1 Reply Last reply
      0
      • L Lost User

        Having "19" in the table is pointless and just complicates the situation. The simplest "model" is to simply join "Users" and "User-SubUser" when you need to; e.g. who can x send to (join on UserID), or who might x receive from (join on ReceiverUserID). If you want the (database) server to maintain "referential integrity", that's another story.

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        A Offline
        A Offline
        Alex Wright 2022
        wrote on last edited by
        #3

        Ok, Considering the final table only includes pairs (removing 19), how can I do joining using C# and EF Core?

        L 1 Reply Last reply
        0
        • A Alex Wright 2022

          Ok, Considering the final table only includes pairs (removing 19), how can I do joining using C# and EF Core?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Your "vehicle" in this case is LINQ. [Complex Query Operators - EF Core | Microsoft Docs](https://docs.microsoft.com/en-us/ef/core/querying/complex-query-operators)

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          A 1 Reply Last reply
          0
          • L Lost User

            Your "vehicle" in this case is LINQ. [Complex Query Operators - EF Core | Microsoft Docs](https://docs.microsoft.com/en-us/ef/core/querying/complex-query-operators)

            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

            A Offline
            A Offline
            Alex Wright 2022
            wrote on last edited by
            #5

            I meant how I can create a UserSubUser table. I need a C# model to create this table (Code-First approach). UserIDs in the UserSubUser table should be linked to the UserModel table.

            L 1 Reply Last reply
            0
            • A Alex Wright 2022

              I meant how I can create a UserSubUser table. I need a C# model to create this table (Code-First approach). UserIDs in the UserSubUser table should be linked to the UserModel table.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              No, they don't. You need to look up normalization, and apply dem rules to your database. If all that is required is whitelisting, then just have a table with users who are allowed to message each other. You think from C#; but the layer your working in is SQL, where you need to think SQL. Normalize them tables, work from there.

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              1 Reply Last reply
              0
              • A Alex Wright 2022

                I'm working on an ASP.NET Core web API and SQLite database. I have created a table for users of my application. The rule is that each user can send a message to some other users (creating White-List). I use the following model for Users:

                public class UserModel
                {
                [Key]
                public int UserID { get; set; }
                [Required]
                public string Username { get; set; }
                [Required]
                public string Password { get; set; }
                [Required]
                public string FirstName { get; set; }
                [Required]
                public string LastName { get; set; }
                [Required]
                public string Role { get; set; }

                //Navigation property
                public Department Department { get; set; }
                

                }

                My desired table is:

                User-SubUser table

                UserID ReceiverUserID


                23 11
                42 11
                19 -
                34 23

                Note that the IDs mentioned above are the UserIDs from the User table. This table says that users 23 and 42 can send a message to user 11, user 34 can send a message to user 23, and user 19 cannot send message to anyone. How can I make a model for creating such a table?

                R Offline
                R Offline
                Richard Deeming
                wrote on last edited by
                #7

                Alex Wright 2022 wrote:

                public string Password { get; set; }

                You appear to be storing your users' passwords in plain text. Don't do that! Secure Password Authentication Explained Simply[^] Salted Password Hashing - Doing it Right[^] ASP.NET already provides a built-in authentication system which does the right thing for you: Introduction to Identity on ASP.NET Core | Microsoft Learn[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups