design issues for my project for entity model and view

arsar 121 Reputation points
2022-01-23T08:15:51.237+00:00

I am trying to describe the requirements of my project. Hope you will be able to understood it.

In my project there are three primary data models, User table, address table and relationship table.

One user will have only one address so it is one-to-one relationship but one user can have more than one relationships so it is one-to-many relationship.

But my project is code-first approach, so I have written classes for User, Address and Relationship.

I have to insert all the details using a single form. There will text box for name and dropdown for selecting address of the user. Then there will be button for relationship, when the user press the relationship button, dropdown for selecting relationship will be shown and in the text box relationship name. Please see the attached images for details.

So how to design my model classes where I can insert my required data using single form.

namespace UI.Models  
{  
    [Table("User_tbl")]  
    public class User  
    {  
        [Key]  
        public int UserId { get; set; }  
        [Required]  
        public string Name { get; set; }  
    }  
}  
  
namespace UI.Models  
{  
    [Table("Address_tbl")]  
    public class Address  
    {  
        [Key]  
        public int AddressId { get; set; }  
        [Required]  
        public string Address_Name { get; set; }  
    }  
}  
  
namespace UI.Models  
{  
    [Table("Relationship_tbl")]  
    public class Relationship  
    {  
        [Key]  
        public int Relationship_Id { get; set; }  
        [Required]  
        public string Address_Name { get; set; }  
    }  
}  

167370-entity1.png

167551-entity2.png

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,417 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,290 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,071 Reputation points
    2022-01-24T03:21:52.78+00:00

    Hi @arsar ,
    Now,your relationship is one user have many relationship.It means that each relationship points to a user.You like do this:

     namespace UI.Models  
     {  
      [Table("Address_tbl")]  
         public class Address  
         {  
             [Key]  
             public int AddressId { get; set; }  
             [Required]  
             public string Address_Name { get; set; }  
      
            public virtual User Users{ get; set; }  
         }  
      
       [Table("User_tbl")]  
         public class User  
         {  
             [Key]  
             public int UserId { get; set; }  
             [Required]  
             public string Name { get; set; }  
            public virtual  Addresser Addresss{ get; set; }  
         }  
           
         [Table("Relationship_tbl")]  
         public class Relationship  
         {  
             [Key]  
             public int Relationship_Id { get; set; }  
             [Required]  
             public string Address_Name { get; set; }  
               
             public User user{ get; set; }  
         }  
     }  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,146 Reputation points
    2022-01-23T13:13:11.85+00:00

    Navigation properties are covered in any beginning level Entity Framework tutorial. Read the following reference documentation.

    Get started with Entity Framework 6
    Relationships, navigation properties, and foreign keys

    1 person found this answer helpful.
    0 comments No comments