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 6623623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:33:16+00:00 2026-05-25T21:33:16+00:00

I get this error: System.Data.SqlClient.SqlException The DELETE statement conflicted with the REFERENCE constraint FK_

  • 0

I get this error:

System.Data.SqlClient.SqlException The DELETE statement conflicted
with the REFERENCE constraint “FK_comments_postId__164452B1″. The
conflict occurred in database “awe”, table “dbo.comments”, column
‘postId’. The statement has been terminated.

I have this structure:

    public class Post
    {
        public long Id { get; set; }
        public string Body { get; set; }     

        public long? ParentId { get; set; }
        public virtual Post Parent { get; set; }
        public virtual ICollection<Post> Posts { get; set; }

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

    public class Comment
    {
        public long Id { get; set; }
        public long PostId { get; set; }
        public virtual Post Post { get; set; }
        public string Body { get; set; }
    }

my delete method:

    public void Delete(long id)
    {
        var p = context.Set<Post>().Get(id);
        if(p == null) throw new MyEx("this post doesn't exist");
        if (p.Posts.Count > 0) throw new MyEx("this post has children and it cannot be  deleted");
        context.Set<Post>().Remove(p);
        context.SaveChanges();
    }

my DbContext:

public class Db : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Comment> Comments { get; set; }
}
  • 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-25T21:33:17+00:00Added an answer on May 25, 2026 at 9:33 pm

    It sounds like the Post that you are trying to delete has child Comments.

    Entity Framework will not take responsibility for cascading a delete in the database – it expects that you will achieve this by setting a cascading delete on the foreign key relationship in the RDBMS.

    Having said this, if you delete a parent entity in Entity Framework, it will attempt to issue delete statements for any child entities which have been loaded into the current DbContext, but it will not initialize any child entities which have not yet been loaded. This may lead to the RDBMS throwing foreign key constraint violation exceptions if a cascading delete has not been specified, like the one you are seeing. For more details about how cascade delete “works” in Entity Framework, see this blog post (alas, dead link as of 2023-04-03).

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

Sidebar

Related Questions

Getting this error: System.Data.SqlClient.SqlException : The conversion of a datetime2 data type to a
We keep getting this error randomly in our web application. System.Data.SqlClient.SqlException: A network-related or
I am currently getting this error: System.Data.SqlClient.SqlException: New transaction is not allowed because there
I've recently encountered the error: System.Data.SqlClient.SqlException: The transaction log for database 'mydatabase' is full.
I get this error: System.Reflection.TargetException: Object does not match target type. when trying to
i get this error {Method 'System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)' has no supported translation to SQL.}
I get an error when I compile this code: using System; public struct Vector2
In SQL Query Analyzer I run this statement and I don't get anny error:
I get this error: Can't locate Foo.pm in @INC Is there an easier way
I get this error on an update panel within a popupControlExtender which is within

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.