在ASP.NET MVC 4 C#Code First中指定ON DELETE NO ACTION

在ASP.NET MVC 4 C#Code First中指定ON DELETE NO ACTION,c#,asp.net-mvc,entity-framework,foreign-keys,code-first,C#,Asp.net Mvc,Entity Framework,Foreign Keys,Code First,如何在模型设计中指定“删除无动作外键”约束 目前,我有: public class Status { [Required] public int StatusId { get; set; } [Required] [DisplayName("Status")] public string Name { get; set; } } public class Restuarant { public int RestaurantId { get; s

如何在模型设计中指定“删除无动作外键”约束

目前,我有:

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; }
}
我的数据库初始化器:


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

  protected override void OnModelCreating( DbModelBuilder modelBuilder )
  {
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
  }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
或者,您可以使用流畅的映射(也可以在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 );

      }

}
公共类菜单项:DbContext
{
公共数据库集状态{get;set;}
公共DbSet餐厅{get;set;}
公共数据库集菜单{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
modelBuilder.Entity()
.HasRequired(f=>f.状态)
.WithRequiredDependent()
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasRequired(f=>f.状态)
.WithRequiredDependent()
.WillCascadeOnDelete(假);
}
}

将其放入您的
MenuEntities
类(从
DbContext
派生的类):

模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}

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


添加Migration MigrationName-Force

只需将FK属性设置为空,那么级联删除将消失

公共int?StatusId{get;set;}
将此行添加到上下文中字段的末尾


.OnDelete(DeleteBehavior.Restrict)

OnModelCreating方法在哪里?我怎么能找到它?更新了我的答案-但是你可以把它放在你的Menuntities类中,你不需要同时删除约定和流畅映射(选择其中一个或另一个),但我将两者都放在这里,这样你就可以看到你将如何使用它们。谢谢Mark,更新了,但仍然有问题。。。见问题更新更新我的流利-我第一次设置错误,我道歉!谢谢,已更新,但仍然是相同的错误。我想这可能是因为我是如何建立餐厅模式的,我是如何尝试为菜单播种的???另一个问题?最快最简单的方法你的答案是1000张赞成票。但是抱歉,由于stackoverflow网站验证逻辑,我只给出了一个。
Multiplicity constraint violated. The role 'Menu_Status_Source' of the relationship 'LaCascadaWebApi.Models.Menu_Status' has multiplicity 1 or 0..1.
  protected override void OnModelCreating( DbModelBuilder modelBuilder )
  {
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
  }
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 );

      }

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