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

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

如何在我的模型设计中指定 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

最佳答案

您可以通过在 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 );

      }

}

关于c# - 在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868912/

相关文章:

c# - ASP.NET MVC : validation domain password

sql-server - 这种情况下最好的数据库结构是什么?

c# - 如何通过代理连接到Azure服务总线主题-C#?

c# - 如何从 byte[], 到 MemoryStream, Unzip, 然后写到 FileStream

asp.net - 在 .NET 中向单个 HTTP 请求发送多个 HTTP 响应

entity-framework - Entity Framework - 实体只读属性映射到相关表的列

asp.net-mvc - Web API 的 ASP.NET MVC Core Controller PATCH 方法

c# - 插件中的 ASP .NET Core MVC 2.1 mvc View

c# - C#中的对象和类有什么区别?

asp.net-mvc - 如何使用 RazorEngine 从模板发送电子邮件