0

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; }
Evonet
  • 3,600
  • 4
  • 37
  • 83

3 Answers3

1

Since I would have to write a lot in the comment, try this, its according to the link I replied but I don't think you followed it right:

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; }

    [ForeignKey("Job")]
    public int JobId { get; set; }

    public virtual Job Job { get; set; }
}

Edit I'm not entirely sure whether you want 0:1 or 1:1 so here are all the possibilities (in links):

1:1

Entity Framework 1 to 1 relationship using code first. how?

EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship

0:1

Is it possible to capture a 0..1 to 0..1 relationship in Entity Framework?

Entity Framework 0..1 to 0 relation

Basically this could be your solution:

public class JobSurvey
{
    [Key, ForeignKey("First")]
    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; }
}
Community
  • 1
  • 1
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
  • Trying this way, I get the following error when adding a migration: One or more validation errors were detected during model generation: JobSurvey_Job_Source: : Multiplicity is not valid in Role 'JobSurvey_Job_Source' in relationship 'JobSurvey_Job'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. – Evonet Aug 29 '16 at 07:02
  • In *one-to-one* relationships there is no need to additional foreign key. – Adil Mammadov Aug 29 '16 at 07:02
  • Do I also need the fluent API entry? – Evonet Aug 29 '16 at 07:02
0

In one-to-one or one-to-zero or one relationships priamry key in the dependent model is also foreign key to parent model. There is no need for additional foreign key because there can only be one data linked to parent, it is reasonable to link them with thier IDs. So in your case JobSurveyId inside JobSurvey model is also foreign key to Job.

public class Job
{
    [Key]
    public int JobID { get; set; }

    public virtual JobSurvey JobSurvey { get; set; }
}

public class JobSurvey
{
    [Key]
    public int JobSurveyId { get; set; } // This is also foreign key to Job

    public virtual Job Job { get; set; }
}

Be sure to check this site out.

Adil Mammadov
  • 8,476
  • 4
  • 31
  • 59
0

you job class is okay, you only have to add ForeignKey attribute to your JobSurvey to the correct id property:

public class Job
{
    [Key]
    public int JobID { get; set; }

    public virtual JobSurvey JobSurvey { get; set; }
}

public class JobSurvey
{
    [Key, ForeignKey("Job")]
    public int JobId { get; set; }
    public Job Job { get; set; }

    /* other properties */
}
LazZiya
  • 5,286
  • 2
  • 24
  • 37