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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:22:52+00:00 2026-06-05T00:22:52+00:00

As a follow-up to my earlier question , I now know that EF doesn’t

  • 0

As a follow-up to my earlier question, I now know that EF doesn’t just save all of the changes of the entire entity for me automatically. If my entity has a List<Foo>, I need to update that list and save it. But how? I’ve tried a few things, but I can’t get the list to save properly.

I have a many-to-many association between Application and CustomVariableGroup. An app can have one or more groups, and a group can belong to one or more apps. I believe I have this set up correctly with my Code First implementation because I see the many-to-many association table in the DB.

The bottom line is that the Application class has a List<CustomVariableGroup>. My simple case is that the app already exists, and now a user has selected a group to belong to the app. I want to save that change in the DB.

Attempt #1

this.Database.Entry(application).State = System.Data.EntityState.Modified;
this.Database.SaveChanges();

Result: Association table still has no rows.

Attempt #2

this.Database.Applications.Attach(application);
var entry = this.Database.Entry(application);
entry.CurrentValues.SetValues(application);
this.Database.SaveChanges();

Result: Association table still has no rows.

Attempt #3

CustomVariableGroup group = application.CustomVariableGroups[0];
application.CustomVariableGroups.Clear();
application.CustomVariableGroups.Add(group);
this.Database.SaveChanges();

Result: Association table still has no rows.

I’ve researched quite a bit, and I’ve tried more things than I’ve shown, and I simply don’t know how to update an Application’s list with a new CustomVariableGroup. How should it be done?

EDIT (Solution)

After hours of trial and error, this seems to be working. It appears that I need to get the objects from the DB, modify them, then save them.

public void Save(Application application)
{
    Application appFromDb = this.Database.Applications.Single(
      x => x.Id == application.Id);
    CustomVariableGroup groupFromDb = this.Database.CustomVariableGroups.Single(
      x => x.Id == 1);
    appFromDb.CustomVariableGroups.Add(groupFromDb);
    this.Database.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-05T00:22:53+00:00Added an answer on June 5, 2026 at 12:22 am

    While I consider this a bit of a hack, it works. I’m posting this in the hopes that it helps someone else save an entire day’s worth of work.

    public void Save(Application incomingApp)
    {
        if (incomingApp == null) { throw new ArgumentNullException("incomingApp"); }
    
        int[] groupIds = GetGroupIds(incomingApp);
    
        Application appToSave;
    
        if (incomingApp.IdForEf == 0)  // New app
        {
            appToSave = incomingApp;
            // Clear groups, otherwise new groups will be added to the groups table.
            appToSave.CustomVariableGroups.Clear();
            this.Database.Applications.Add(appToSave);                
        }
        else
        {
            appToSave = this.Database.Applications
                    .Include(x => x.CustomVariableGroups)
                    .Single(x => x.IdForEf == incomingApp.IdForEf);
        }
    
        AddGroupsToApp(groupIds, appToSave);
        this.Database.SaveChanges();
    }
    
    private void AddGroupsToApp(int[] groupIds, Application app)
    {
        app.CustomVariableGroups.Clear();
    
        List<CustomVariableGroup> groupsFromDb2 =
            this.Database.CustomVariableGroups.Where(g => groupIds.Contains(g.IdForEf)).ToList();
    
        foreach (CustomVariableGroup group in groupsFromDb2)
        {
            app.CustomVariableGroups.Add(group);
        }
    }
    
    private static int[] GetGroupIds(Application application)
    {
        int[] groupIds = new int[application.CustomVariableGroups.Count];
    
        int i = 0;
        foreach (CustomVariableGroup group in application.CustomVariableGroups)
        {
            groupIds[i] = group.IdForEf;
            i++;
        }
    
        return groupIds;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up to an earlier question I posted on EF4 entity keys
As a follow up to an earlier question that attracted a whole zero answers,
This question is a follow up to an earlier question I had that was
This is a follow-up question to this question I asked earlier. Btw thanks Neil
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
This is a follow-up to another question I asked earlier today. I am creating
This is somewhat of a follow up to a question posted earlier last month.
As a follow up to my earlier question , I'm thinking of using simplexml_load_file
This is a follow-up to a question I'd asked earlier which phrased this as
(Follow-up to my earlier question, Ruby: how can I copy a variable without pointing

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.