Controller Edit - Entering Values

Asked

Viewed 171 times

3

My method responsible for editing is inserting values in the table CONSUL_EnciclopediasCONSUL_Promocoesinstead of editing. Therefore, if I have the options x with id=1 and y id=2 within the course to with id=1 and originally I choose the option x, my table gets 1 and 1. I edit and now choose the option y, My table keeps 1 and 1 and also inserts 1 and 2.

Domain Encyclopedias

 public class CONSUL_Enciclopedias
    {
        public int ID { get; set; }
        public string Nome { get; set; }
        public string Descricao { get; set; }
        public string PrazoEntrega { get; set; }
        public string Banner { get; set; }
        public string Foto { get; set; }
        public string LinkLoja { get; set; }
        public string DataEnvio { get; set; }
        public bool Ativo { get; set; }
        public virtual ICollection<CONSUL_Promocoes> Promocoes { get; set; }

    }

Domain Promotions

public class CONSUL_Promocoes
    {
        public int ID { get; set; }
        public string Nome { get; set; }
        public string Foto { get; set; }
        public string Descricao { get; set; }
        public bool Ativo { get; set; }
        public virtual ICollection<CONSUL_Enciclopedias> Enciclopedias { get; set; }

    }

My table CONSUL_EnciclopediasCONSUL_Promocoes receives CONSUL_Enciclopedias_ID and also CONSUL_Promocoes_ID

My editing method in my Encyclopedia Controller

