locked
ForeignKey one-to-one mapping RRS feed

  • Question

  • User-654577185 posted

    May someone correct my ForeignKey mapping as my LINQ query is not be able to run due to the wrong mapping?

    My goal is to make the attribute Marketer as a usable User object rather than just the marketer Id attribute.   I thought ForeignKey("Marketer") would feed the MARKETER_ID value to the public virtual User Marketer function.  Apparently this is not the right syntax.

    Can someone please pinpoint the error?

    Here is my code.

        [Table("USER")]
        public class User
        {
            private string userId;
            [Key, Column("USER_ID")]
            public string UserId
            {
                get { return userId; }
                set { userId = value; }
            }
    
            public virtual ICollection<UserMarketer> Marketers { get; set; }
        }
    
        [Table("USER_MARKETER")]
        public class UserMarketer
        {
            private string userId;
            [Key, Column("USER_ID", Order = 0)]
            public string UserId
            {
                get { return userId; }
                set { userId = value; }
            }
    
            private string marketerId;
            [Key, Column("MARKETER_ID", Order = 1)]
            [ForeignKey("Marketer")]
            public string MarketerId
            {
                get { return marketerId; }
                set { marketerId = value; }
            }
    
            public virtual User Marketer { get; set; }
    		
            [InverseProperty("Marketers")]
            public virtual User User { get; set; }
        }
    	

    Wednesday, July 15, 2015 2:29 PM

All replies

  • User-84896714 posted

    Hi User_Stephen,

    Welcome to ASP.NET  forums. Try below code.

        [Table("USER")]
        public class User
        {
            private string userId;
            [Key, Column("USER_ID")]
            public string UserId
            {
                get { return userId; }
                set { userId = value; }
            }
    
            public virtual ICollection<UserMarketer> Marketers { get; set; }
        }
    
        [Table("USER_MARKETER")]
        public class UserMarketer
        {
            private string userId;
            [Column("USER_ID", Order = 0)]
            public string UserId
            {
                get { return userId; }
                set { userId = value; }
            }
    
            private string marketerId;
            [Key, Column("MARKETER_ID", Order = 1)]
            public string MarketerId
            {
                get { return marketerId; }
                set { marketerId = value; }
            }
            [ForeignKey("UserId")]
            public virtual User Marketer { get; set; }
        }

    For more information about table mapping on entity framework, link below is for your reference.
    http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

    Best Regards,
    Wang Li

    Thursday, July 16, 2015 3:29 AM
  • User-654577185 posted

    I think you are changing meaning of the relationships.

    I want the following virtual function to use the MARKETER_ID as the foreign key to the User table:
            public virtual User Marketer { get; set; }

    The other virtual function (public virtual User User { get; set; }) works fine.  It can be mapped using either [ForeignKey("UserId")] or [InverseProperty("Marketers")]

    The problem I am having is the MARKETER_ID.  The MARKETER_ID does not help me enough.  I want a full object using the MARKETER_ID field.
    That is why I have the virtual function (public virtual User Marketer { get; set; }) but it did not work.

            private string marketerId;
            [Key, Column("MARKETER_ID", Order = 1)]
            [ForeignKey("Marketer")]
            public string MarketerId
            {
                get { return marketerId; }
                set { marketerId = value; }
            }

            public virtual User Marketer { get; set; }

    Thursday, July 16, 2015 9:24 AM
  • User-84896714 posted

    Hi User_Stephen,

    Thank you for your post. Entity framework map are base on sql server tables relationship, if you are confused with entity map, you could create tables first and post your table definition.

    Best Regards,
    Wang Li

    Friday, July 24, 2015 5:48 AM