Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 6 how to set two foreign keys to same table

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; }
 }
like image 619
Martin Wambugu Avatar asked Feb 18 '15 11:02

Martin Wambugu


People also ask

Can a table have 2 foreign keys to the same table?

A table can have multiple foreign keys based on the requirement.

Can 2 foreign keys reference the same primary key?

Yes, it is okay to have two fk to the same pk in one table.

Can a foreign key point to the same table?

If you mean "can foreign key 'refer' to a primary key in the same table?", the answer is a firm yes as some replied.

Can an object have multiple foreign keys?

A table can have more than one foreign key constraint. This is used to implement many-to-many relationships between tables.


1 Answers

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.

like image 200
danludwig Avatar answered Sep 18 '22 07:09

danludwig