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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:08:12+00:00 2026-05-23T22:08:12+00:00

Actually I use DbContext, but I just tested it with ObjectContext as well. //

  • 0

Actually I use DbContext, but I just tested it with ObjectContext as well.

// Engine 1
Engine engine = new Engine();
Manufacturer manufacturer = new Manufacturer();

engine.Manufacturer = manufacturer;

// Engine 2
engine = new Engine();
engine.Manufacturer = manufacturer // Engine 2 has the same manufacturer like Engine 1

context.SaveChanges();

I use identity columns (int), where new IDs are generated. In debug mode I see that the ID for engine is “0”. Well, if I implement context.SaveChanges right after the Engine 1 block, the new manufacturer is being saved to DB. With EntityKey or Any check, I can reference the new manufacturer to Engine 2 without any problems. But without immediate SaveChanges(), two entries of same manufacturer are being saved to DB (the code above). Is EF unable to reference internally like normal objects? As you see above, manufacturer is one and the same object, so I wonder if it’s possible to obtain a successfull insert without pre-save the child/manufacturer.

EDIT: I think I found the problem

MachineEntities context = new MachineEntities();
        context.Configuration.AutoDetectChangesEnabled = true;

        // Engine 1
        Engine engine1 = new Engine();
        engine1.Name = "engine1";

        Manufacturer manufacturer = new Manufacturer();
        manufacturer.Name = "manufacturer1";

        engine1.Manufacturer = manufacturer;

        // Engine 2
        Engine engine2 = new Engine();
        engine2.Name = "engine2";

        manufacturer = new Manufacturer();
        manufacturer.Name = "manufacturer1";

        engine2.Manufacturer = manufacturer;


        // Add Engine 1

        if (context.Manufacturers.Any(m => m.Name == engine1.Manufacturer.Name))
        {
            // The manufacturer's name is identical, so use the one in the context instead the assigned one.
            engine1.Manufacturer = context.Manufacturers.Single(m => m.Name == engine1.Manufacturer.Name);
        }
        else
        {
            // The manufacturer is not known, add it to the context
            context.Set<Manufacturer>().Add(engine1.Manufacturer);
        }

        // Add Engine 2


        if (context.Manufacturers.Any(m => m.Name == engine1.Manufacturer.Name))
        {
            // The manufacturer's name is identical, so use the one in the context instead the assigned one.
            engine2.Manufacturer = context.Manufacturers.Single(m => m.Name == engine2.Manufacturer.Name);
        }
        else
        {
            context.Manufacturers.Add(engine2.Manufacturer);
        }

        context.SaveChanges();

        context.Dispose();

“Any” or any comparison won’t give me any results. It only gives me those entities, which are already persisted in DB, but not the fresh new added ones. So it’s duplicated. The local ones are ignored as I see in debugger, and the one in “Result View” are the one, where the command is executed against. So the new added entities are located in “Manufacturers.Local”.

  • 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-23T22:08:13+00:00Added an answer on May 23, 2026 at 10:08 pm

    I just tried the following:

    var a1 = new Activity{WorkflowId = 1, Title = "test"};
    var a2 = new Activity{WorkflowId = 1, Title = "test2"};
    var d = new WorkflowDisplay{WorkflowId = 1, Title = "Test"};
    a1.WorkflowDisplay = d;
    a2.WorkflowDisplay = d;
    // Any of the next three lines can be commented out, but if
    // they are all commented, nothing happens.
    AddToWorkflowDisplays(d);
    AddToActivities(a1);
    AddToActivities(a2);
    SaveChanges();
    

    … and I only saw a single WorkflowDisplay added. So I’m pretty sure this has something to do with your specific implementation. Have you overridden the GetHashCode or Equals methods of any of your entities, or done any similar sort of customization of the auto-generated code?

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

Sidebar

Related Questions

Is it possible to actually make use of placement new in portable code when
I actually use a combination of OS X, Linux and Windows, but Windows is
My project actually use XML files to define flow of the application. I like
I'm quite new to Flex and wondered if other developers actually use its stylesheet
Can someone give an example of a good time to actually use unsafe and
I'm trying to grasp the concept of .NET Generics and actually use them in
I've been trying to use WF in my ASP.NET application (actually, it's ASP.NET MVC
What program can I use to decompile a class file? Will I actually get
I am actually rendering an excel file in the browser. I use the Response.Writefile(filePath)
Is it possible to actually use the Xcode debugger when running an iPhone app

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.