子集合未在EntityFrameworkCore中更新

.net c# entity-framework-core uwp win-universal-app

考慮以下具有嵌套一對多關係的簡化模型:

public class Report
{
    public Guid ReportId { get; set; }
    public string Name { get; set; }
    public List<Schedule> Schedules { get; set; }
}

public class Schedule
{
    public Guid ScheduleId { get; set; }
    public string Title { get; set; }
    public List<Description> Descriptions { get; set;}

    // FK
    public Guid ReportId { get; set; }
    public Report Report { get; set; }
}

public class Description
{
    public Guid DescriptionId { get; set; }
    public string Content { get; set; }

    // FK
    public Guid ScheduleId { get; set; }
    public Schedule Schedule { get; set; }
}

public class DataContext : DbContext
{
    public DbSet<Report> Reports { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=mydbname.db");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Report>()
            .HasMany(report => report.Schedules)
            .WithOne(schedule => schedule.Report);
        modelBuilder.Entity<Schedule>()
            .HasOne(schedule => schedule.Report)
            .WithMany(report => report.Schedules)
            .OnDelete(DeleteBehavior.Cascade);
        modelBuilder.Entity<Description>()
            .HasOne(description => description.Schedule)
            .WithMany(schedule => schedule.Descriptions)
            .OnDelete(DeleteBehavior.Cascade);
    }
}

按預期添加工作,每個項目都插入到每個表中:

Description description = new Description();
description.DescriptionId = Guid.NewGuid();
description.Content = "my content";

Schedule schedule = new Schedule();
schedule.ScheduleId = Guid.NewGuid();
schedule.Title = "my schedule";
schedule.Descriptions = new List<Description>();
schedule.Descriptions.Add(description);

Report report = new Report;
report.ReportId = Guid.NewGuid();
report.Name = "my report";
report.Schedules = new List<Schedule>();
report.Schedules.Add(schedule);

using(var db = new DataContext())
{
    db.Reports.Add(report);
    db.SaveChanges();
}

刪除實體也按預期工作,所有項目都在每個表中刪除:

using(var db = new DataContext())
{
    db.Reports.Remove(report);
    db.SaveChanges();
}

但是,更新實體僅在我更改現有項目的內容時才有效,而不是在我在集合上添加新項目時。例如:

// this works
report.Name = "updated report";
report.Schedules.ElementAt(0).Descriptions.ElementAt(0).Content = "updated content";
using(var db = new DataContext())
{
    db.Reports.Update(report);
    db.SaveChanges();
}

// this throws DbUpdateConcurrencyException because no rows are affected.
Description newDescription = new Description();
newDescription.DescriptionId = Guid.NewGuid();
newDescription.Content = "new content";

Schedule newSchedule = new Schedule();
newSchedule.ScheduleId = Guid.NewGuid();
newSchedule.Title = "new schedule";
newSchedule.Descriptions = new List<Description>();
newSchedule.Descriptions.Add(newDescription);

report.Schedules.Add(newSchedule);
using(var db = new DataContext())
{
    db.Reports.Update(report);
    db.SaveChanges();
}

如何正確地將項目添加到集合中並在之後更新它?我的桌子設計是否有意義?這是我獲取Report對象的方法:

List<Report> Reports;
using (var db = new DataContext())
    Reports = db.Reports
                    .Include(report => report.Schedules)
                        .ThenInclude(schedule => schedule.Descriptions)
                    .ToList()

編輯

正如Sunteen - MSFT帖子暗示的那樣,我的代碼似乎沒有任何問題。我發現在我的情況下導致問題的原因是,我不應故意生成每個實體的主鍵ID( Guid.NewGuid() )。事實證明,ID是自動生成的,問題已解決。謝謝Sunteen - MSFT。

一般承認的答案

我無法重現你的問題。我編寫的唯一代碼是獲取您未在代碼段中顯示的report對象。請確保表中存在您嘗試更新的report對象。我剛剛從Report表中獲得了第一條記錄然後更新,代碼如下,

using (var db = new DataContext())
{
    List<Report> Reports;
    Reports = db.Reports.Include(report1 => report1.Schedules)
                        .ThenInclude(schedule => schedule.Descriptions)
                    .ToList();
    var report = Reports[0];

    Description newDescription = new Description();
    newDescription.DescriptionId = Guid.NewGuid();
    newDescription.Content = "new content";

    Schedule newSchedule = new Schedule();
    newSchedule.ScheduleId = Guid.NewGuid();
    newSchedule.Title = "new schedule";
    newSchedule.Descriptions = new List<Description>();
    newSchedule.Descriptions.Add(newDescription);
    report.Schedules.Add(newSchedule);
    db.Reports.Update(report);
    db.SaveChanges();
}

我用您的代碼創建了一個演示,它運行良好,我將演示上傳到github,您可以下載測試並與您的項目進行比較以找到差異。如果無法通過比較差異解決,您可以嘗試在此演示中重現您的問題,讓我們再次幫助解決。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因