Andy Johnson
Andy Johnson

Reputation: 693

Entity Framework DbContext Delete child object

I am trying to delete the child objects in foreach loop but that doesn't seem to be working.. Here is what I have. I have a meeting and meeting_category entities which has 1-to-many relationship..

    foreach (meeting_category meetingCategory in currentMeeting.meeting_category)
            {
              dbContext.meeting_category.Remove(meetingCategory);
              dbContext.Entry(meetingCategory).State = EntityState.Deleted;
              dbContext.SaveChanges();
            }

I am then returning to the same view with updated results.. This seems to be very inconsistent. Sometimes it deletes entities and sometimes it doesn't. Even when it deletes it, my meeting object still have meeting_category objects I deleted.

When it doesn't delete from DB, it updates the meeting object and removed meeting_category objects from the meeting.. Is there any kind of reset or refresh that I will have to do in order for meeting object to remove the meeting_category objects.. How can I make sure that it consistently deletes the meeting_category objects both from DB and from the meeting object and when I return to the view, I have an updated meeting object?

Here is my meeting entity

    public partial class meeting
    {
        public meeting()
        {
            this.meeting_questions = new HashSet<meeting_questions>();
            this.meeting_abstract = new HashSet<meeting_abstract>();
            this.meeting_category = new HashSet<meeting_category>();
            this.meeting_image = new HashSet<meeting_image>();
        }

        public int meeting_id { get; set; }
        public int language_id { get; set; }
        public string meeting_code { get; set; }
        public string meeting_name { get; set; }
        public string meeting_description { get; set; }
        public System.DateTime meeting_start_date { get; set; }
        public System.DateTime meeting_end_date { get; set; }
        public System.DateTime abstract_cutoff_date { get; set; }
        public string meeting_guidelines { get; set; }
        public string created_by { get; set; }
        public Nullable<System.DateTime> created_datetime { get; set; }
        public Nullable<bool> meeting_published { get; set; }
        public Nullable<bool> meeting_deleted { get; set; }
        public Nullable<bool> meeting_fully_created { get; set; }

        public virtual language language { get; set; }
        public virtual ICollection<meeting_questions> meeting_questions { get; set; }
        public virtual ICollection<meeting_abstract> meeting_abstract { get; set; }
        public virtual ICollection<meeting_category> meeting_category { get; set; }
        public virtual ICollection<meeting_image> meeting_image { get; set; }
    }

And here is my meeting_category entity

 public partial class meeting_category
    {
        public meeting_category()
        {
            this.abstract_category = new HashSet<abstract_category>();
        }

        public int meeting_category_id { get; set; }
        public int meeting_id { get; set; }
        public int category_type_id { get; set; }
        public int category_id { get; set; }
        public string category_name { get; set; }
        public string category_name_en { get; set; }

        public virtual ICollection<abstract_category> abstract_category { get; set; }
        public virtual category category { get; set; }
        public virtual category_type category_type { get; set; }
        public virtual meeting meeting { get; set; }
    }

Upvotes: 0

Views: 522

Answers (2)

Andy Johnson
Andy Johnson

Reputation: 693

I managed to fix the issue by creating the DbContext per controller action and destroying it before loading the view. The problem was that I was creating the DbContext in another class and was reusing the same context on all controller action. Since Entities are cached in dbcontext, I was always getting the old cached copy of entities and was working very inconsistently.

Upvotes: 0

Mike Dymond
Mike Dymond

Reputation: 1185

I have a couple of questions about what it is that you are trying to achieve.

Firstly are you sure that the Meeting -> Category relationship is many-to-one? I would normally expect it to be many-to-many, i.e. each Meeting can have many Categories and each Category can be attached to many Meetings.

Secondly, are you actually trying to delete the Category or are you trying to remove a Category from a Meeting?

If you are simply trying to remove the relationship between a Meeting and a Category then you need to edit the navigation property, i.e. MyMeeting.Categories.Remove(MyCategory).

If you are trying to delete the actual Category then you will need to make sure that that Category is not currently linked to any Meeting otherwise you will get a Foreign Key violation if you try to remove the Category.

On top of all of this you may be encountering chaching or simply object lifetime management issues.

What type of application are you creating? ASP.NET? WinForms? etc.

How are you actually instantiating a concrete version of your DBContext? Are you using DI?

Cheers Mike

Upvotes: 0

Related Questions