指定ON DELETE在ASP.NET MVC 4 C#code首先NO ACTION [英] Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First
问题描述
我如何指定ON DELETE NO ACTION外键约束在我的模型设计?
How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs?
在present,我有:
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; }
}
和我的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; }
}
正如你所看到的:
As you can see:
- 餐厅有很多菜单
- 餐厅有一个状态
- Menu(菜单)属于一间餐厅
- 两个餐厅和菜单具有1的状态。 (现场,无形的,草案)
- 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)
当然,如果一个状态被删除了,我当然不希望级联,因为这将淤泥一切。
Naturally, if a status is deleted, I certainly don't want to cascade as this will muck everything up.
更新:
标记Oreta提到使用下他的例子如下:
Mark Oreta mentions using the following in his example below:
modelBuilder.Entity<FirstEntity>()
.HasMany(f => f.SecondEntities)
.WithOptional()
.WillCascadeOnDelete(false);
我在哪里可以把这个code?在我的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.
我的数据库初始化器:
My Database Initialiser:
推荐答案
您可以通过删除级联删除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 );
}
}
这篇关于指定ON DELETE在ASP.NET MVC 4 C#code首先NO ACTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!