美文网首页
【EntityFramework-5】表之间的关系

【EntityFramework-5】表之间的关系

作者: 枫雪幻林 | 来源:发表于2017-06-15 23:33 被阅读0次

1. 基本引用模式:

this.Has****** (p=>p.AAA).With******() // ******有这几个可选值:

  • Optional 可选(0或1个)
  • Required有且只有一个(1个)
  • Many(多个)

2. 一对多关系配置:

基本的插入查询(建立好外键约束):

 public class Student
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public virtual Class cls { get; set; }
        public long classId { get; set; }
    }
    public class Class
    {
        public long ID { get; set; }
        public string Name { get; set; }
    }

插入数据:

using (TestDBContent tdc = new TestDBContent())
{
    Class cls = new Class() { Name="测试1班"};
    tdc.Classes.Add(cls);
    Student stu = new Student() { 
    Age=18,
    cls=cls,
    Name="风雪幻林"
    };
    tdc.students.Add(stu);
    tdc.SaveChanges();
}
2.1 不要设计双向关系:
public class Class
{
    public long ID { get; set; }
    public string Name { get; set; }
    public ICollection<Student> students { get; set; }
}

查询数据:

Class clz = tdc.Classes.First();
var stus = tdc.students.Where(i => i.classId == clz.ID);
foreach (var item in stus)
{
    Console.WriteLine(item.Name);
}
2.2 通过Fluent API做配置:
  • 在一对多的表中(多的一端做配置)
    this. HasRequired(s=> s.Class).WithMany() .HasForeignKey(s => s.ClassId)
  • 找不到 Class_Id 字段
    需要在 ClassConfig 中再反向配置一遍

3. 多对多关系配置:

  • 推荐配置:
 public class Student
    {
        public Student()
        {
            teachers = new List<Teacher>();
        }
        public long ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public virtual ICollection<Teacher> teachers { get; set; }
    }
    public class Teacher
    {
        public Teacher()
        {
            students = new List<Student>();
        }
        public long ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Student> students { get; set; }
    }
  • Fluent API 多对多的配置:
  • 在其中一端配置
public class TeacherConfig : EntityTypeConfiguration<Teacher>
{
    public TeacherConfig()
    {
        this.ToTable("t_class");
        this.Property(e => e.Name).HasMaxLength(30).IsRequired();
        this.HasMany(e=>e.students).WithMany(i=>i.teachers).Map(m=>m.ToTable("t_teacherstudent").MapLeftKey("techerId").MapRightKey("studentId"));
    }
}
public class StudentConfig : EntityTypeConfiguration<Student>
{
    public StudentConfig()
    {
        this.ToTable("t_student");           
    }
}
  • 调用:
static void Main(string[] args)
{
    Teacher t = new Teacher() { Name = "语文老师" };
    Teacher t2 = new Teacher() { Name = "数学老师" };
    Student s = new Student() { Name = "张三", Age = 21 };
    Student s2 = new Student() { Name = "李四", Age = 22 };
    using (TestDBContent tdb =new TestDBContent ())
    {
        t.students.Add(s);
        t.students.Add(s2);
        t2.students.Add(s);
        t2.students.Add(s2);
        tdb.teachers.Add(t);
        tdb.teachers.Add(t2);
        tdb.SaveChanges();
    }
    Console.WriteLine("ok");
    Console.ReadKey();
}
  • 多对多的关系:
  • 移除关系: t.Students.Remove(t.Students.First())

相关文章

网友评论

      本文标题:【EntityFramework-5】表之间的关系

      本文链接:https://www.haomeiwen.com/subject/xjbuqxtx.html