Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public class Patient
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public string Email ( get; set; }
  6. public virtual Nutritionist Nutritionist { get; set; }
  7. }
  8.  
  9. public class Nutritionist
  10. {
  11. public int Id { get; set; }
  12. public string Name { get; set; }
  13. public virtual List<Patient> Patients { get; set; }
  14. }
  15.  
  16. [TestMethod]
  17. public void Changing_Nutritionist_In_Disconnected_Context_Works()
  18. {
  19. Patient p;
  20. Nutritionist n;
  21. using (var c = new Context())
  22. {
  23. // Get a patient where I know p.Nutritionist.Id == 1 in database.
  24. p = c.Patients.Find(1);
  25.  
  26. // Get some other nutritionist
  27. n = c.Nutritionists.Find(3);
  28. }
  29. using (var c = new Context())
  30. {
  31. //change patient's email and nutritionist
  32. p.Email = "patient@domain.com";
  33. p.Nutritionist = n;
  34.  
  35. c.Patients.Attach(p);
  36. c.Entry(p).State = EntityState.Modified;
  37. c.SaveChanges();
  38. }
  39. using (var c = new Context())
  40. {
  41. Assert.AreEqual(3,c.Patients.Find(1).Nutritionist.Id);
  42. }
  43. }
  44.  
  45. exec sp_executesql N'UPDATE [dbo].[Patients]
  46. SET [Name] = @0, [Email] = @1
  47. WHERE ([Id] = @2)
  48. ',N'@0 nvarchar(max) ,@1 nvarchar(max) ,@2 int',@0=N'some name',@1=N'patient@domain.com',@2=1
  49. go
  50.  
  51. cn.Patients.Attach(p);
  52. cn.Nutritionists.Attach(n);
  53. cn.Entry(p).Reference(x => x.Nutritionist).Load();
  54. p.Nutritionist = n;
  55. cn.Entry(p).State = EntityState.Modified;
  56. cn.SaveChanges();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement