Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:07:52+00:00 2026-05-31T03:07:52+00:00

Is it possible to get cascade delete on this object model? public class Entity

  • 0

Is it possible to get cascade delete on this object model?

public class Entity
{
    [Key, Column("Id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
}

public class Comment : Entity
{
    public string Text { get; set; }
}

public class Owner : Entity
{
    public Owner()
    {
        Pets = new List<Pet>();
        Comments = new List<Comment>(); 
    }

    public string Name { get; set; }
    public virtual ICollection<Pet> Pets { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

public class Pet : Entity
{
    public Pet()
    {
        Comments = new List<Comment>();    
    }

    public virtual Owner Owner { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public string Name { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Pet>()
        .HasOptional(d => d.Owner)
        .WithMany(d => d.Pets)
        .WillCascadeOnDelete(true);
}

This model will generate a database that will cascade delete Pets when a Owner gets deleted. But it will not cascade delete Comment, it will just delete the FK from the Comments table. If I try to modify the constraints accordingly.

ALTER TABLE [dbo].[Comments]  WITH CHECK ADD  CONSTRAINT [FK_Comments_Pets_Pet_Id]     FOREIGN KEY([Pet_Id])
REFERENCES [dbo].[Pets] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Comments] CHECK CONSTRAINT [FK_Comments_Pets_Pet_Id]
GO

ALTER TABLE [dbo].[Comments]  WITH CHECK ADD  CONSTRAINT [FK_Comments_Owners_Owner_Id]     FOREIGN KEY([Owner_Id])
REFERENCES [dbo].[Owners] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Comments] CHECK CONSTRAINT [FK_Comments_Owners_Owner_Id]
GO

The following sql error appears.

Introducing FOREIGN KEY constraint 'FK_Comments_Pets_Pet_Id' on table 'Comments' may   cause cycles or multiple cascade paths

This brings me to the conclusion that the model created cannot support cascade delete and I’m am forced to to the delete of comments separatly. Or am I’m missing something here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:07:53+00:00Added an answer on May 31, 2026 at 3:07 am

    Yes the model doesn’t support direct cascade delete in SQL server because even if you don’t think about it your database representation support same Comment row to be associated with both Owner and Pet. If you then delete Owner it will trigger cascade delete to Comment and pet and Pet will trigger cascade delete to Comment as well => multiple cascade paths which are not supported in SQL server.

    This is usually handled by manual deletion or by separate database trigger handling deletion of related records.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Taking the following object model: public abstract class Entity { public Guid Id {
Is it possible to get Core Data cascade delete to delete objects more than
Is it possible to get the originating port from an ActionExecutingContext object? If so,
Given these classes: using System.Collections.Generic; namespace FluentMappingsQuestion { public class Entity { public virtual
It's possible get each row as an object like in ezSQL? For example, ezSQL
Possible Duplicate: Get first day of week in PHP? i have this script that
Using the following classes.. public class Trait { public virtual int Id { get;
Possible Duplicate: Get CVS history for a particular user Is it possible to list
Possible Duplicate: Get all files from VSS for a given date? I need to
Is it possible to get gdb or use some other tools to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.