简体   繁体   English

级联删除

[英]Delete in cascade

I'm new to asp.net and mvc. 我是asp.net和mvc的新手。 I have a class AsyApp 我上了一课AsyApp

 public class AsyApp
{
    [Key]
    public int AsyAppId { get; set; }

    [DisplayName("Nom")]
    public string Name { get; set; }

    public virtual ICollection<Scene> Scenes { get; set; }
}

It has a collection of scenes 它有一个场景集合

 public class Scene
{

    [Key]
    [Required]
    public int SceneId { get; set; }


    [ForeignKey("AsyApp")]
    public int AsyAppId { get; set; }
    [Required]
    public virtual AsyApp AsyApp { get; set; }


    [ForeignKey("Name")]
    public int NameId { get; set; }

    [DisplayName("Nom")]
    public virtual Translation Name { get; set; }

    public int Order { get; set; }
}

And the Scene has a property Name from a class Translation 并且Scene具有来自类Translation的属性Name

public class Translation
{
    [Key]
    [Required]
    public int TranslationId { get; set; }

    public string Fr { get; set; }
    public string En { get; set; }

}

Here is my issue. 这是我的问题。 I will use the class Translation to handle language for every single string in my application. 我将使用Translation类来处理应用程序中每个字符串的语言。 I could be in any other classes 我可能在任何其他班上

I would like that when I delete an application, it delete the appropriate scenes and localisations 我希望在删除应用程序时删除适当的场景和本地化

Here is my code for deleting an app 这是我删除应用程序的代码

[HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        AsyApp asyApp = db.AsyApps.Find(id);
        if (asyApp == null)
        {
            return HttpNotFound();
        }

        db.AsyApps.Remove(asyApp);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

The scenes seams to be deleted automaticaly but not the localisations associated with them... 场景接缝将自动删除,但不会自动删除与它们关联的本地化...

Hope you can help me 希望你能帮我

Thanks a lot 非常感谢

look at this link: https://msdn.microsoft.com/en-us/data/jj591620.aspx#CascadeDelete 查看此链接: https : //msdn.microsoft.com/zh-cn/data/jj591620.aspx#CascadeDelete

you can enable cascade delete using Fluent API. 您可以使用Fluent API启用级联删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM