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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:05:31+00:00 2026-05-15T05:05:31+00:00

I have an ASP.NET application using an entity framework model. In an import routine,

  • 0

I have an ASP.NET application using an entity framework model. In an import routine, with the code below, I get a “Cannot insert duplicate key” exception for AccountNum on the SaveChanges call, but when execution stops for the exception, I can query the database for the apparently duplicated field, and no prior record exists.

        using (var ents = new PvmmsEntities())
        {
            foreach (DataRow row in importedResources.Rows)
            {
                var empCode = row["EmployeeCode"].ToString();
                try
                {
                    var resource = ents.ActivationResources.FirstOrDefault(rs => rs.EmployeeCode == empCode);
                    if (resource == null)
                    {
                        resource = new ActivationResources();
                        resource.EmployeeCode = empCode;
                        ents.AddToActivationResources(resource);
                    }
                    resource.AccountNum = row["AccountNum"].ToString();
                    ents.SaveChanges(true);
                } catch(Exception ex)
                {
                }
            }
        }

UPDATE:
With employee 1546 I catch a valid duplicate key exception; he has a duplicate bank account. Then, the very next employee is 1548 (1547 is genuinely missing). 1548 has a unique bank account, but for 1548 I get the duplicate key exception on SaveChanges. A profile shows that SaveChanges is still trying to insert 1546, which in fact does still have a duplicate bank account.

  • 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-15T05:05:32+00:00Added an answer on May 15, 2026 at 5:05 am

    Your original object (the one with the “real” duplicate error) is still in the context. Doing SaveChanges after adding the “good” object later on tries to save both objects. You must remove the “bad” object you added to the context or new up a new context without the “bad” object added.

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

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If it is about WCF exceptions, I would recommend plugging… May 16, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer You don't have to put your Models in the 'Models'… May 16, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer Simple code to redirect page <!-- html button designing and… May 16, 2026 at 6:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I'm using the Entity Framework as my ORM for an ASP.Net (webforms) application. Suppose
I have an asp.net mvc calendar application (using jquery ui datepicker) and i am
I have an asp.net application which is purely build using C#, CSS, Javascript. Now
I have an ASP.Net MVC application and I am using StructureMap within MVC to
We are building an ASP.Net MVC 2 multi-tenant application on SQL Server using the
I have an Entity Framework data model. Part of the model is a Customer
I'm in the process of building my first web application using ASP.NET MVC 2
I'm writing a log parser for an asp.net mvc2 web application. I use Entity
I am an asp.net web application developer and I always have used Enterprise Library

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.