【问题标题】:Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION
【发布时间】:2012-10-03 20:24:19
【问题描述】:

如何在我的模型设计中指定 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 类中? 任何人都可以提供一个使用这个的例子吗?

更新: 现在让这个位工作,但是这在尝试为 DB 播种时产生了多重性约束错误......

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

【问题讨论】:

    标签: c# asp.net-mvc entity-framework foreign-keys code-first


    【解决方案1】:

    您可以通过删除 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 );
    
          }
    
    }
    

    【讨论】:

    • OnModelCreating 方法在哪里?我怎样才能找到它?
    • 更新了我的答案——但你可以把它放在你的 MenuEntities 类中,你不需要同时删除约定和流畅的映射(选择一个或另一个),但我把两者都可以让您了解如何使用它们。
    • 感谢马克,已更新,但仍有问题...请参阅有问题的更新
    • 更新了我的fluent - 我第一次设置错了,我很抱歉!
    • 谢谢,更新了,但还是一样的错误。我认为这可能是因为我如何设置餐厅模型以及我如何尝试为菜单播种???单独的问题?
    【解决方案2】:

    只要使 FK 属性可以为空,那么级联删除就会消失。

    public int? StatusId { get; set; }
    

    【讨论】:

    • 最快最简单的方法
    • 您的回答值得 1000 次赞成票。但;抱歉,由于 stackoverflow 网站验证逻辑,我只给出了一个。
    【解决方案3】:

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

    Add-Migration MigrationName -Force

    【讨论】:

      【解决方案4】:

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

      .OnDelete(DeleteBehavior.Restrict);

      【讨论】:

        【解决方案5】:

        把它放到你的MenuEntities 类(从DbContext 继承的类):

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-23
          • 2021-05-29
          • 2023-01-24
          • 2023-03-05
          • 1970-01-01
          相关资源
          最近更新 更多