none
Multiplicity is not valid in Role RRS feed

  • Question

  • I am using Code first with no fluent API.
    I have two Entities (below), they reference each other.  Person has a main Company link and Company has main contact (person) link.
    When I run the application I get the Error message:
    test_Company_mainContact_Target: Multiplicity is not valid in Role 'test_Company_mainContact_Target' in relationship 
    'test_Company_mainContact'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the 
    Dependent Role must be '*'.

    Any help would be appreciated.


        public class test_Company
        {
            [ScaffoldColumn(false)]
            [Key]
            public Guid? guidID { get; set; }
           
            [DisplayName("Company Name")]
            public string compName { get; set; }

            [ForeignKey("mainContact")]
            public Guid? mainContact_guidID { get; set; }
            [XmlIgnore]
            public virtual test_Person mainContact { get; set; }

        }//End of class test_Company

        public class test_Person
        {
            [ScaffoldColumn(false)]
            [Key]
            public Guid? guidID { get; set; }

            [DisplayName("Persons Name")]
            public string perName { get; set; }

            [ForeignKey("mainCompany")]
            public Guid? mainCompany_guidID { get; set; }
            [XmlIgnore]
            public virtual test_Company mainCompany { get; set; }

        }//End of class test_Company

    Tuesday, November 4, 2014 8:21 PM

Answers

  • Hello charliecase,

    >>I have two Entities (below), they reference each other.  Person has a main Company link and Company has main contact (person) link.

    From your provided code demo, it seems that you are trying to create a One-to-One relationship, I am not sure if you only want to fix this error message or want to create a One-to-one foreign key associations actually, if it is the former, you could try with below demo which create a one to one relationship which shares the primary key:

    public class test_Company
    
        {
    
            [ScaffoldColumn(false)]
    
            [Key]
    
            public int guidID { get; set; }
    
    
            [DisplayName("Company Name")]
    
            public string compName { get; set; }
    
            public virtual test_Person mainContact { get; set; }
    
    
        }//End of class test_Company
    
        public class test_Person
    
        {
    
            [ScaffoldColumn(false)]
    
            [Key]
    
            public int guidID { get; set; }
    
            [DisplayName("Persons Name")]
    
            public string perName { get; set; }
    
            [ForeignKey("guidID")]
    
            [Required]
    
            [XmlIgnore]
    
            public virtual test_Company mainCompany { get; set; }
    
    
    }//End of class test_Company
    


    If the later, One-to-one foreign key associations are not supported by default in Entitiy Framework, for support it, we need to do a little more work, for details, you could refer to this link:

    Associations in EF Code First: Part 5 – One-to-One Foreign Key Associations

    Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Charlie Case Wednesday, November 5, 2014 7:23 PM
    Wednesday, November 5, 2014 2:25 AM
    Moderator

All replies

  • Hello charliecase,

    >>I have two Entities (below), they reference each other.  Person has a main Company link and Company has main contact (person) link.

    From your provided code demo, it seems that you are trying to create a One-to-One relationship, I am not sure if you only want to fix this error message or want to create a One-to-one foreign key associations actually, if it is the former, you could try with below demo which create a one to one relationship which shares the primary key:

    public class test_Company
    
        {
    
            [ScaffoldColumn(false)]
    
            [Key]
    
            public int guidID { get; set; }
    
    
            [DisplayName("Company Name")]
    
            public string compName { get; set; }
    
            public virtual test_Person mainContact { get; set; }
    
    
        }//End of class test_Company
    
        public class test_Person
    
        {
    
            [ScaffoldColumn(false)]
    
            [Key]
    
            public int guidID { get; set; }
    
            [DisplayName("Persons Name")]
    
            public string perName { get; set; }
    
            [ForeignKey("guidID")]
    
            [Required]
    
            [XmlIgnore]
    
            public virtual test_Company mainCompany { get; set; }
    
    
    }//End of class test_Company
    


    If the later, One-to-one foreign key associations are not supported by default in Entitiy Framework, for support it, we need to do a little more work, for details, you could refer to this link:

    Associations in EF Code First: Part 5 – One-to-One Foreign Key Associations

    Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Charlie Case Wednesday, November 5, 2014 7:23 PM
    Wednesday, November 5, 2014 2:25 AM
    Moderator
  • Thanks

    The link to Part 5 was just what I needed!

    Wednesday, November 5, 2014 7:24 PM