1

我正在构建报告系统,但从数据库中删除数据时遇到问题。

public class Report
{
    [Key]
    public int ReportId { get; set; }
    public string Title { get; set; }
    public int? DateRange { get; set; }
    public int Layout { get; set; }
    public DateTime? DateFrom { get; set; }
    public DateTime? DateTo { get; set; }
    public int OwnerId { get; set; }
    public DateTime DateCreated { get; set; }
    public bool Active { get; set; } 
    public virtual List<ReportCharts> ReportCharts { get; set; }
    public virtual List<ReportElements> ReportElements { get; set; }
}

public class ReportElements
{
    [Key, Column(Order = 1)]
    public int ReportId { get; set; }
    [Key, Column(Order = 2)]
    public string ElementName { get; set; }
    public Boolean Active { get; set; }
}

public class ReportCharts
{
    [Key, Column(Order = 1)]
    public int ReportId { get; set; }
    [Key, Column(Order = 2)]
    public string ChartId { get; set; }
    public Boolean Active { get; set; }
}

在 DbContext 中:

    public DbSet<Report> Reports { get; set; }

    // Report entity mapping
    protected virtual void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Report>().Property(t => t.ReportId).HasColumnName("ReportId");
        modelBuilder.Entity<Report>().Property(t => t.Title).HasColumnName("Title");
        modelBuilder.Entity<Report>().Property(t => t.DateRange).HasColumnName("DateRange");
        modelBuilder.Entity<Report>().Property(t => t.Layout).HasColumnName("Layout");
        modelBuilder.Entity<Report>().Property(t => t.DateFrom).HasColumnName("DateFrom");
        modelBuilder.Entity<Report>().Property(t => t.DateTo).HasColumnName("DateTo");
        modelBuilder.Entity<Report>().Property(t => t.OwnerId).HasColumnName("OwnerId");
        modelBuilder.Entity<Report>().Property(t => t.DateCreated).HasColumnName("DateCreated");
        modelBuilder.Entity<Report>().Property(t => t.Active).HasColumnName("Active");
        modelBuilder.Entity<Report>().HasMany(t => t.ReportElements).WithRequired().HasForeignKey(c => c.ReportId);
        modelBuilder.Entity<Report>().HasMany(t => t.ReportCharts).WithRequired().HasForeignKey(p => p.ReportId);
        modelBuilder.Entity<ReportElements>().Property(c => c.ElementName).HasColumnName("ElementName");
        modelBuilder.Entity<ReportElements>().HasKey(c => new { c.ReportId, c.ElementName, c.Active });
        modelBuilder.Entity<ReportCharts>().Property(p => p.ChartId).HasColumnName("ChartId");
        modelBuilder.Entity<ReportCharts>().HasKey(c => new { c.ReportId, c.ChartId, c.Active });
    }

在 EF 存储库中:

    public void RemoveReport(Report rep)
    {
        context.Reports.Remove(rep);
        context.SaveChanges();
    }

当我执行删除时,它只从报告表中删除,而不是从所有三个表中删除。当我保存报告时,它会保存在所有三个表中。所以真的不明白什么是错的。

非常感谢您的帮助

4

2 回答 2

1

您需要查看 Entity Framework 中的级联删除选项,并在需要时应用它们。

于 2013-09-11T14:55:28.947 回答
0

尝试这个:

modelBuilder.Entity<Report>().HasMany(t => t.ReportElements)
                            .WithRequired()
                            .HasForeignKey(c => c.ReportId)
                            .WillCascadeOnDelete(true);

modelBuilder.Entity<Report>().HasMany(t => t.ReportCharts)
                            .WithRequired()
                            .HasForeignKey(p => p.ReportId)
                            .WillCascadeOnDelete(true);         
于 2013-09-11T14:58:05.393 回答