I have a table in a SQL database which should have a relation with one of two tables, but not both.
My manager solved it by using the ID field of the table Competitor as both primary key and as foreign key for the two other tables. None of the tables have autoincremental IDs.
Problem is, when I try to add a new compeitor, it fails unless there is an entry in both of the other tables with the specified ID, which is the opposite of what I want.
Here is an illustration to show you how it's done:
I am sorry if this has been posted or answered before. I could not find anything while searching.
Best Regards Kajac
Sure just set the key as Foreign and Primary on the "dependant properties". The Competitor should have the primary key.
public class Competitor
{
[Key]
public int Id { get; set; }
}
public class Equestrain
{
[Key]
[ForeignKey("Competitor")]
public int Id{ get; set; }
public Competitor Competitor { get; set; }
}
public class TeamEquestrain
{
[Key]
[ForeignKey("Competitor")]
public int Id{ get; set; }
public Competitor Competitor { get; set; }
}
MSDN - Configuring a Required to Optional Relationship (One to Zero or One)