开发者

Entity Behavior in Entity Framework is very strange

开发者 https://www.devze.com 2023-03-31 06:26 出处:网络
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;

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.Ativ开发者_如何学Pythono);

    //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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消