实体框架6 asp.net代码优先安装

时间:2015-10-25 02:01:13

标签: c# asp.net entity-framework

嗨,伙计们,我正在为这一个转动轮子。我使用的是EF6和ASP.Net 4.6。我有一个给定的表,其中包含学生信息和父母信息。学生可以有很多父母,父母可以有很多学生。请拨打此表'联系'。我要创建一个名为' Request'这将保存父母提交学生请求的信息。我将创建一个包含两列的查找表,一列用于学生ID,另一列用于父ID,名为“StudentParents”#39;我希望能够让父母登录,从他所有学生的下拉列表中选择他的学生并提交请求。对于我的包含声明,多对多的关系让我失望。我怎样才能让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);
    }
}

1 个答案:

答案 0 :(得分:0)

改变了策略。 Made Request有很多联系方式。所以我在请求中添加了一个构造函数:

public int ContactId { get; set; }

public Contact() { }
public virtual Request Request { get; set; }

接下来我更改了联系人类:

caret

通过这种关系,我可以从与请求相关联的联系人中提取父母和学生。