0

I'm having problems updating entities with EF6 and MySql.

This is part of the code:

public partial class Repositorio<T> : IRepositorio<T> where T : EntidadBase
{
    private readonly IDbContext _contexto;
    private IDbSet<T> _entidades;

    public Repositorio(IDbContext contexto)
    {
        this._contexto = contexto;
    }

    private IDbSet<T> Entidades
    {
        get
        {
            if (_entidades == null)
                _entidades = _contexto.Set<T>();
            return _entidades;
        }
    }

    public void Insert(T entity)
    {
        try
        {
            if (entity == null)
                throw new ArgumentNullException("entity");

            this.Entidades.Add(entity);

            this._contexto.SaveChanges();
        }
        catch (Exception dbEx)
        {
            throw dbEx
        }
    }


    public void Update(T entidad)
    {
        try
        {
            if (entidad == null)
                throw new ArgumentNullException("entidad");

            this._contexto.SaveChanges();
        }
        catch (Exception dbEx)
        {
            throw dbEx;
        }
    }
}

And I used it with something like that:

var _repositorio = new Repositorio<MyEntity>(myContext);
var myEntity = _repositorio.GetById(13);

myEntity.Name = "Mooo";

_repositorio.Update(myEntity);

It doesn't throw any exception, but the data has no changes.

The Insert() method works perfect.

Any idea about this?

PS: I'm using MySql.Data.Entities from NuGet Package Installer, and it includes EF 6.0.0, MySql.Data 6.8.3.0 and MySql.Data.Entity.EF6 6.8.3.0.

dank0ne
  • 37
  • 8
  • Your sample code shows the insert method, but the call to the repo calls the update method. What is the code for the update method you are calling? – Neil Hosey Mar 09 '14 at 16:53
  • Hello Neil. You have to scroll down in the code block of the repository to see it :-) – dank0ne Mar 09 '14 at 17:36
  • Consider to remove this repository layer. It will only trouble you. For one, you can only update single instances of data. What if you want to save many objects in one transaction? See [this question](http://stackoverflow.com/q/21758807/861716) for a profound discussion of the subject. And [this one](http://stackoverflow.com/questions/5625746/generic-repository-with-ef-4-1-what-is-the-point/5626884#5626884) may convince you to work with `DbSet` and `DbContext` directly. – Gert Arnold Mar 10 '14 at 09:53
  • Thank you Gert. I will think about it :-) – dank0ne Mar 10 '14 at 10:23

1 Answers1

0

haha, ah ok I see your problem.. change your update method to this:

 public void Update(T entidad)
    {
        try
        {
            if (entidad == null)
                throw new ArgumentNullException("entidad");

            _contexto.Entry(entidad).State = System.Data.EntityState.Modified;
            this._contexto.SaveChanges();
        }
        catch (Exception dbEx)
        {
            throw dbEx;
        }
    }

Also, have a look at using the UnitOfWork pattern along with the repository pattern, you will need it.

Neil Hosey
  • 465
  • 5
  • 23
  • But the entity already should be `Modified` at this point (assuming that `GetById` attaches it to the repository's context). – Gert Arnold Mar 10 '14 at 09:46