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

  • Home
  • SEARCH
  • 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 7560639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:57:18+00:00 2026-05-30T12:57:18+00:00

This problem is similar to my previously asked question. When I query data using

  • 0

This problem is similar to my previously asked question. When I query data using the Entity Framework (EF) I always use the MergeOption.NoTracking option because I end up taking my EF generated objects and mapping them to view models which have lovely attributes decorated on the property to enforce validation and so on.

I am trying to add a foreign key relationship using the EF, but anytime I do I am getting the following exception:

The object being attached to the source object is not attached to the same ObjectContext as the source object

Here is my code:

    public static void UpdateDownloadFileVersion(DownloadFile downloadFile, int[] selectedVersions) {
        using (SupportEntity supportContext = new SupportEntity()) {
            supportContext.DownloadFiles.Attach(downloadFile);

            var productVersionIdsToAdd = (from v in selectedVersions
                                          where (downloadFile.ProductVersions.Any(pv => pv.Id == v) == false)
                                          select v).ToList();

            foreach (var productVersionId in productVersionIdsToAdd) {
                var productVersion = new ProductVersion() { Id = productVersionId };
                downloadFile.ProductVersions.Attach(productVersion); //Exception happens here.
                downloadFile.ProductVersions.Add(productVersion);
            }

            supportContext.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-05-30T12:57:20+00:00Added an answer on May 30, 2026 at 12:57 pm

    This is where Stub Entities become very very useful…

    var productVersion = new ProductVersion() { Id = productVersionId };  
    supportContext.AttachTo("ProductVersions", productVersion); 
    

    Here is a good article

    In above case, when attached productVersion is assigned to product versions‘ entity, productversion entity gets attached to context, with EntityState=Added. Entire graph will be in or out of context.

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

Sidebar

Related Questions

I have previously solved a similar problem in this question , where I asked
I have a problem similar to this question which was previously asked but my
Similar to this question: link However I have already mastered that. My problem is
I asked a similar question like this yesterday but after waiting for ever I
I have a similar problem as a previously solved problem of mine, except this
I have asked a similar question previously but it was never resolved so here
I've already asked question similar to this some time ago, I thought I solved
A while ago I asked this question: using xslt stylesheet to convert xhtml blank
I have posted a question on here previously asking similar advise, but this project
This problem is similar to when creating alternate colour table-rows, only we are dealing

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.