Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.4 Doesn't detect changes done by AutoMapper

I'm using a repository patter for my webApi, and trying to make Update work. What this does is receives a "new" entity from the mvc webapi controller, and tries to update the existing object

public void Update(TEntity entity)
        {

            var oldEntry = _context.Set<TEntity>().Find(entity.Id);

            oldEntry = Mapper.Map(entity, oldEntry);

            Console.WriteLine(oldEntry.ToString());

            _context.SaveChanges();

        }

Save changes does nothing.

If I explicitly specify

oldEntry.SomeTextProperty = "TestText";

and then call

 _context.SaveChanges();

all is good. How to fix this ? or work around this ? Maybe there is a way to tell AutoMapper to invoke property setters ?

like image 210
Marty Avatar asked Mar 24 '26 07:03

Marty


1 Answers

You have to tell EF that you modified your entity before saving changes. This should work in framework 4.5

_context.Set<TEntity>().AddOrUpdate(new[] {oldEntry});

Or maybe this in framework 4:

_context.Entry(oldEntry).State = System.Data.EntityState.Modified;
like image 178
Kek Avatar answered Mar 26 '26 22:03

Kek