Problem in RepositoryBase.cs

Mar 20, 2012 at 10:57 AM
Edited Mar 20, 2012 at 11:00 AM

I'm having a problem where I try to Update a DBContext and I get en error when I try to Update:

"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." 

public virtual void Update(T entity)
{
	dbset.Attach(entity); //<-error
        dataContext.Entry(entity).State = EntityState.Modified;
}
If I change to:

public virtual void Update(T entity)
{
	this.dataContext.SaveChanges();
}

It works but I'm not sure if I'm doing to correct thing..
Mar 22, 2012 at 12:49 PM

Never mind this was a mistake of mine. I wasn't instantiating the services correctly.

 

Would be nice to add some sample Unit test objects. If anyone needs them just get in touch with me.

Apr 20, 2012 at 12:40 PM

finally this is correct or not, because on your last download souurce code it's not changed

Tx

Apr 20, 2012 at 4:21 PM
jlsfernandez wrote:

finally this is correct or not, because on your last download souurce code it's not changed

Tx

Original code is fine. It was my test units that where wrong. This project does not include test units....

Feb 5, 2013 at 1:39 PM
Hi fabriciom,

I'm getting the same error on the same line in RepositoryBase.cs.

I too can fix the issue with the code you're using above, however, I'd like to make sure I'm not calling it incorrectly from my controller as I'm getting the same error currently.

Does this look incorrect?
            var policy = policyRepository.GetById(pk);            
            var command = new CreateOrUpdatePolicyCommand();
            
            command.PolicyNumber = value;
            command.PolicyId = policy.PolicyId;
            command.UserId = policy.UserId;            
            command.InsurerId = policy.InsurerId;
            command.Insured = policy.Insured;
                   
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);
            }
Feb 5, 2013 at 2:12 PM
Jason,

I gave up on using entity framework and changed to NHibernate.

-Cheers,
Fabricio