我有這樣的模式:一個零到多個級聯刪除
public class FileUpload
{
public int FileUploadId { get; set; }
public string FileName { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public string Name { get; set; }
public FileUpload Logo { get; set; }
public int? LogoId { get; set; }
public FileUpload Catalog { get; set; }
public int? CatalogId { get; set; }
}
public class Ads
{
public int AdsId { get; set; }
public string Name { get; set; }
public FileUpload Picture { get; set; }
public int? PictureId { get; set; }
}
public class TestContext : DbContext
{
public DbSet<Company> Companies { get; set; }
public DbSet<Ads> Adses { get; set; }
public DbSet<FileUpload> FileUploads { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Ads>()
.HasOptional(a => a.Picture)
.WithMany()
.HasForeignKey(a => a.PictureId)
.WillCascadeOnDelete();
modelBuilder.Entity<Company>()
.HasOptional(a => a.Logo)
.WithMany()
.HasForeignKey(a => a.LogoId)
.WillCascadeOnDelete();
modelBuilder.Entity<Company>()
.HasOptional(a => a.Catalog)
.WithMany()
.HasForeignKey(a => a.CatalogId)
.WillCascadeOnDelete();
}
}
這個代碼是不是EF遷移工作!
我想要: - 每個公司都有可選的徽標或目錄,每個廣告都有可選的圖片。 - FileUpload記錄可以直接刪除 - 級聯刪除
如何做到這一點?
解釋一下你的_not working_ – Eranga