asp.net mvc启用删除级联整个项目 [英] asp.net mvc enable on delete cascade for entire project
问题描述
我搜索了解决我的问题的东西,但似乎没有找到任何东西。
I've searched for something that solves my problem, but don't seem to find anything.
有没有办法简单地为整个项目启用OnDeleteCascade ?
Is there a way to simply enable OnDeleteCascade for the entire project?
任何人都可以帮忙吗?
推荐答案
您的DbContext中的关系,并在您的模型构建器中指定级联删除。
You want to setup your relationships in your DbContext and specify your cascade deletes in your model builder.
public class SomeContext : DbContext
{
public DbSet<EntityOne> EntityOnes { get; set; }
public DbSet<EntityTwo> EntityTwos { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityOne>()
.HasRequired(d => d.EntityTwo)
.WillCascadeOnDelete(true); // this is what you want to do
}
}
参考(这是一个非常好的各种关系): https://msdn.microsoft.com/en-us/library/jj591620(v = vs.113).aspx
Reference (This one is really good for various relationships): https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspx
编辑:想要请注意,如果您想要获取更多信息,请点击此处 Entity Framework Fluent API 。
Wanted to note this is called Entity Framework Fluent API if you wanted to google for some more info.
这篇关于asp.net mvc启用删除级联整个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!