在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION [英] Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

查看:31
本文介绍了在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs?

目前,我有:

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:

And my 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 个状态.(实时、隐形、草稿)

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

Naturally, if a status is deleted, I certainly don't want to cascade as this will muck everything up.

更新:

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

Mark Oreta mentions using the following in his example below:

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

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

Where do I put this code? Within my MenuEntities / DbContext class? Can anybody provide an example of this being used?

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

UPDATE: Got this bit working now, however this has created a multiplicity constraint error when trying to seed the 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

推荐答案

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

You can either disable it for your entire context by removing the cascade delete convention in the OnModelCreating method:

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

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

or, you can do it per relationship using a fluent mapping (also in the 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 );

      }

}

这篇关于在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