首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Entity Framework 6 asp.net代码优先设置

Entity Framework 6 asp.net代码优先设置
EN

Stack Overflow用户
提问于 2015-10-25 10:01:13
回答 1查看 69关注 0票数 0

嗨,伙计们,我正在旋转这个轮子。我使用的是EF6和ASP.Net 4.6。我有一个给定的表,其中包含学生信息和家长信息。一个学生可以有多个家长,一个家长可以有多个学生。将此表称为“Contact”。我将创建一个名为' request‘的表,其中包含家长为其学生提交请求的信息。我将创建一个包含两列的查找表,一列用于学生id,另一列用于名为'StudentParents‘的家长id。我希望能够让家长登录,从所有学生的下拉列表中选择他的学生并提交请求。多对多的关系在我的include语句中被抛到了一边。如何让EF设置此结构,以便当I GetRequest(id)时,我可以获取要包括的学生信息和家长信息?以下是我的代码,它不包含除请求之外的任何内容。

代码语言:javascript
运行
复制
    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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-10-29 01:10:35

改变了策略。所发出的请求有许多联系人。因此,我向请求添加了一个构造函数:

代码语言:javascript
运行
复制
public Request()
{
    Contacts = new List<Contact>();
}
public virtual ICollection<Contact> Contacts { get; set; }

接下来,我更改了contact类:

代码语言:javascript
运行
复制
public int ContactId { get; set; }

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

有了这种关系,我可以从与请求相关联的联系人中同时拉出家长和学生。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33325417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档