I have a model of Job that I'm trying to add an optional job survey to:
public class Job
{
[Key]
public int JobID { get; set; }
// Leaving out all the other fields...
public virtual JobSurvey JobSurvey { get; set; }
}
The job survey model looks like this:
public class JobSurvey
{
[Key]
public int JobSurveyId { get; set; }
public string CustomerEmail { get; set; }
[Index]
[Column(TypeName = "Date")]
public DateTime? SentDate { get; set; }
[Column(TypeName = "Date")]
public DateTime? ReturnedDate { get; set; }
public int? RatingValue { get; set; }
public virtual Job Job { get; set; }
}
In my context I've added the following:
modelBuilder.Entity<Job>()
.HasOptional(s => s.JobSurvey)
.WithRequired(j => j.Job);
When I ran add-migration, the script created the following:
CreateTable(
"dbo.JobSurveys",
c => new
{
JobSurveyId = c.Int(nullable: false),
CustomerEmail = c.String(nullable: false, maxLength: 100),
SentDate = c.DateTime(storeType: "date"),
RatingValue = c.Int(),
})
.PrimaryKey(t => t.JobSurveyId)
.ForeignKey("dbo.Jobs", t => t.JobSurveyId)
My problem is that the table that was created, has no foreign key property to be able to go to the related entity.
So in my SQL, there is no JobSurveyId property that lets me get the survey for a job, and my JobSurvey table doesn't have a navigation property going back to the Job. So I can create JobSurveys, but they're not linked.
What have I done wrong?
Edit
I've tried modifying JobSurvey as follows:
[Key]
[ForeignKey("Job")]
public int JobSurveyId { get; set; }
public int JobId { get; set; }
no success
Edit 2
Have also tried adding [Required] to the navigation property, but add-migration isn't picking up this as a change that needs updating:
[Required]
public virtual Job Job { get; set; }