我有这个模型:
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 记录可以直接删除 - 级联删除
这该怎么做?!