How to know which Id belongs to which object?
-
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.
-
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.
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.
-
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.
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.
-
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.
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
-
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
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
-
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
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
-
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
Thank you Deeming - thanks a lot my friend :)