none
Entity Framework Code First - do not delete if there are relations RRS feed

  • Question

  • public virtual ICollection<Article> Article { get; set; }

    I'm using a waregroup class with a public virtual ICollection entry as shown above.
    When calling

    _context.Waregroups.Remove(waregroup);
    _context.SaveChanges();

    in my service class - the waregroup gets deleted even if there are Articles present for the waregroup and the waregroup reference for the according article entries is set empty.

    What is the correct way to inform the user that there are still articles bound to this waregroup.
    *) implement the code in the service class "by hand" and just inform the user if there are references and do not remove the waregroup at all
    *) catch a DbUpdateException and handle it from there? (which is what I tried at the moment - but which doesn't seem to trigger when removing the waregroup references in the articles

    thank you for inputs

     

    • Moved by Bob Shen Thursday, April 4, 2013 3:14 AM
    Wednesday, April 3, 2013 11:15 AM

Answers

  • I managed to do it by adding

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            }
    to the context and creating a migration for it.
    • Marked as answer by FloLeBlanc Thursday, April 4, 2013 10:34 AM
    Thursday, April 4, 2013 10:34 AM

All replies

  • You current approach (catching the DbUpdateException) should work. Is waregroup set up as a required navigation property on article? If so Entity Framework sets cascade delete to false by default. This should cause an exception when you try to save changes after deleting a waregroup with articles referencing it.

    Wednesday, April 3, 2013 12:59 PM
  • Thank you - but unfortunately I seem to miss something.
    I want to setup a scenario where a waregroup does not HAVE to be related to an article but an article MUST have a waregroup - deleting a waregroup must not be allowed while it is referenced to an article.

    articles table

    CONSTRAINT [FK_dbo.Articles_dbo.Waregroups_Waregroup_Id] FOREIGN KEY ([Waregroup_Id]) REFERENCES [dbo].[Waregroups] ([Id])
    
    [Waregroup_Id]     INT            NOT NULL,

    articles.cs

    [Required]
    public virtual Waregroup Waregroup { get; set; }

    what I actually DO get at the moment is a cascading deletetion, in one of my migrations it is specifically set as:

    AddForeignKey("dbo.Articles", "Waregroup_Id", "dbo.Waregroups", "Id", cascadeDelete: false);
    however.


    Thursday, April 4, 2013 8:52 AM
  • I managed to do it by adding

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            }
    to the context and creating a migration for it.
    • Marked as answer by FloLeBlanc Thursday, April 4, 2013 10:34 AM
    Thursday, April 4, 2013 10:34 AM