Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- namespace CsTest.ChangeTracking
- {
- class TestContext : DbContext
- {
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlite("DataSource=test.db");
- base.OnConfiguring(optionsBuilder);
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- base.OnModelCreating(modelBuilder);
- modelBuilder.Entity<Ownership>().HasKey(t => new {t.HumanId, t.PetId});
- modelBuilder.Entity<Ownership>().HasOne(t => t.Human).WithMany(t => t.Ownerships)
- .HasForeignKey(t => t.HumanId);
- modelBuilder.Entity<Ownership>().HasOne(t => t.Pet).WithMany().HasForeignKey(t => t.PetId);
- }
- public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
- {
- Console.WriteLine("TRACE");
- foreach (var entityEntry in ChangeTracker.Entries())
- {
- Console.WriteLine("[{0}] entity ({1})", entityEntry.State, entityEntry.Entity.ToString());
- }
- Console.WriteLine("ENDTRACE");
- return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
- }
- }
- class Pet
- {
- [Key]
- public Guid Id { get; set; }
- public string Name { get; set; }
- public override string ToString()
- {
- return string.Format("{0}({1})", GetType().Name, Id);
- }
- }
- class Human
- {
- [Key]
- public Guid Id { get; set; }
- public string Name { get; set; }
- public virtual HashSet<Ownership> Ownerships { get; set; }
- public override string ToString()
- {
- return string.Format("{0}({1})", GetType().Name, Id);
- }
- public void AddPets(params Guid[] ids)
- {
- ids.Select(t => new Ownership()
- {
- PetId = t,
- HumanId = Id
- }).ToList()
- .ForEach(t => Ownerships.Add(t));
- }
- }
- class Ownership
- {
- public Guid PetId { get; set; }
- public Pet Pet { get; set; }
- public Human Human { get; set; }
- public Guid HumanId { get; set; }
- public override string ToString()
- {
- return string.Format("{0}({1}-{2})", GetType().Name, HumanId, PetId);
- }
- public override bool Equals(object b)
- {
- var a = b as Ownership;
- Console.WriteLine(b);
- return b is Ownership o
- && o.HumanId == HumanId && o.PetId == PetId;
- }
- public override int GetHashCode()
- {
- return HumanId.GetHashCode() + PetId.GetHashCode();
- }
- }
- class Program
- {
- public static void Main(string[] args)
- {
- Guid kristenId;
- Guid samId;
- using (var db = new TestContext())
- {
- db.Database.EnsureCreated();
- var bucky = db.Add(new Pet() {Name = "Buck"}).Entity;
- var sam = db.Add(new Pet() {Name = "Sam"}).Entity;
- db.Add(new Human() {Name = "John"});
- var kristen = db.Add(new Human()
- {
- Name = "Kristen",
- Ownerships = new HashSet<Ownership>()
- {
- new Ownership()
- {
- PetId = bucky.Id
- }
- }
- }).Entity;
- db.SaveChangesAsync();
- kristenId = kristen.Id;
- samId = sam.Id;
- Console.WriteLine("======");
- }
- using (var db = new TestContext())
- {
- var allPets = db.Set<Pet>().Select(t => t.Id).ToArray();
- var kristen = db.Set<Human>().Include(t => t.Ownerships).FirstOrDefault(t => t.Id == kristenId);
- kristen?.AddPets(allPets);
- Console.WriteLine("Kristen now have all {0} pets", kristen.Ownerships.Count);
- db.Update(kristen);
- db.SaveChangesAsync();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment