我有一个Job模型,我试图将可选的工作调查添加到:
public class Job
{
[Key]
public int JobID { get; set; }
// Leaving out all the other fields...
public virtual JobSurvey JobSurvey { get; set; }
}
工作调查模型如下所示:
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; }
}
在我的上下文中,我添加了以下内容:
modelBuilder.Entity<Job>()
.HasOptional(s => s.JobSurvey)
.WithRequired(j => j.Job);
当我运行add-migration时,脚本创建了以下内容:
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)
我的问题是创建的表没有外键属性可以转到相关实体。
所以在我的SQL中,没有JobSurveyId属性可以让我获得作业的调查,而我的JobSurvey表没有导航属性返回到作业。所以我可以创建JobSurveys,但他们没有联系。
我做错了什么?
修改
我尝试按如下方式修改JobSurvey:
[Key]
[ForeignKey("Job")]
public int JobSurveyId { get; set; }
public int JobId { get; set; }
没有成功
修改2
还尝试将[必需]添加到导航属性,但添加迁移并不是将此作为需要更新的更改:
[Required]
public virtual Job Job { get; set; }
答案 0 :(得分:1)
由于我必须在评论中写下很多东西,试试这个,根据我回复的链接,但我不认为你是正确的:
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; }
}
修改强> 我不完全确定你是想要0:1还是1:1所以这里有所有可能性(在链接中):
<强> 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
基本上这可能是你的解决方案:
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; }
}
答案 1 :(得分:0)
依赖模型中的一对一或一对一或一个关系 priamry密钥也是外键到父模型。不需要额外的外键,因为只有一个数据链接到父级,将它们与其ID链接是合理的。因此,在您的情况下,JobSurveyId
内JobSurvey
模型也是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; }
}
务必检查this site。
答案 2 :(得分:0)
你的工作班是可以的,你只需要将JobKurvey的ForeignKey属性添加到正确的id属性:
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 */
}