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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:23:21+00:00 2026-05-31T21:23:21+00:00

I cannot update created previously entity. I’m getting a StaleObjectException exception with message: Row

  • 0

I cannot update created previously entity. I’m getting a StaleObjectException exception with message:

Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Project.DomainLayer.Entities.Employee#00000000-0000-0000-0000-000000000000]

I don’t share the update process with anyone. What’s the problem?

Data Access / DI

public class DataAccessModule : Ninject.Modules.NinjectModule
{
    public override void Load()
    {
        this.Bind<ISessionFactory>()
            .ToMethod(c => new Configuration().Configure().BuildSessionFactory())
            .InSingletonScope();

        this.Bind<ISession>()
            .ToMethod(ctx => ctx.Kernel.TryGet<ISessionFactory>().OpenSession())
            .InRequestScope();

        this.Bind(typeof(IRepository<>)).To(typeof(Repository<>))
            .InRequestScope();
    }
}

Data Access / Mappings

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.DomainLayer"   namespace="Project.DomainLayer.Entities">
<class name="Employee" optimistic-lock="version">
    <id name="ID" column="EmployeeID" unsaved-value="00000000-0000-0000-0000-000000000000">
        <generator class="guid.comb" />
    </id>
    <version name="Version" type="Int32" column="Version" />
    <!-- properties -->
    <property name="EmployeeNumber" />
    <!-- ... -->
    <property name="PassportRegistredOn" not-null="true" />
    <!-- sets -->
    <set name="AttachedInformation" cascade="all">
        <key column="EmployeeID" />
        <element column="Attachment" />
    </set>
    <set name="TravelVouchers" cascade="all">
        <key column="EmployeeID" />
        <one-to-many class="TravelVoucher" />
    </set>
  </class>
</hibernate-mapping>

Data Access / Repository

public class Repository<T> : IRepository<T> where T : AbstractEntity<T>, IAggregateRoot
{
    private ISession session;

    public Repository(ISession session)
    {
        this.session = session;
    }

    // other methods are omitted

    public void Update(T entity)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Update(entity);
            transaction.Commit();
        }
    }
    public void Update(Guid id)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Update(this.session.Load<T>(id));
            transaction.Commit();
        }
    }
} 

Inside a Controller

public class EmployeeController : Controller
{
    private IRepository<Employee> repository;

    public EmployeeController(IRepository<Employee> repository)
    {
        this.repository = repository;
    }        
    public ActionResult Edit(Guid id)
    {
        var e = repository.Load(id);
        return View(e);
    }
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Employee employee)
    {
        if(ModelState.IsValid)
        {
            repository.Update(employee);
            return RedirectToAction("Deatils", "Employee", new { id = employee.ID });
        }
        else
        {
            return View(employee);
        }
    }
}

How do I update my entities?
Thanks!

EDIT

So I added unsaved-value="{Guid.Empty goes here}" to my markup. Moreover I’ve tried to do the next thing:

public void Update(T entity)
{
    using(var transaction = this.session.BeginTransaction())
    {
        try
        {
            this.session.Update(entity);
            transaction.Commit();
        }
        catch(StaleObjectStateException ex)
        {
            try
            {
                session.Merge(entity);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

    }
}

And this gives me the same effect.. I mean transaction.Commit(); after Merge gives the same exception.

Also I’m wondering should I expose, using hidden input, the entity ID on the Edit view?

EDIT

So entity really detaches. When it passes to controller the ID equals Guid.Empty. How do I handle it, Merge or Reattach?

  • 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-31T21:23:22+00:00Added an answer on May 31, 2026 at 9:23 pm

    There are two scenarios that you can run into, given your code pattern.

    1. You could retrieve the object from the db using ISession.Get() which can be followed by a change/update to the retrieved object. For this change to be effective, all you need to do is flush the session or commit the transaction as Nhibernate will track all the changes for you automatically.

    2. You have a transient instance, an object that is not associated with the ISession in context, from which you want to update. In this case, from my experience, the best practice is to ISession.Get() the object and make the corresponding changes to the object you just retrieve. (usually your view model is different from your domain model as well, don’t mix both) This pattern is shown below. It works all the time. Make sure you also use ISession.SaveOrUpdate().

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Employee employee)
    {
        if(ModelState.IsValid)
        {
            var persistentEmployee = repository.Get(employee.Id);
            if(  persistentEmployee == null){
                throw new Exception(String.Format("Employee with Id: {0} does not exist.", employee.Id));
            }
            persistentEmployee.Name = employee.Name;
            persistentEmployee.PhoneNumber = employee.PhoneNumber;
            //and so on
            repository.Update(persistentEmployee);
            return RedirectToAction("Deatils", "Employee", new { id = employee.ID });
        }
        else
        {
            return View(employee);
        }
    }
    

    Also, notice that your controller is probably instantiated on a per-request basis, hence, the lifetime of your ISession does not span multiple calls to the different methods you have in your controller. In other words, every method is almost always working within the context of a new ISession (unit of work).

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

Sidebar

Related Questions

For issues created in Jira, I cannot seem to update either Status or Resolution
Update: looks like this is indeed a WiX limitation - Cannot add 64bit com+
i want to update my sqlite database but i cannot find the way to
Assuming that I cannot run something like this with Fabric: run(svn update --password 'password'
A puzzler from a coworker that I cannot figure out... update btd.dbo.tblpayroll set empname
[Updated, sorry about the change but now to the real problem] I cannot include
I recently made an update to JasperReports library version 4.1.3. Previously, I had a
I recently updated my python installation to 2.7 (previously 2.5), and I've noticed a
I just released an update and got an exception report from the wild after
I have two models: UserProfile (extended from user) and Cv . I created another

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.