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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:52:23+00:00 2026-06-15T08:52:23+00:00

I am trying to handle a situation in a bulk insert process where there

  • 0

I am trying to handle a situation in a bulk insert process where there may be entities with the same primary key, which of course is going to make SaveChanges throw an exception.

Here’s what I have:

try
{
    _context.SaveChanges();
    _context.Dispose();
    _context = null;
    _context = SelectContext<T>();
    _commitCount = 0;
}
catch (System.Data.UpdateException updateEx)
{
    //Remove from _context all the entries that errored in the SaveChange() process...
    if (updateEx.StateEntries != null)
    {
        foreach (ObjectStateEntry stateEntry in updateEx.StateEntries)
        {

            if ((System.Data.EntityState)stateEntry.Entity.GetType().GetProperty("EntityState").GetValue(stateEntry.Entity, null) != System.Data.EntityState.Detached)
            {
                _context.Detach(stateEntry.Entity);
            }
         }

}

//Save context changes again this time without erroneous entries...
try
{
     _context.SaveChanges();
     _context.Dispose();
     _context = null;
     _context = SelectContext<T>();
     _commitCount = 0;
 }
 catch (Exception ex)
 {                  
   //Welp, at the point, I'm clueless...
 }

If I look in the ObjectStateManager, the entity is indeed removed (the count goes down by the number of time the foreach loop is iterated.)

But it still throws an exception on the second attempt, whinning about a dupe PK.

I tought detaching an entity was the same is if it was never in the context in the first place. Do I need to do something else ?

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-06-15T08:52:24+00:00Added an answer on June 15, 2026 at 8:52 am

    This is what I ended up doing in my case:

    _context.ObjectStateManager.ChangeObjectState(stateEntry.Entity, System.Data.EntityState.Unchanged);
    _context.ApplyOriginalValues<T>(entityType, (T)stateEntry.Entity);
    _context.Detach(stateEntry.Entity);
    

    After that, I’m able to save the context changes.

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

Sidebar

Related Questions

I'm trying to figure out how to handle this situation. I have the following
I am trying to configure logback-classic to handle the following situation: All messages originating
I am trying to handle a case of a DateTime which might be null
I am using AJAX in asp:net and am trying to handle a situation where
I am trying to handle flow control situation on producer end. I have a
I am trying to handle a situation when after an successful authentication with openId
I'm trying handle bad json data when parsed through json_decode(). I'm using the following
I'm trying to handle a JSONP Callback, I have the following JavaScript var URL
I'm trying to handle a file address within my python program and pass it
I'm trying to handle the problem where a user can try to open, with

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.