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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:27:49+00:00 2026-05-26T11:27:49+00:00

I am working on a WPF application using Entity Framework 4.0. When I tried

  • 0

I am working on a WPF application using Entity Framework 4.0. When I tried to save the object, I got a primary key exception, but the primary key is an AutoIncremented field and I cannot understand the reason for the exception.

So after trying this and that, and a little debugging and using the SQL profiler, I found out that prior to inserting my object, a record must be inserted in the parent table, as I set the navigation property of that object.

So the crux is if an attempt to insert Employee object and set its department as Employee.Department = deptObject, then a new record is set to be inserted on department object.

Kindly suggest me someway by which navigational property objects won’t be inserted in the database, any property or any method, Anything.

Thanks

  • 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-26T11:27:49+00:00Added an answer on May 26, 2026 at 11:27 am

    This is the way how EF works if you incorrectly use detached entities. I suppose you are using something like this:

    var employee = new Employee();
    employee.Department = GetDepartmentFromSomewhere(departmentId);
    
    ...
    
    using (var context = new YourContext())
    {
        context.Employees.AddObject(employee);
        context.SaveChanges();
    }
    

    This code prepared employee entity, added reference to existing department and saved new employee to the database. Where is the problem? The problem is that AddObject doesn’t add only employee but whole object graph. That is how EF works – you cannot have object graph where part of objects are connected to context and part of not. AddObject adds every object in the graph as a new one (new one = insert in database). So you must either change sequence of your operations or fix state of entities manually so that your context knows that department already exists.

    First solution – use the same context for loading department and saving employee:

    using (var context = new YourContext())
    {
        var employee = new Employee();
        ...
        context.Employees.AddObject(employee);
    
        employee.Department = context.Departments.Single(d => d.Id == departmentId);
        context.SaveChanges();
    }
    

    Second solution – connect entities to the context separately and after that make reference between entities:

    var employee = new Employee();
    ...
    
    var department = GetDepartmentFromSomewhere(departmentId);
    
    using (var context = new YourContext())
    {
        context.Employees.AddObject(employee);
        context.Departments.Attach(department);
        employee.Department = department;
    
        context.SaveChanges();
    }
    

    Third solution – correct state of the department manually so that context doesn’t insert it again:

    var employee = new Employee();
    employee.Department = GetDepartmentFromSomewhere(departmentId);
    
    ...
    
    using (var context = new YourContext())
    {
        context.Employees.AddObject(employee);
        context.ObjectStateManager.ChangeObjectState(employee.Department, 
                                                     EntityState.Unchanged);
        context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an WPF application using the mvvm-light framework. I'm new to both
I am working on WPF application which uses Entity Framework alongwith POCO classes. I
I'm working on a WPF application and is using the Model-View-ViewModel pattern. The application
I'm working on a WPF application, and I'm structuring it using the MVVM pattern.
I'm working on writing a small application using the WPF MediaKit , and I
I'm working on a WPF MVVM application and I've got a TextBox on my
I am working on a WPF application and using a scrollViewer to view the
I am working on a wpf application, when using Combo Box control i am
I am currently working on an application using WPF and MVVM. Now if I
I'm working on a new C# application using WPF. I was under the impression

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.