[CustomActionFilter]
[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Edit(int id, CONSUL_Enciclopedias Enciclopedias, FormCollection collection)
{
    PromocoesAplicacao bdPromocoes;
    EnciclopediasAplicacao bdEnciclopedia;
    bdPromocoes = PromocoesAplicacaoConstrutor.PromocoesAplicacaoEF();
    bdEnciclopedia = EnciclopediasAplicacaoConstrutor.EnciclopediasAplicacaoEF();

    var enciclopedia = bdEnciclopedia.ListarPorId(id.ToString());
    var promoBD = bdPromocoes.ListarTodos();
    var listaCombo = new List<CONSUL_Promocoes>();

    if (!string.IsNullOrEmpty(collection["Promocoes"]))
    {
        var promos = collection["Promocoes"].ToString().Split(',');

        foreach (var promo in promos)
        {
            listaCombo.Add(promoBD.First(c => c.ID.ToString() == promo));
        }
    }

    Enciclopedias.Promocoes = listaCombo;
    bdEnciclopedias.Salvar(Enciclopedias);
    return null;
}

Repository

public class EnciclopediasRepositorioEF : IRepositorio<CONSUL_Enciclopedias>
{
    private readonly Contexto contexto;

    public EnciclopediasRepositorioEF()
    {
        contexto = new Contexto();
    }

    public void Salvar(CONSUL_Enciclopedias entidade)
    {
        if (entidade.ID > 0)
        {
            var EnciclopediasAlterar = contexto.Enciclopedias.First(x => x.ID == entidade.ID);
            EnciclopediasAlterar.PrazoEntrega = entidade.PrazoEntrega;
            EnciclopediasAlterar.Banner = entidade.Banner;
            EnciclopediasAlterar.Nome = entidade.Nome;
            EnciclopediasAlterar.Descricao = entidade.Descricao;
            EnciclopediasAlterar.Foto = entidade.Foto;
            EnciclopediasAlterar.LinkLoja = entidade.LinkLoja;
            EnciclopediasAlterar.Ativo = entidade.Ativo;
            EnciclopediasAlterar.DataEnvio = entidade.DataEnvio;
            EnciclopediasAlterar.CONSUL_Promocoes = entidade.CONSUL_Promocoes.Select(promo => contexto.Promocoes.FirstOrDefault(y => y.ID == promo.ID)).ToList();
        }
        else
        {
            entidade.CONSUL_Promocoes.Select(promo => contexto.Promocoes.FirstOrDefault(y => y.ID == promo.ID)).ToList();
            contexto.Enciclopedias.Add(entidade);
        }
        contexto.SaveChanges();
    }

    public void Excluir(CONSUL_Enciclopedias entidade)
    {
        var cartaAlterar = contexto.Enciclopedias.First(x => x.ID == entidade.ID);

        contexto.Set<CONSUL_Enciclopedias>().Remove(cartaAlterar);
        contexto.SaveChanges();
    }

    public IEnumerable<CONSUL_Enciclopedias> ListarTodos()
    {
        return contexto.Enciclopedias.Include(x => x.CONSUL_Promocoes).ToList();
    }

    public CONSUL_Enciclopedias ListarPorId(string id)
    {
        int idInt;
        Int32.TryParse(id, out idInt);
        return contexto.Enciclopedias.Include(x => x.CONSUL_Promocoes).FirstOrDefault(x => x.ID == idInt);
    }
}
  • You can also enter the code of your application classes, please?

  • @Of course, I edited. Thank you

1 answer

5


Another example of the classic problem of the highlighted context.

When you do that:

bdPromocoes = PromocoesAplicacaoConstrutor.PromocoesAplicacaoEF();
bdEnciclopedia = EnciclopediasAplicacaoConstrutor.EnciclopediasAplicacaoEF();

You are creating two separate contexts, which do not know the information that each one monitors. Therefore, even if the record already exists, the context will understand that it is a new record.

My suggestion is to instantiate the context in the Controller and build the application classes with it:

public EnciclopediasRepositorioEF()
{
    contexto = new Contexto();
}

public EnciclopediasRepositorioEF(Contexto contexto)
{
    this.contexto = contexto;
}

And then:

var contexto = new Contexto();

bdPromocoes = PromocoesAplicacaoConstrutor.PromocoesAplicacaoEF(contexto);
bdEnciclopedia = EnciclopediasAplicacaoConstrutor.EnciclopediasAplicacaoEF(contexto);

Another problem I consider is your modeling. You modeled like this:

public virtual ICollection<CONSUL_Enciclopedias> Enciclopedias { get; set; }
public virtual ICollection<CONSUL_Promocoes> Promocoes { get; set; }

This does not produce an associative table alone. You need to explain by Fluent API that there is this association (I’m not a fan of this approach, personally speaking), or else model a Model extra that associates the two (my favorite approach):

public class EnciclopediaPromocao 
{
    [Key]
    public int EnciclopediaPromocaoId { get; set; }
    [Index("IUQ_EnciclopediaPromocao_EnciclopediaId_PromocaoId", IsUnique = true, Order = 1)]
    public int EnciclopediaId { get; set; }
    [Index("IUQ_EnciclopediaPromocao_EnciclopediaId_PromocaoId", IsUnique = true, Order = 2)]
    public int PromocaoId { get; set; }

    public virtual Enciclopedia Enciclopedia { get; set; }
    public virtual Promocao Promocao { get; set; }
}

And then change the associations in both Models, Enciclopedia and Promocao:

public virtual ICollection<EnciclopediaPromocao> EnciclopediaPromocoes { get; set; }

[Index], introduced in this form from the Entity Framework 6.1.0, ensures the uniqueness of the associative record. Additional validations may be required in the application to avoid strange key duplicity errors for the user.

  • Just a question. I changed the line EnciclopediasAlterar.CONSUL_Promocoes = entidade.CONSUL_Promocoes.Select(promo => contexto.Promocoes.FirstOrDefault(y => y.ID == promo.ID)).ToList(); for EnciclopediasAlterar.CONSUL_Promocoes = contexto.Promocoes.ToList(); . Now I am using the same context, however, it is giving the same error. It was not to have occurred something different?

  • No. This wouldn’t happen if the modeling was correct. From what I understand, you want to associate Encyclopedias with Promotions. There would be an associative table between the two (for example PromocoesEnciclopedia) so that promotions are not inserted in duplicate. You need me to put this in the answer?

  • Would you kindly put?

  • 1

    @Rafaelbarbosa Feito.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.