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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:29:40+00:00 2026-06-09T02:29:40+00:00

Consider the following entity model: public class Agreement : Entity { public int AgreementId

  • 0

Consider the following entity model:

public class Agreement : Entity
{
    public int AgreementId { get; set; }
    public virtual ICollection<Participant> Participants { get; set; }
}

public class Establishment : Entity
{
    public int EstablishmentId { get; set; }
}

public class Participant : Entity
{
    public int AgreementId { get; set; }
    public virtual Agreement Agreement { get; set; }

    public int EstablishmentId { get; set; }
    public virtual Establishment { get; set; }

    public bool IsOwner { get; set; }
}

The reason there is not a direct ICollection<Establishment> on the Agreement entity is because of the IsOwner property, which further defines this many-to-many relationship.

The relationship is mapped like so:

internal ParticipantOrm()
{
    ToTable(typeof(Participant).Name);

    HasKey(k => new { k.AgreementId, k.EstablishmentId });

    HasRequired(d => d.Agreement)
        .WithMany(p => p.Participants)
        .HasForeignKey(d => d.AgreementId)
        .WillCascadeOnDelete(true);

    HasRequired(d => d.Establishment)
        .WithMany()
        .HasForeignKey(d => d.EstablishmentId)
        .WillCascadeOnDelete(true);
}

The relationship is uni-directional, meaning you can only get to the Participants from within an Agreement — you cannot access the agreements from the Establishment.

Now, since the Agreement entity’s primary key is part of the gerund Participant’s primary key, and since cascade delete is specified, I would expect that putting the Agreement into the Deleted state would also cause each entity in its Participants collection to also be put into the deleted state.

This appears to work when calling Remove on an Agreement DbSet:

// this works
dbContext.Agreements.Remove(agreement);
dbContext.SaveChanges();

However, it does not appear to work when simply setting the agreement entry’s state to deleted:

// this causes an exception when Participants is not empty
dbContext.Entry(agreement).State = EntityState.Deleted;
dbContext.SaveChanges();

Is there a way I can define the relationship so that simply putting the Agreement entity into the deleted state will also cause the corresponding collection item entities to be put into the deleted state?

Update

I have been reading about this, and found out that eager loading the Participants collection does not help:

// eager loading does not help, this still breaks
var agreement = dbContext.Set<Agreement>()
    .Include(a => a.Participants)
    .FirstOrDefault();
dbContext.Entry(agreement).State = EntityState.Deleted;
dbContext.SaveChanges();
  • 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-06-09T02:29:42+00:00Added an answer on June 9, 2026 at 2:29 am

    Ended up solving this by calling the following:

    dbContext.Set<Agreement>().Remove(agreement);
    

    I wanted to get rid of the Agreements property on the DbContext, which is why I was trying to do it with Entry(agreement).State = EntityState.Deleted.

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

Sidebar

Related Questions

consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider the following Entities. public class Product{ int id; Date effectiveDate; Date expiryDate; Set<Inventory>
Consider following class public class AccountGroup : Entity<int> { public AccountGroup() { Accounts =
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
To illustrate my question consider the following example: @Entity public class Box implements Serializable
consider the following simplified example: public class Ticket { public int Id; public TicketState
Consider the following Entity: @Entity public class Employee { @Id @GeneratedValue private Long id;
Please consider following sample @Entity public class Abc { @Id private Long id; @Unindexed
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
Please consider the following two entities: @Entity @Table(name = PROFILE) public class Profile extends

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.