0

I'm using EF 4.1 and I have a Edit method in repository:

public virtual void Edit(TEntity entity)
{
   this.DbContext.Entry(entity).State = EntityState.Modified;
}

This method don't update my aggregates, I have this entity:

public class Credencial : Entity
{
    private IList<Perfil> _perfis;

    public Credencial()
    {
        Status = Status.Inativo;
    }

    public virtual Usuario Usuario { get; set; }

    public virtual IList<Perfil> Perfis
    {
        get { return _perfis ?? (_perfis = new List<Perfil>()); }
        set { _perfis = value; }
    }

    public virtual Status Status { get; set; }
    public byte StatusId
    {
        get { return (byte)Status; }
        set { Status = (Status)value; }
    }
    public string NomeUsuario { get; set; }

    public string Senha { get; set; }
}

I'm trying to update Perfis property like this:

[TestMethod]
public void DeveEditarUsuarioNoRepositorio()
{
    Usuario usuario = _usuarioRepository.GetById(1);

    usuario.Credencial.Perfis = null;

    usuario.Nome = "Samla Peidorreira";
    usuario.Email = "samlapeidanascalca@samlapeidanascalca.com";
    usuario.DataNascimento = new DateTime(1988, 11, 19, 4, 23, 54, 0);
    usuario.Sexo = (Sexo)Convert.ToByte(Sexo.Masculino);
    usuario.Telefone = null;
    usuario.Credencial.Status = (Status) Convert.ToByte(Status.Ativo);

    //Here I want to replace Perfis, but it only add one more
    usuario.Credencial.Perfis = new List<Perfil>() { new Perfil() { Nome = "Fotografos", DataEdicao = new DateTime(1996, 2, 1, 12, 15, 42, 27), Deletado = true, Status = false, Tipo = 3 } };

    usuario.Credencial.NomeUsuario = "samlapeidanascalca";
    usuario.Credencial.Senha = "samlacagona";

    _usuarioRepository.Edit(usuario);

    _context.SaveChanges();

    Usuario usuarioEditado = _usuarioRepository.GetById(1);

    Assert.AreEqual("Samla Peidorreira", usuarioEditado.Nome);
    Assert.AreEqual("samlapeidanascalca@samlapeidanascalca.com", usuarioEditado.Email);
    Assert.AreEqual(new DateTime(1988, 11, 19, 4, 23, 54, 0), usuarioEditado.DataNascimento);
    Assert.AreEqual(Sexo.Masculino, usuarioEditado.Sexo);
    Assert.AreEqual(null, usuarioEditado.Telefone);
    Assert.AreEqual(Status.Ativo, usuarioEditado.Credencial.Status);
    Assert.AreEqual(1, usuarioEditado.Credencial.Perfis.Count);
    Assert.AreEqual(5, usuarioEditado.Credencial.Perfis[0].Id);
    Assert.AreEqual("samlapeidanascalca", usuarioEditado.Credencial.NomeUsuario);
    Assert.AreEqual("samlacagona", usuarioEditado.Credencial.Senha);
}

What happen is the repository add one more Perfil in my entity. I don't want to add one more, I want to replace.

Update:

I found a question with same problem: Update method for generic Entity framework repository

Community
  • 1
  • 1
Acaz Souza
  • 8,311
  • 11
  • 54
  • 97

1 Answers1

0

Setting state of the entity has no effect on the state of its relations. If you want to replace data in Perfil you must handle it separately and set its correct state. It is a feature (it is about ObjectContext API but the same is true or worse with DbContext API).

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • OMG, are you sure? Why EF do it with me? You not think EF is implemented in a wrong way? – Acaz Souza Aug 29 '11 at 13:34
  • I'm pretty sure. Once you are working with detached entities you must manually set state of each entity or relation once you attache them to the context. – Ladislav Mrnka Aug 29 '11 at 13:37