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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:37:46+00:00 2026-05-31T11:37:46+00:00

I have an action [HttpPost] public string Edit(Member member) and Member has a collection

  • 0

I have an action

[HttpPost]
public string Edit(Member member)

and Member has a collection of children entities ICollection<AgeBracket> AgeBrackets.

Currently I do retrieve all AgeBrackets associated with the member, mark everyone as deleted, then loop through new collection and create a new entry for each. Then I update my parent entity. It works, but there should be a better way to do it:

for example, if I would wrote SQL, I could delete all existing children with just one line

DELETE FROM AgeBrackets WHERE MemberId = @MemberId

In my situation it makes a select to retrieve existing items, then generate delete for each of them, then generate insert for each new child and then it generates update for parent.

Here is how my code looks now:

IList<AgeBracket> ageBrackets = db.AgeBrackets.Where<AgeBracket>(x => x.MemberId == member.MemberId).ToList();
        foreach (AgeBracket ab in ageBrackets)
            db.Entry(ab).State = EntityState.Deleted;
        if (member.AgeBrackets != null)
        foreach (AgeBracket ab in member.AgeBrackets)
        {
            ab.MemberId = member.MemberId;                  
            db.AgeBrackets.Add(ab);
        }
        db.Entry(member).State = EntityState.Modified;

Initially I was trying to query existing children and compare each of them to new set, but it seems to be over-complicated.

What is the best way to update member and all it’s children?

  • 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-31T11:37:47+00:00Added an answer on May 31, 2026 at 11:37 am

    There’s another way to do

    var originalAgeBrackets = db.AgeBrackets.Where(x => x.MemberId == member.MemberId).ToArray();
    var currentAgeBrackets = member.AgeBrackets;
    
    foreach (var original in originalAgeBrackets) {
        // check if the original age brackets were modified ou should be removed
        var current = currentAgeBrackets.FirstOrDefault(c => c.AgeBracketId == original.AgeBracketId);
    
        if(current != null) {
            var entry = db.Entry(original);
            entry.OriginalValues.SetValues(original);
            entry.CurrentValues.SetValues(current);
        } else {
            db.Entry(original).State = EntityState.Deleted;
        }
    }
    
    // add all age brackets not listed in originalAgeBrackets
    foreach (var current in currentAgeBrackets.Where(c => !originalAgeBrackets.Select(o => o.AgeBracketId).Contains(c.AgeBracketId))) {
        db.AgeBrackets.Add(current);
    }
    
    db.SaveChanges();
    

    Unfortunately what you want to do haven’t native support to EF Code First. What will help you would be EntityFramework.Extended. This will allow you to do something like:

    db.AgeBrackets.Delete(a => a.MemberId == member.MemberId);
    

    You should take care of change-tracks by yourself.
    Hope it helps you.

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

Sidebar

Related Questions

I have this action method: [HttpPost] public ActionResult GetNextImage(int m_id) { ... return Json(new{...});
I have an action : [HttpPost] public ActionResult MyAction(MyModel model) { ... if (model.MyCondition
I have a controller with action: [HttpPost] public ActionResult Add(Question container, HttpPostedFileBase file) {
I have an action method to handle the HTTP-POST as follows. [HttpPost] public ActionResult
If I have a controller like this: [HttpPost] public JsonResult FindStuff(string query) { var
I have the following action/controller on MVC3: [HttpPost] public ActionResult AX_AddItemResponse(ItemResponsesVM response) { return
i have 2 actions public ActionResult FilesAdd(int id) { FillParentMenuDDL(id); return View(); } [HttpPost]
I have a view model with a collection of other objects in it. public
I have an HttpPost controller action that takes in a simple form DTO object.
I have created an Edit Action method but it is not going inside ModelState.isValid.

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.