EF 1-M插入问题

时间:2016-11-20 17:29:03

标签: c# entity-framework

我有两张1-M relation的表格。我在这些表中插入数据如下。一切都很好,除了它在Student表中插入重复项,我现在想停止。

foreach (var s in students)
{

    Student student = new Student();
    Class class = new Class();

    student.A = s.A;
    student.B = s.B;

    _context.Students.Add(student);

    class.D = s.D;
    class.E = s.E;
    class.F = s.F;
    class.student = student;
    _context.Class.Add(class);
}

db.saveChanges();

这是我的数据采样

public class Student
{
  public string A {get;set;}
  public string B {get; set;}

  public string D {get;set;}
  public string E {get; set;}
  public string F {get;set;}
}

students.Add(
new Student 
    {
         new Student {A = "100", B = "100", D = "101", E = "e1", F = "f1"},
         new Student {A = "100", B = "100", D = "101", E = "e2", F = "f2"}
         new Student {A = "200", B = "200", D = "201", E = "e3", F = "f3"}
         new Student {A = "200", B = "200", D = "202", E = "e4", F = "f4"}
    }
)

P.S。我没有使用EF 6.我使用的是旧版本。

0 个答案:

没有答案