【发布时间】:2015-10-29 01:10:35
【问题描述】:
大家好,我在这台上旋转。我正在使用 EF6 和 ASP.Net 4.6。我有一个给定的表格,其中包含学生信息和家长信息。一个学生可以有多个家长,一个家长可以有多个学生。将此表称为“联系人”。我要创建一个名为“请求”的表格,其中将保存为家长提交学生请求的信息。我将创建一个包含两列的查找表,一列用于学生 ID,另一列用于父 ID,称为“StudentParents”。我希望能够让家长登录,从所有学生的下拉列表中选择他的学生并提交请求。就我的包含语句而言,多对多关系让我失望。如何让 EF 设置此结构,以便在 GetRequest(id) 时可以获取学生信息和家长信息?这是我的代码,除了请求之外不包含任何内容。
public class Contact
{
[Key]
public int id { get; set; }
public string student_id { get; set; }//This is the Student ID
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public string grade { get; set; }
public int current_school_id { get; set; }
public string current_school_name { get; set; }
[Display(Name = "Parent Last Name")]
public string contact_first_name { get; set; }
[Display(Name = "Parent Middle Name")]
public string contact_middle_name { get; set; }
[Display(Name = "Parent Last Name")]
public string contact_last_name { get; set; }
public string contact_relationship { get; set; }
[Display(Name = "Parent Email")]
public string contact_email { get; set; }
[Display(Name = "Parent Address")]
public string login { get; set; }//This is the Parent ID
public string Classif_description { get; set; }
}
public class Request
{
[Key]
public int id { get; set; }
public Student student_id { get; set; }
public Contact login { get; set; }
[Display(Name = "First School Choice")]
public string firstSchool { get; set; }
[Display(Name = "Second School Choice")]
public string secSchool { get; set; }
[Display(Name = "Rising Grade")]
public string rising_grade { get; set; }
public DateTime ReqSubmitted { get; set; }
public string ReqStatus { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string ModifBy { get; set; }
}
public class Parent
{
public int id { get; set; }
public string contact_first_name { get; set; }
public string contact_middle_name { get; set; }
public string contact_last_name { get; set; }
public string contact_relationship { get; set; }
public string contact_email { get; set; }
public string contact_address { get; set; }
public string contact_city { get; set; }
public string contact_zip { get; set; }
[Key]
public string login { get; set; }
public string contact_pw { get; set; }
public string phone { get; set; }
public string phone_type { get; set; }
public Parent() { }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
[Key]
public int id { get; set; }
public int student_id { get; set; }
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public DateTime birthdate { get; set; }
public string gender { get; set; }
public string grade { get; set; }
public string Fed_race_description { get; set; }
public string Classif_description { get; set; }
public int current_school_id { get; set; }
public string current_school_name { get; set; }
public int home_school_id { get; set; }
public string home_school_name { get; set; }
public Student()
{
this.Parents = new HashSet<Parent>();
}
public virtual ICollection<Parent> Parents { get; set; }
}
public class OEContext : DbContext
{
public OEContext() : base("name=OEContext")
{
}
public DbSet<Request> Requests { get; set; }
public DbSet<Parent> Parents { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>()
.HasMany(s => s.Students)
.WithMany()
.Map(x =>
{
x.MapLeftKey("login");
x.MapRightKey("student_id");
x.ToTable("StudentParents");
}
);
base.OnModelCreating(modelBuilder);
}
}
【问题讨论】:
-
这些类之间有很多重复。我认为您甚至根本不需要
Contact类,而是您的Request应该同时具有Parent属性和Student属性。 -
查看此页面了解如何在 EF6 中设置许多许多关系,有或没有导航属性:msdn.microsoft.com/en-us/data/jj591620
-
是的。我倾向于拥有一个有学生的
Request。Student班级应该有一个父母名单。问题仍然是如何为以下类型的结构设置 EF6。Contact类是具有学生和家长信息的给定表的模型。这就是我问多对多的原因。 Contact 表中的记录包含同一学生的多个条目,因为他可以有多个父母(母亲和父亲)。 -
Contact类是具有学生和家长信息的给定表的模型。我还没有弄清楚如何从联系人表中提取信息。我将填充Student表、Parent表和StudentParents表,这些记录来自我在家长登录并为请求选择学生后从联系人中拉回的记录。问题仍然是如何为以下类型的结构设置 EF6。一个请求有一个联系人。我想从联系人表中的每条记录中提取学生和他的家长。 -
更改了
Request类并添加了这个`public virtual ICollectionStudents { get;放; } 公共虚拟学生 学生 { 得到;放; }`
标签: c# asp.net entity-framework