c# - Entity Framework 0:1 relationship isn't being mapped -
i have model of job i'm trying add optional job survey to:
public class job { [key] public int jobid { get; set; } // leaving out other fields... public virtual jobsurvey jobsurvey { get; set; } }
the job survey model looks 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 context i've added following:
modelbuilder.entity<job>() .hasoptional(s => s.jobsurvey) .withrequired(j => j.job);
when ran add-migration, script created 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 table created, has no foreign key property able go related entity.
so in sql, there no jobsurveyid property lets me survey job, , jobsurvey table doesn't have navigation property going job. can create jobsurveys, they're not linked.
what have done wrong?
edit
i've tried modifying jobsurvey follows:
[key] [foreignkey("job")] public int jobsurveyid { get; set; } public int jobid { get; set; }
no success
edit 2
have tried adding [required] navigation property, add-migration isn't picking change needs updating:
[required] public virtual job job { get; set; }
in one-to-one or one-to-zero or one relationships priamry key in dependent model foreign key parent model. there no need additional foreign key because there can 1 data linked parent, reasonable link them thier ids. in case jobsurveyid
inside jobsurvey
model foreign key 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; } // foreign key job public virtual job job { get; set; } }
be sure check this site out.
Comments
Post a Comment