Guest User

Untitled

a guest
Aug 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.EntityFrameworkCore;
  8.  
  9. namespace CsTest.ChangeTracking
  10. {
  11. class TestContext : DbContext
  12. {
  13. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  14. {
  15. optionsBuilder.UseSqlite("DataSource=test.db");
  16. base.OnConfiguring(optionsBuilder);
  17. }
  18.  
  19. protected override void OnModelCreating(ModelBuilder modelBuilder)
  20. {
  21. base.OnModelCreating(modelBuilder);
  22. modelBuilder.Entity<Ownership>().HasKey(t => new {t.HumanId, t.PetId});
  23. modelBuilder.Entity<Ownership>().HasOne(t => t.Human).WithMany(t => t.Ownerships)
  24. .HasForeignKey(t => t.HumanId);
  25. modelBuilder.Entity<Ownership>().HasOne(t => t.Pet).WithMany().HasForeignKey(t => t.PetId);
  26. }
  27.  
  28. public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
  29. {
  30. Console.WriteLine("TRACE");
  31. foreach (var entityEntry in ChangeTracker.Entries())
  32. {
  33. Console.WriteLine("[{0}] entity ({1})", entityEntry.State, entityEntry.Entity.ToString());
  34. }
  35. Console.WriteLine("ENDTRACE");
  36. return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
  37. }
  38. }
  39.  
  40. class Pet
  41. {
  42. [Key]
  43. public Guid Id { get; set; }
  44. public string Name { get; set; }
  45.  
  46. public override string ToString()
  47. {
  48. return string.Format("{0}({1})", GetType().Name, Id);
  49. }
  50. }
  51.  
  52. class Human
  53. {
  54. [Key]
  55. public Guid Id { get; set; }
  56. public string Name { get; set; }
  57.  
  58. public virtual HashSet<Ownership> Ownerships { get; set; }
  59.  
  60. public override string ToString()
  61. {
  62. return string.Format("{0}({1})", GetType().Name, Id);
  63. }
  64.  
  65. public void AddPets(params Guid[] ids)
  66. {
  67. ids.Select(t => new Ownership()
  68. {
  69. PetId = t,
  70. HumanId = Id
  71. }).ToList()
  72. .ForEach(t => Ownerships.Add(t));
  73. }
  74. }
  75.  
  76. class Ownership
  77. {
  78. public Guid PetId { get; set; }
  79. public Pet Pet { get; set; }
  80.  
  81. public Human Human { get; set; }
  82. public Guid HumanId { get; set; }
  83.  
  84. public override string ToString()
  85. {
  86. return string.Format("{0}({1}-{2})", GetType().Name, HumanId, PetId);
  87. }
  88.  
  89. public override bool Equals(object b)
  90. {
  91. var a = b as Ownership;
  92. Console.WriteLine(b);
  93. return b is Ownership o
  94. && o.HumanId == HumanId && o.PetId == PetId;
  95. }
  96.  
  97. public override int GetHashCode()
  98. {
  99. return HumanId.GetHashCode() + PetId.GetHashCode();
  100. }
  101. }
  102.  
  103. class Program
  104. {
  105. public static void Main(string[] args)
  106. {
  107. Guid kristenId;
  108. Guid samId;
  109.  
  110. using (var db = new TestContext())
  111. {
  112. db.Database.EnsureCreated();
  113. var bucky = db.Add(new Pet() {Name = "Buck"}).Entity;
  114. var sam = db.Add(new Pet() {Name = "Sam"}).Entity;
  115.  
  116. db.Add(new Human() {Name = "John"});
  117. var kristen = db.Add(new Human()
  118. {
  119. Name = "Kristen",
  120. Ownerships = new HashSet<Ownership>()
  121. {
  122. new Ownership()
  123. {
  124. PetId = bucky.Id
  125. }
  126. }
  127. }).Entity;
  128.  
  129. db.SaveChangesAsync();
  130.  
  131. kristenId = kristen.Id;
  132. samId = sam.Id;
  133. Console.WriteLine("======");
  134. }
  135.  
  136. using (var db = new TestContext())
  137. {
  138. var allPets = db.Set<Pet>().Select(t => t.Id).ToArray();
  139. var kristen = db.Set<Human>().Include(t => t.Ownerships).FirstOrDefault(t => t.Id == kristenId);
  140. kristen?.AddPets(allPets);
  141.  
  142. Console.WriteLine("Kristen now have all {0} pets", kristen.Ownerships.Count);
  143. db.Update(kristen);
  144. db.SaveChangesAsync();
  145. }
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment