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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:10:28+00:00 2026-06-04T17:10:28+00:00

This is driving me nuts. I’m not sure what else to try. This is

  • 0

This is driving me nuts. I’m not sure what else to try. This is my latest attempt to update a list of objects within another object using EF 4.3.

The scenario is that a user has added a new Task to an Application that already has one task in its Tasks property. The Application is not attached to the DB context because it was retrieved in a prior logic/DB call. This is the class and property:

public class Application : EntityBase
{
    public ObservableCollection<TaskBase> Tasks { // typical get/set code here }
}

This is my attempt to update the list. What happens is that the new Task gets added and the association correctly exists in the DB. However, the first task, that wasn’t altered, has its association removed in the DB (its reference to the Application).

This is the Save() method that takes the Application that the user modified:

public void Save(Application newApp)
{
    Application appFromContext;

    appFromContext = this.Database.Applications
        .Include(x => x.Tasks)
        .Single(x => x.IdForEf == newApp.IdForEf);

    AddTasksToApp(newApp, appFromContext);
    this.Database.SaveChanges();
}

And this is the hooey that’s apparently necessary to save using EF:

private void AddTasksToApp(Application appNotAssociatedWithContext, Application appFromContext)
{
    List<TaskBase> originalTasks = appFromContext.Tasks.ToList();

    appFromContext.Tasks.Clear();

    foreach (TaskBase taskModified in appNotAssociatedWithContext.Tasks)
    {
        if (taskModified.IdForEf == 0)
        {
            appFromContext.Tasks.Add(taskModified);
        }
        else
        {
            TaskBase taskBase = originalTasks.Single(x => x.IdForEf == taskModified.IdForEf);  // Get original task
            this.Database.Entry(taskBase).CurrentValues.SetValues(taskModified);  // Update with new
        }
    }
}

Can anyone see why the first task would be losing its association to the Application in the DB? That first task goes through the else block in the above code.

Next, I’ll need to figure out how to delete one or more items, but first things first…

  • 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-04T17:10:31+00:00Added an answer on June 4, 2026 at 5:10 pm

    After continual trial and error, this appears to be working, including deleting Tasks. I thought I’d post this in case it helps someone else. I’m also hoping that someone tells me that I’m making this more complicated than it should be. This is tedious and error-prone code to write when saving every object that has a list property.

    private void AddTasksToApp(Application appNotAssociatedWithContext, Application appFromContext)
    {
        foreach (TaskBase taskModified in appNotAssociatedWithContext.Tasks)
        {
            if (taskModified.IdForEf == 0)
            {
                appFromContext.Tasks.Add(taskModified);
            }
            else
            {
                TaskBase taskBase = appFromContext.Tasks.Single(x => x.IdForEf == taskModified.IdForEf);  // Get original task
                this.Database.Entry(taskBase).CurrentValues.SetValues(taskModified);  // Update with new
            }
        }
    
        // Delete tasks that no longer exist within the app.
        List<TaskBase> tasksToDelete = new List<TaskBase>();
        foreach (TaskBase originalTask in appFromContext.Tasks)
        {
            TaskBase task = appNotAssociatedWithContext.Tasks.Where(x => x.IdForEf == originalTask.IdForEf).FirstOrDefault();
    
            if (task == null)
            {                    
                tasksToDelete.Add(originalTask);
            }
        }
    
        foreach (TaskBase taskToDelete in tasksToDelete)
        {                
            appFromContext.Tasks.Remove(taskToDelete);
            this.Database.TaskBases.Remove(taskToDelete);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me nuts. I can't seem to get the data template within
OK, this is driving me nuts. I'm sure it is trivial, but I've been
This has been driving me nuts. I hope it's not been asked before but
This is driving me nuts: HTML: <div><h1>Hello World!</h1></div> CSS: *:not(div) h1 { color: #900;
This is driving me nuts, I'm sure it's rather simple but can't get it
This is driving me nuts. I think it is because of some connection string
This is driving me nuts. I believe I asked this exact same question, but
This is driving me nuts. I've looked at all the relevant MSDN tutorials but
This is driving me nuts. I'm checking 3 conditions in my sql query. If
Ok, this is driving me nuts... I'm trying to rewrite my urls like this:

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.