在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12868912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First
提问by Gravy
How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs?
如何在模型设计中指定 ON DELETE NO ACTION 外键约束?
At present, I have:
目前,我有:
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; }
}
And my DbContext:
还有我的 DbContext:
public class MenuEntities : DbContext
{
public DbSet<Status> Statuses { get; set; }
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<Menu> Menus { get; set; }
}
As you can see:
如你看到的:
- a Restaurant has many menus
- a Restaurant has one status
- a Menu belongs to 1 restaurant
- Both Restaurants and Menus have 1 status. (Live, Invisible, Draft)
- 餐厅有很多菜单
- 一个餐厅有一个状态
- 一份菜单属于 1 家餐厅
- 餐厅和菜单都有 1 个状态。(实时、隐形、草稿)
Naturally, if a status is deleted, I certainly don't want to cascade as this will muck everything up.
自然,如果一个状态被删除,我当然不想级联,因为这会把一切都搞砸。
UPDATE:
更新:
Mark Oreta mentions using the following in his example below:
Mark Oreta 在下面的示例中提到使用以下内容:
modelBuilder.Entity<FirstEntity>()
.HasMany(f => f.SecondEntities)
.WithOptional()
.WillCascadeOnDelete(false);
Where do I put this code? Within my MenuEntities / DbContext class? Can anybody provide an example of this being used?
我把这段代码放在哪里?在我的 MenuEntities / DbContext 类中?任何人都可以提供一个使用这个的例子吗?
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.
My Database Initialiser:
我的数据库初始化程序:
采纳答案by Mark Oreta
You can either disable it for your entire context by removing the cascade delete convention in the OnModelCreating method:
您可以通过删除 OnModelCreating 方法中的级联删除约定来为整个上下文禁用它:
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
or, you can do it per relationship using a fluent mapping (also in the OnModelCreating):
或者,您可以使用流畅的映射(也在 OnModelCreating 中)按关系执行此操作:
EDIT: you would put it in your menu entities
编辑:你会把它放在你的菜单实体中
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 );
}
}
回答by Lionfoo
Put this into your MenuEntitiesclass (class that descend from DbContext):
把它放到你的MenuEntities班级(从 下降的班级DbContext):
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
回答by Ali Sakhi
After making the changes to the model, make sure you regenerate the migration file by adding the -Force parameter.
对模型进行更改后,请确保通过添加 -Force 参数重新生成迁移文件。
Add-Migration MigrationName -Force
Add-Migration MigrationName -Force
回答by Bob Yang
Just make the FK property nullable, then the cascade delete will be gone.
只需让 FK 属性可以为空,那么级联删除就会消失。
public int? StatusId { get; set; }
回答by Ghadir Farzaneh
add this line to end of the field in the context;
将此行添加到上下文中的字段末尾;
.OnDelete(DeleteBehavior.Restrict);
.OnDelete(DeleteBehavior.Restrict);

