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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:54:55+00:00 2026-05-15T00:54:55+00:00

Lets say i have a Person table, a Role table with a trel table

  • 0

Lets say i have a Person table, a Role table with a trel table PersonRoles linking them as many to many.

I create a new person and assign them to 2 roles (role 1, role 3).

I then want to edit this person; so i retrieve their data and bind their roles to a checkboxes. I change the values (Deselect role 1 and select role 2 instead) I then post this data back through a viewmodel.

Can i then get Entity Framework to update these roles for me, as in delete the entry in PersonRoles to role 1 and then add a new entry as role 2? Or do i have to do the logic for this myself?

Something like ** this ** i was hopeing but doesn’t work obviously.

[HttpPost]
    public ActionResult Edit(int id, PersonViewModel model)
    {
        if (ModelState.IsValid)
        {
            var person= GetPerson(id);
            person.Name= model.Name;
            person.Age = model.Age;

           ICollection<Roles> personroles = new Collection<Roles>();
            foreach (string rId in model.Roles)
            {
                personroles.Add(this.RoleRepository.Single(r => r.Id.ToString() == rId));
            }

            **person.Roles = personroles;**

            this.PersonRepository.SaveChanges();
            return RedirectToIndex(personId);
        }

        return View();
    }

Cheers,
Kohan

  • 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-15T00:54:56+00:00Added an answer on May 15, 2026 at 12:54 am

    The devil is in the details, and you don’t give any, but generally, yes, the EF can do this.

    The best approach is to make PersonRoles have only two columns: The FKs to Person and Role, with cascading deletes on both, and make the PK a compound key on both. If you have no other columns, the EF will subsume this table into a many to many relationship which will behave the way you’re expecting.

    Update: Since you’ve now added source, let’s fix it (untested; this is off the top of my head):

        if (ModelState.IsValid)
        {
            var person= GetPerson(id);
            person.Name= model.Name;
            person.Age = model.Age;
    
            var rolesToAdd = model.Roles.Where(mr => !person.Roles.Any(pr => pr.Id == mr);
            var rolesToRemove = person.Roles.Where(pr => !model.Roles.Any(mr => pr.Id == mr);
    
            foreach (string rId in rolesToAdd)
            {
                person.Roles.Add(this.RoleRepository.Single(r => r.Id.ToString() == rId));
            }
            foreach (string rrId in rolesToRemove)
            {
                var remove = person.Roles.Where(r => r.Id == rrId).Single();
                person.Roles.Remove(remove);
            }
    
            this.PersonRepository.SaveChanges();
            return RedirectToIndex(personId);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a a table called... Person, I guess, and it has
Lets say we have a table here, populated with the following data: acc_id1 acc_id2
Lets say we have a table Names: ID Name Surname 1 Matt Smith 2
I have a table with a primary key (lets call it person), and another
Let's say I have a table called Person with the columns: id, name, parent_id
Lets say you have a table with some winning numbers in it. Any of
Let's say I have a table Person and I have another 3 Doctor ,
Let's say I have a table called Event which has a to-many relationship with
lets say I have a table with date,personid 1/1/2001 1 1/2/2001 3 1/3/2001 2
Let's say I have a person table, and I want it to classify all

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.