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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:59:39+00:00 2026-06-16T00:59:39+00:00

I am trying to attach an Entity that has never been ‘pulled’ from the

  • 0

I am trying to attach an Entity that has never been ‘pulled’ from the DB and have it update the existing row. For example, I am passing a full model through in MVC (modified), and I am just trying to save it without a roundtrip to first get the entity, then edit since I already have the info from a previous call.

What I have is this:

public static int ModifyExistingEntity<T>(this DbContext db, T updated) where T : class
{
    DbSet<T> set = db.Set<T>();
    set.Attach(updated);
    db.Entry(updated).State = EntityState.Modified;
    return db.SaveChanges();
}

But when I run it I get this:

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store
update, insert, or delete statement affected an unexpected number of
rows (0). Entities may have been modified or deleted since entities
were loaded. Refresh ObjectStateManager entries. —>
System.Data.OptimisticConcurrencyException: Store update, insert, or
delete statement affected an unexpected number of rows (0). Entities
may have been modified or deleted since entities were loaded. Refresh
ObjectStateManager entries.

Which means nothing was updated. What am I missing here? The primary Key is set correctly, but remember that the Entity is not pulled from the DB, it was constructed from previously queried information + updates.

  • 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-16T00:59:40+00:00Added an answer on June 16, 2026 at 12:59 am

    May be some issue with the static extension you can try with inheritance,

    public class MyContext:DbContext {
    
      public int ModifyExistingEntity<T>(this DbContext db, T updated) where T : class
      {
        DbSet<T> set = Set<T>();
        set.Attach(updated);
        this.Entry(updated).State = EntityState.Modified;
        return SaveChanges();
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An attempt has been made to Attach or Add an entity that is not
I'm trying to attach my database on a server that is running SQL2005 and
I have a question related to this one : I'm trying to attach an
I've been getting several errors: cannot add an entity with a key that is
I'm trying to use the entity framework to take data (nested relationships) from one
I'm trying attach a footer, that is fixed and always visible, to the bottom
I'm trying to attach a click function that adds a CSS class to all
I am getting the following error while trying to update an entity, 'An entity
I am trying to attach Watin to an existing instance of Internet Explorer 9.
I am trying to attach a file from from my website using pulling from

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.