Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First(在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01