【发布时间】:2011-10-01 19:29:37
【问题描述】:
我有以下课程
public class Subject{
public int SubjectId { get; set; }
public String SubjectName { get; set; }
public String SubjectCategory { get; set; }
}
public class QuestionDescriptor {
public int QuestionDescriptorId { get; set; }
public String QuestionText { get; set; }
public String Answer { get; set; }
public int SubjectId { get; set; }
public virtual Subject Subject { get; set; }
}
我已经使用以下代码对其进行了配置,我希望一个主题可以有多个 QuestionDescriptors
modelBuilder.Entity<QuestionDescriptor>()
.HasRequired(qd => qd.Subject)
.WithMany()
.HasForeignKey(qd => qd.SubjectId)
.WillCascadeOnDelete(true);
现在我有以下问题
- 我做得对吗?
- 我需要 Subject 类中的导航属性吗?
-
如果我这样做会发生什么
public class Subject { public int SubjectId { get; set; } public String SubjectName { get; set; } public String SubjectCategory { get; set; } public int QuestionDescriptorId {get;set;} public virtual QuestionDescriptor {get;set;} } 如果我执行上述操作,我需要对配置进行哪些更改?为什么?
- 如果我想要属于特定主题的所有问题,那么我可以通过查询 QuestionDescriptor 来获得它们,为什么我需要双向属性?
【问题讨论】:
标签: c#-4.0 entity-framework-4.1 ef-code-first