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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:01:07+00:00 2026-06-06T12:01:07+00:00

I’m new to Entify Framework so this is probably a very basic question. In

  • 0

I’m new to Entify Framework so this is probably a very basic question. In a WinForms application I have a data entry page that works fine until I add a listbox and try to update the database with the selections that have been made.

On the form the user selects a file to upload and specifies one or more departments that can access the file. Here’s how I thought it would work:

using (var ctx = new FCEntities())
{
  var batch = new Batch() { Description = txtDescription.Text, Filename = filename, Departments = (System.Data.Objects.DataClasses.EntityCollection<Department>)lstDepartments.SelectedItems };
  ctx.AddToBatches(batch);
  ctx.SaveChanges();
}

But when this didn’t work I did some research and learned that I can’t cast the SelectedItems to EntityCollection so I decided to copy the items from the original collection into a new collection and then use the new collection as follows:

using (var ctx = new FCEntities())
{
  var departments = new System.Data.Objects.DataClasses.EntityCollection<Department>();

  foreach (var department in lstDepartments.SelectedItems)
  {
    departments.Add((Department)department);
  }

  var batch = new Batch() {Description = txtDescription.Text, Filename = filename, Departments=departments };
  ctx.AddToBatches(batch);
      ctx.SaveChanges();

}

This didn’t work either and gave this error on the departments.Add line:

“An object that is attached to an ObjectContext cannot be added to an
EntityCollection or EntityReference that is not associated with a
source object.”

I don’t understand because it doesn’t appear to me that the department object is attached to the ObjectContext? I’m obviously missing something fundamental, so any advice and/or links to examples of how others do this would be appreciated.

  • 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-06T12:01:09+00:00Added an answer on June 6, 2026 at 12:01 pm

    I wanted to leave an answer to this in case someone else runs into this someday. The comments left by Wiktor were helpful in getting me in the right direction. I decided I had a lack of fundamental understanding so I did some reading on MSDN and was able to resolve my issue.

    The datamodel behind this existed of three tables: Batches, Departments, and Batches_Departments which allowed for a many to many relationship between Batches and Departments.

    The problem with my original code/logic, in a nutshell, was that the Department objects in the ListBox were associated with a different context than the one I was using in my Save method. EF didn’t like this for obvious reasons (at least now they are obvious), so in the save method I used the ID from the selected Departments to get a reference to the same Department in the current context. I could then add this Department to the newly created batch.

    Here’s what the code now looks like:

    using (var ctx = new FCEntities())
    {
      var batch = new Batch() { Description = txtDescription.Text, Filename = filename};
    
      foreach (var department in lstDepartments.CheckedItems)
      {
        var dept = (from d in ctx.Departments where d.DepartmentID == ((Department)department).DepartmentID select d).First();
        batch.Departments.Add(dept);
      }
    
      ctx.Batches.AddObject(batch);
      ctx.SaveChanges();
    }
    

    Hopefully this helps someone else who is dealing with the same issue.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.