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. .NET (Core and Framework)
  4. How to know which Id belongs to which object?

How to know which Id belongs to which object?

Scheduled Pinned Locked Moved .NET (Core and Framework)
tutorialquestiondatabasehelp
7 Posts 3 Posters 2 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.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    Hi all - I have 3 foreign key ids and they are coming from same table, so they are referenced with table names in the below class, now - how can I know which object refers to which ID - for example the Ids

    public partial class InspectionResult
    {
        \[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")\]
        public InspectionResult()
        {
            this.EventLogs = new HashSet();
            this.InspectionItems = new HashSet();
            this.InspectionResultStatCounts = new HashSet();
            this.NOVs = new HashSet();
            this.UploadedDocuments = new HashSet();
            this.AspNetUsers = new HashSet();
        }
    
        public int InspectionResultId { get; set; }
       
        public string EnteredById { get; set; }
       
        public string CreatedByUserId { get; set; }
        public string UpdatedByUserId { get; set; }
    
        public virtual AspNetUser AspNetUser { get; set; }
        public virtual AspNetUser AspNetUser1 { get; set; }
        public virtual AspNetUser AspNetUser2 { get; set; }
        
        public virtual ICollection AspNetUsers { get; set; }
    }
    

    EnteredById, CreatedByUserId, UpdatedByUserId are coming from same table AspNetUser, but how would I know which Id belongs to AspNetUser, AspNetUser1, ,AspNetUser2 and how to handle AspNetUsers object? I am using EF Database First Approach any help please - thank you.

    L Richard DeemingR 2 Replies Last reply
    0
    • S simpledeveloper

      Hi all - I have 3 foreign key ids and they are coming from same table, so they are referenced with table names in the below class, now - how can I know which object refers to which ID - for example the Ids

      public partial class InspectionResult
      {
          \[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")\]
          public InspectionResult()
          {
              this.EventLogs = new HashSet();
              this.InspectionItems = new HashSet();
              this.InspectionResultStatCounts = new HashSet();
              this.NOVs = new HashSet();
              this.UploadedDocuments = new HashSet();
              this.AspNetUsers = new HashSet();
          }
      
          public int InspectionResultId { get; set; }
         
          public string EnteredById { get; set; }
         
          public string CreatedByUserId { get; set; }
          public string UpdatedByUserId { get; set; }
      
          public virtual AspNetUser AspNetUser { get; set; }
          public virtual AspNetUser AspNetUser1 { get; set; }
          public virtual AspNetUser AspNetUser2 { get; set; }
          
          public virtual ICollection AspNetUsers { get; set; }
      }
      

      EnteredById, CreatedByUserId, UpdatedByUserId are coming from same table AspNetUser, but how would I know which Id belongs to AspNetUser, AspNetUser1, ,AspNetUser2 and how to handle AspNetUsers object? I am using EF Database First Approach any help please - thank you.

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

      There should be no "AspNetUser1" or "AspNetUser2" tables. I'd delete that code, regardless of who wrote it. Yuck. A thousand times, yuck. Normalize your database. There should only be one table with users, and then things become easy. If you can't normalize, you deserve the problems. Either learn or drown. If that is too hard (meaning, no one in your company willing to invest two days), then the best way is to suggest that a "AspNetUser3" table solves all. They asking for it.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      S 1 Reply Last reply
      0
      • L Lost User

        There should be no "AspNetUser1" or "AspNetUser2" tables. I'd delete that code, regardless of who wrote it. Yuck. A thousand times, yuck. Normalize your database. There should only be one table with users, and then things become easy. If you can't normalize, you deserve the problems. Either learn or drown. If that is too hard (meaning, no one in your company willing to invest two days), then the best way is to suggest that a "AspNetUser3" table solves all. They asking for it.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        S Offline
        S Offline
        simpledeveloper
        wrote on last edited by
        #3

        That's one table referenced 3 times in another table hence the Entity Framework created 3 different objects - its not a problem on Normalization - I am asking about Entity Framework solution for it - any help my friend. I am using Entity Framework Database first approach.

        1 Reply Last reply
        0
        • S simpledeveloper

          Hi all - I have 3 foreign key ids and they are coming from same table, so they are referenced with table names in the below class, now - how can I know which object refers to which ID - for example the Ids

          public partial class InspectionResult
          {
              \[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")\]
              public InspectionResult()
              {
                  this.EventLogs = new HashSet();
                  this.InspectionItems = new HashSet();
                  this.InspectionResultStatCounts = new HashSet();
                  this.NOVs = new HashSet();
                  this.UploadedDocuments = new HashSet();
                  this.AspNetUsers = new HashSet();
              }
          
              public int InspectionResultId { get; set; }
             
              public string EnteredById { get; set; }
             
              public string CreatedByUserId { get; set; }
              public string UpdatedByUserId { get; set; }
          
              public virtual AspNetUser AspNetUser { get; set; }
              public virtual AspNetUser AspNetUser1 { get; set; }
              public virtual AspNetUser AspNetUser2 { get; set; }
              
              public virtual ICollection AspNetUsers { get; set; }
          }
          

          EnteredById, CreatedByUserId, UpdatedByUserId are coming from same table AspNetUser, but how would I know which Id belongs to AspNetUser, AspNetUser1, ,AspNetUser2 and how to handle AspNetUsers object? I am using EF Database First Approach any help please - thank you.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          If you want the Entity Framework conventions to work here, you'd need to name your navigation properties accordingly:

          public string EnteredById { get; set; }
          public virtual AspNetUser EnteredBy { get; set; }

          public string CreatedByUserId { get; set; }
          public virtual AspNetUser CreatedByUser { get; set; }

          public string UpdatedByUserId { get; set; }
          public virtual AspNetUser UpdatedByUser { get; set; }

          Otherwise you'll have to use either the [ForeignKey] data annotation, or the fluent API, to configure your relationships.


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

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

          S 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            If you want the Entity Framework conventions to work here, you'd need to name your navigation properties accordingly:

            public string EnteredById { get; set; }
            public virtual AspNetUser EnteredBy { get; set; }

            public string CreatedByUserId { get; set; }
            public virtual AspNetUser CreatedByUser { get; set; }

            public string UpdatedByUserId { get; set; }
            public virtual AspNetUser UpdatedByUser { get; set; }

            Otherwise you'll have to use either the [ForeignKey] data annotation, or the fluent API, to configure your relationships.


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

            S Offline
            S Offline
            simpledeveloper
            wrote on last edited by
            #5

            But its Database First - when I refresh the Entities automatically wouldn't it remove the settings that I put in my Entity classes? Just checking - thank you - how to handle if its Database first approach - thank you

            Richard DeemingR 1 Reply Last reply
            0
            • S simpledeveloper

              But its Database First - when I refresh the Entities automatically wouldn't it remove the settings that I put in my Entity classes? Just checking - thank you - how to handle if its Database first approach - thank you

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              You should be able to change the navigation property names in the model browser: c# - Entity Framework (Database-First) multiple relations to same table naming conventions control - Stack Overflow[^]


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

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

              S 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                You should be able to change the navigation property names in the model browser: c# - Entity Framework (Database-First) multiple relations to same table naming conventions control - Stack Overflow[^]


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

                S Offline
                S Offline
                simpledeveloper
                wrote on last edited by
                #7

                Thank you Deeming - thanks a lot my friend :)

                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