32

如何在我的模型设计中指定 ON DELETE NO ACTION 外键约束?

目前,我有:

public class Status
{
    [Required]
    public int StatusId { get; set; }

    [Required]
    [DisplayName("Status")]
    public string Name { get; set; }
}

public class Restuarant
{
    public int RestaurantId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    public string Telephone { get; set; }
    [Required]
    public int StatusId { get; set; }
    public List<Menu> Menus { get; set; }

    // NAVIGATION PROPERTIES
    public virtual Status Status { get; set; }
}

public class Menu
{
    public int MenuId { get; set; }

    [Required]
    public int RestaurantId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public int StatusId { get; set; }

    // NAVIGATION PROPERTIES
    public virtual Status Status { get; set; }
    public virtual Restaurant Restaurant { get; set; }
}

还有我的 DbContext:

public class MenuEntities : DbContext
{
    public DbSet<Status> Statuses { get; set; }
    public DbSet<Restaurant> Restaurants { get; set; }
    public DbSet<Menu> Menus { get; set; }
}

如你看到的:

  • 一家餐厅有很多菜单
  • 餐厅有一个状态
  • 菜单属于 1 家餐厅
  • 餐厅和菜单都有 1 个状态。(现场、隐形、草稿)

自然,如果状态被删除,我当然不想级​​联,因为这会搞砸一切。

更新:

Mark Oreta 在下面的示例中提到使用以下内容:

modelBuilder.Entity<FirstEntity>() 
    .HasMany(f => f.SecondEntities) 
    .WithOptional() 
    .WillCascadeOnDelete(false); 

我把这段代码放在哪里?在我的 MenuEntities / DbContext 类中?任何人都可以提供一个使用这个的例子吗?

更新: 现在开始工作了,但是这在尝试为数据库播种时产生了多重性约束错误......

Multiplicity constraint violated. The role 'Menu_Status_Source' of the relationship 'LaCascadaWebApi.Models.Menu_Status' has multiplicity 1 or 0..1.

我的数据库初始化程序:

http://pastebin.com/T2XWsAqk

4

5 回答 5

61

您可以通过删除 OnModelCreating 方法中的级联删除约定来为整个上下文禁用它:

  protected override void OnModelCreating( DbModelBuilder modelBuilder )
  {
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
  }

或者,您可以使用流畅的映射(也在 OnModelCreating 中)为每个关系执行此操作:

编辑:你会把它放在你的菜单实体中

public class MenuEntities : DbContext
{
    public DbSet<Status> Statuses { get; set; }
    public DbSet<Restaurant> Restaurants { get; set; }
    public DbSet<Menu> Menus { get; set; }

      protected override void OnModelCreating( DbModelBuilder modelBuilder )
      {

         modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

     modelBuilder.Entity<Menu>()
        .HasRequired( f => f.Status )
        .WithRequiredDependent()
        .WillCascadeOnDelete( false );

     modelBuilder.Entity<Restaurant>()
        .HasRequired( f => f.Status )
        .WithRequiredDependent()
        .WillCascadeOnDelete( false );

      }

}
于 2012-10-13T01:21:02.667 回答
35

只需将 FK 属性设置为可为空,然后级联删除就会消失。

public int? StatusId { get; set; }
于 2018-03-07T05:39:48.060 回答
1

对模型进行更改后,请确保通过添加 -Force 参数重新生成迁移文件。

Add-Migration MigrationName -Force

于 2015-10-22T08:55:12.697 回答
1

将此行添加到上下文中字段的末尾;

.OnDelete(DeleteBehavior.Restrict);

于 2019-03-12T13:32:04.783 回答
0

把它放到你的MenuEntities班级(来自 的班级DbContext):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 
}
于 2014-05-14T19:23:27.897 回答