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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:50:41+00:00 2026-06-16T15:50:41+00:00

I have the following update method in my generic Repository public class Repository<T> :

  • 0

I have the following update method in my generic Repository

public class Repository<T> : IRepository<T> where T : class
{
    private readonly DbSet<T> _dbSet;
    public virtual T Update(T item) {
        return _dbSet.Attach(item);
    }
}

the UnitOfWork has a commit method which calls the SaveChanges on the context. More details here
https://codereview.stackexchange.com/questions/19037/entity-framework-generic-repository-pattern

When I update an entity and then call

ProductRepository.Update(modifiedProduct);
UnitOfWork.Commit;

Nothing floats down to the database.

However , Merely calling the Commit works ( no call to the update method ).

So, what is the Attach Method doing that causes the changes to not flow down to the database.
I think the attach call is the correct call to make in the Update Method. So, what is causing the unexpected behavior.

From the EF Source code on CodePlex

/// <summary>
///     Attaches the given entity to the context underlying the set.  That is, the entity is placed
///     into the context in the Unchanged state, just as if it had been read from the database.
/// </summary>
/// <param name="entity"> The entity to attach. </param>
/// <returns> The entity. </returns>
/// <remarks>
///     Attach is used to repopulate a context with an entity that is known to already exist in the database.
///     SaveChanges will therefore not attempt to insert an attached entity into the database because
///     it is assumed to already be there.
///     Note that entities that are already in the context in some other state will have their state set
///     to Unchanged.  Attach is a no-op if the entity is already in the context in the Unchanged state.
/// </remarks>
public object Attach(object entity)
{
    Check.NotNull(entity, "entity");

    InternalSet.Attach(entity);
    return entity;
}
  • 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-16T15:50:43+00:00Added an answer on June 16, 2026 at 3:50 pm
    ///     Attach is used to repopulate a context with an entity that is known to already exist in the database.
    ///     SaveChanges will therefore not attempt to insert an attached entity into the database because
    ///     it is assumed to already be there.
    ///     Note that entities that are already in the context in some other state will have their state set
    ///     to Unchanged. 
    

    After attaching the entity, the state will be Unchanged, so no UPDATE sql is going to fire for that entity. You would need to manually set the state of the entity after attaching.

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

Sidebar

Related Questions

Suppose you have the following Generic class heirarchy: public abstract class GenericBase<T> { T
I have the following controller: class Tests extends CI_Controller { public function update_record_test() {
I have the following code in an Update method of a ContentProvider subclass: When
Here is my problem in C# : I have the following classes: public class
I'm trying to create an update method in a generic repository as a LINQ
I have the following code: public class OMyObject { public int Id { get;
If i have the following entity: public class PocoWithDates { public string PocoName {
I have the following structure: abstract class Base { public abstract List<...> Get(); //What
I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I have tried the following MySQL update but it only sets the first field

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.