我有這些模型public class WarehouseAllocation{ public int Id { get; set; } public Warehouse Warehouse { get; set; } public IList<Dispatch> Dispatches { get; set; } //---- removed other properties for brevity}public class Dispatch { public int Id { get; set; } public IList<DispatchDetail> DispatchDetails { get; set; } //---- removed other properties for brevity}在數(shù)據(jù)庫(kù)上,Dispatch 有一個(gè)引用 WarehouseAllocation 表的外鍵 WarehouseAllocationId。我使用 Fluent API 將模型映射到數(shù)據(jù)庫(kù),如下所示:modelBuilder.Entity<WarehouseAllocation>(m =>{ m.ToTable("WarehouseAllocation"); m.Property(wa => wa.Id).HasColumnName("WarehouseAllocationId") .ValueGeneratedOnAdd(); m.HasKey(wa => wa.Id); m.HasOne(wa => wa.Warehouse) .WithMany() .HasForeignKey("WarehouseId"); m.HasMany(w => w.Dispatches) .WithOne();});modelBuilder.Entity<Dispatch>(m =>{ m.ToTable("Dispatch"); m.Property(wa => wa.Id).HasColumnName("DispatchId") .ValueGeneratedOnAdd(); m.HasKey(wa => wa.Id);});當(dāng)我調(diào)用 時(shí) dbContext.WarehouseAllocations .Include(w => w.Dispatches) .ThenInclude(w => w.DispatchDetails).ToList(),Ef 核心檢索所有倉(cāng)庫(kù)分配及其調(diào)度,包括詳細(xì)信息。問(wèn)題是當(dāng)我使用這種方法時(shí):var warehouseAllocation = dbContext.WarehouseAllocations .Include(w => w.Dispatches) .ThenInclude(d => d.DispatchDetails) .SingleOrDefault(w => w.Id == warehouseAllocationId);warehouseAllocation.Dispatches.Add(new Dispatch{ //--including other properties DispatchDetails = new List<DispatchDetail> { new DispatchDetail { //--other properties } }});// call another query that includes WarehouseAllocationdbContext.ChangeTracker.HasChanges() // this is falsedbContext.SaveChanges() // this keeps returning zero為什么沒(méi)有檢測(cè)到變化?
Ef 核心不跟蹤子集合的更改
慕桂英3389331
2021-10-31 19:13:06