daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Jan 18th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Student Table
  2. - StudentId
  3. - Name
  4.  
  5. Homework Table
  6. - HomeworkId
  7. - Name
  8.  
  9. StudentHomework Table
  10. - StudentId
  11. - HomeworkId
  12.    
  13. class Student
  14. {
  15.     public int StudentId { get; set; }
  16.     public string Name { get; set; }
  17.  
  18.     public ICollection<Homework> Homeworks { get; set; }
  19. }
  20.  
  21. class Homework
  22. {
  23.     public int HomeworkId { get; set; }
  24.     public string Name { get; set; }
  25. }
  26.    
  27. modelBuilder.Entity<Student>().HasMany(u => u.Homeworks).WithMany().Map(m =>
  28. {
  29.     m.MapLeftKey("StudentId");
  30.     m.MapRightKey("HomeworkId");
  31.     m.ToTable("StudentHomework");
  32. });
  33.    
  34. var newStudent = new Student
  35. {
  36.     Name = "John Smith"
  37. };
  38.  
  39. var homework1 = DataApi.GetData(...);
  40. var homework2 = DataApi.GetData(...);
  41.  
  42. newStudent.Homeworks = new List<Homework>();
  43. newStudent.Homeworks.Add(homework1);
  44. newStudent.Homeworks.Add(homework2);
  45.  
  46. // Throws SqlException Violation of UNIQUE KEY constraint
  47. DataApi.Save<Student>(newStudent);
RAW Paste Data
Top