locked
Primary/Foreign key in Models RRS feed

  • Question

  • User-619898029 posted

    Hi All,

    I am having problem in assigning the value to a model property. 

    I have two models "User" and "UserInfo"

    User.cs 

    [Table("tblUsers")]
        public class User
        {
           //Primary key
            [Key]
            [Column("Users_IDPK")]
            public string UserID { get; set; }
            
            [Column("Users_Email")]
            public string Email { get; set; }
            
            [Column("Users_Password")]
            public string Password { get; set; }
    
           
    
            
        }

    Userinfo.cs

    [Table("tblUserInfo")]
        public class UserInfo
        {
            //foreign key
            public string UserIDFK { get; set; }
    
            [Column("UserInfo_FirstName")]
            public string FirstName { get; set; }
    
            [Column("UserInfo_LastName")]
            public string LastName { get; set; }
      
            [ForeignKey("Users_IDPK")]
            public virtual User Users { get; set; }
    
        }

    1. I am trying to establish Primary/Foriegn Key relationship between the two models.

    2. Also I am trying to assign value to Firstname like below which is giving an error saying it does not contain a definition for UserInfo"

    User objUserDetails = new User();
    objUserDetails.UserInfo.FirstName ="Abc";

    Can anyone help me on this?

    Thanks,

    mds2907

    Thursday, January 14, 2016 9:42 AM

Answers

  • User-271186128 posted

    Hi mds2907,

    You could use the ForeignKey attribute to assign the foreign key. Like this:

    public class Student
    {
        public Student()
        { 
            
        }
        public int StudentID { get; set; }
        public string StudentName { get; set; }
            
        //Foreign key for Standard
        public int StandardRefId { get; set; }
    
        [ForeignKey("StandardRefId")]
        public Standard Standard { get; set; }
    }
    
    public class Standard
    {
        public Standard()
        { 
            
        }
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection<Student> Students { get; set; }
       
    }

    More details, please refer to this article: http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 15, 2016 7:14 AM

All replies

  • User-821857111 posted

    I don't understand the purpose of the UserInfo class. FirstName and LastName should belong to the User class. All of the data you have spread across two classes should be in one class and stored in one table in the database. But if you must have this kind of design, you need to configure a one-to-one relationship: http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

    public class User
    {
        //Primary key
        [Key]
        [Column("Users_IDPK")]
        public string UserID { get; set; }
        
        [Column("Users_Email")]
        public string Email { get; set; }
        
        [Column("Users_Password")]
        public string Password { get; set; }
    }
    Userinfo.cs
    
    [Table("tblUserInfo")]
    public class UserInfo
    {
        [Key, ForeignKey("Users")]
        public string UserIDFK { get; set; }
    
        [Column("UserInfo_FirstName")]
        public string FirstName { get; set; }
    
        [Column("UserInfo_LastName")]
        public string LastName { get; set; }
    
        public virtual User Users { get; set; }
    }

    Thursday, January 14, 2016 1:51 PM
  • User-619898029 posted

    Hi Mike,

    The UserInfo.cs has many fields related to a single user. For simplicity I have eliminated all of those fields and presented only few of them. 

    I am using code first, and in my table I am using UserIDFK is only foreign key , I dont want it to be a primary key. Therefore I am just trying to make a model which is similar to me database tables. The only thing here I am struggling to assign values to UserInfo class properties. 

    Thanks,

    mds2907

    Thursday, January 14, 2016 2:22 PM
  • User-271186128 posted

    Hi mds2907,

    You could use the ForeignKey attribute to assign the foreign key. Like this:

    public class Student
    {
        public Student()
        { 
            
        }
        public int StudentID { get; set; }
        public string StudentName { get; set; }
            
        //Foreign key for Standard
        public int StandardRefId { get; set; }
    
        [ForeignKey("StandardRefId")]
        public Standard Standard { get; set; }
    }
    
    public class Standard
    {
        public Standard()
        { 
            
        }
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection<Student> Students { get; set; }
       
    }

    More details, please refer to this article: http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 15, 2016 7:14 AM