Entity Framework Code First : how to annotate a foreign key for a "Default" value?

data-annotations, ef-code-first, entity-framework

Solution

The problem is that when you have multiple relationships between two entities, EF Code First isn't able to find out which navigation properties match up, unless, you tell it how, here is the code:

public class Client
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public string ClientName { get; set; }

    /****Change Nullable<int> by int?, looks better****/
    public int? DefaultSurveyID { get; set; }

    /****You need to add this attribute****/
    [InverseProperty("ID")]
    [ForeignKey("DefaultSurveyID")]
    public virtual Survey DefaultSurvey { get; set; }

    public virtual ICollection<Survey> Surveys { get; set; }
}

With your previous version, EF was creating that extra relationship because it didn't know that the `DefaultSurvey` property was referencing the `ID` of the `Survey` class, but you can let it know that, adding the attribute `InverseProperty` whose parameter is the name of the property in `Survey` you need `DefaultSurvey` to match with.

Problem

I have 2 classes: Client and Survey. Each Client can have many surveys - but only one default survey. I have defined the classes like this: ``` public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string ClientName { get; set; } public Nullable<int> DefaultSurveyID { get; set; } [ForeignKey("DefaultSurveyID")] public virtual Survey DefaultSurvey { get; set; } public virtual ICollection<Survey> Surveys { get; set; } } public class Survey { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string SurveyName { get; set; } [Required] public int ClientID { get; set; } [ForeignKey("ClientID")] public virtual Client Client { get; set; } } ``` This creates the Client table as I expect: ``` [dbo].[Clients] ( [ID] [int] IDENTITY(1,1) NOT NULL, [ClientName] [nvarchar](max) NULL, [DefaultSurveyID] [int] NULL ) ``` But the Survey table has an extra foreign key: ``` [dbo].[Surveys] ( [ID] [int] IDENTITY(1,1) NOT NULL, [SurveyName] [nvarchar](max) NULL, [ClientID] [int] NOT NULL, [Client_ID] [int] NULL ) ``` Why is Code First generating this relationship and how to I tell it not to?

Original source

Related problems