EF 6 how to set two foreign keys to same table

c# entity-framework

Question

I have a table UserForms that has two foreign keys to a Countries table, but on creating my controller and create view (for the UserForms model) the two fields linking to the foreign keys do not appear. What should I do to sort this problem? Below are the two models:

public class UserForms
{
     public int Id { get; set; }

     public string FullNames { get; set; }
     public Countries IndividualsCountry { get; set; }
     public Countries BusinessCountry { get; set; }
}

public class Countries
{
     public Countries()
     {
         this.STRBusinessCountry = new HashSet<UserForms>();
         this.STRIndividualsCountry = new HashSet<UserForms>();
     }

     public int Id { get; set; }
     public string NameOfCountry { get; set; }

     [InverseProperty("IndividualsCountry")]
     public virtual ICollection<UserForm> STRIndividualsCountry { get; set; }
     [InverseProperty("BusinessCountry")]
     public virtual ICollection<UserForm> STRBusinessCountry { get; set; }
 }
1
8
2/18/2015 12:16:54 PM

Accepted Answer

The comment left by @T.Glatzer is correct. You should expose foreign key properties on your dependent entities:

public class UserForms
{
    public int Id { get; set; }

    public string FullNames { get; set; }

    public int IndividualsCountryId { get; set; }
    [ForeignKey("IndividualsCountryId")]
    public virtual Countries IndividualsCountry { get; set; }

    public int BusinessCountryId { get; set; }
    [ForeignKey("BusinessCountryId")]
    public virtual Countries BusinessCountry { get; set; }
}

Here I used int, but if either of these navigation properties are optional, you would just substitute int? or System.Nullable<int> instead (which will create an int NULL column in the database rather than an int NOT NULL).

Although EF does not require you to expose navigation properties, it is generally a good practice to. Trust me. It will help you avoid unexpected exceptions later on. In fact, some EF exception messages actually recommend exposing foreign key properties on the entity classes to help EF better figure out how to map relationships. Here is an example of one such exception. Note "Additional Information" section:

{"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.DependentTable_dbo.PrincipalTable_Id". The conflict occurred in database "DatabaseName", table "dbo.PrincipalTable", column 'Id'. The statement has been terminated."}

Additional information: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

9
2/18/2015 12:11:53 PM

Popular Answer

@danludwig thanks for expounding @T.Glatzer answer this has worked for me! thank you. my final code that is now working is

public class UserForms
    {
        public int Id { get; set; }

        public string FullNames { get; set; }
    [ForeignKey("IndividualsCountry")]
        public int? IndividualsCountryId { get; set; }
    [ForeignKey("BusinessCountry")]
        public int? BusinessCountryId { get; set; }

        public virtual Countries IndividualsCountry { get; set; }
    public virtual Countries BusinessCountry { get; set; }
    }

public class Countries
    {
        public Countries()
        {
            this.STRBusinessCountry = new HashSet<UserForms>();
            this.STRIndividualsCountry = new HashSet<UserForms>();
        }

        public int Id { get; set; }
        public string NameOfCountry { get; set; }

        [InverseProperty("IndividualsCountry")]
        public virtual ICollection<UserForms> STRIndividualsCountry { get; set; }
        [InverseProperty("BusinessCountry")]
        public virtual ICollection<UserForms> STRBusinessCountry { get; set; }
    }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow