How to remove an object from a HasManyToMany relationship?

0

I have 2 entities that relate: Entities Empresa and FormaPagamento many companies can have many forms of payment, to save this working well but now I want to remove the forms of payment that the company has and I can not.

How to do this?

Mappings

public class EmpresaMap : ClassMap<Empresa> {
    public EmpresaMap() {
        Table("EMPRESA");
        Id(e => e.id).GeneratedBy.Native();
        Map(e => e.razaoSocial).Not.Nullable().Length(255);
        Map(e => e.nomeFantasia).Length(255);
        Map(e => e.cnpj).Not.Nullable().Length(18); //22.222.222/0001-00
        Map(e => e.inscEstadual).Length(255);
        Map(e => e.telefone).Not.Nullable().Length(14); //(17)98156-8355
        Map(e => e.isAberta);
        Map(e => e.imagem); 

        //embeddable
        Component(e => e.endereco,  e => {
                                    e.Map(c => c.endereco);
                                    e.Map(c => c.numero).CustomType<int>();
                                    e.Map(c => c.bairro);
                                    e.Map(c => c.complemento);
                                    e.Map(c => c.cidade);
                                    e.Map(c => c.cep);
                                    e.Map(c => c.uf).CustomType<GenericEnumMapper<EstadosBrasil>>();
        });

        References<Usuario>(e => e.usuario).Column("usuario_id");
        HasManyToMany<FormaPagamento>(e => e.formasPagto)
                                    .Table("empresa_formaspagto")
                                    .ParentKeyColumn("empresa_id")
                                    .ChildKeyColumn("formapagto_id")
                                    .Cascade
                                    .SaveUpdate();

    }
}


public class FormaPagamentoMap : ClassMap<FormaPagamento> {
    public FormaPagamentoMap(){
        Table("FORMAPAGAMENTO");
        Id(fp => fp.id).GeneratedBy.Native();
        Map(fp => fp.descricao).Not.Nullable().Unique().Length(50);

        HasManyToMany<Empresa>(fp => fp.empresas)
                                    .Table("empresa_formaspagto")
                                    .ParentKeyColumn("formapagto_id")
                                    .ChildKeyColumn("empresa_id")
                                    .Inverse();


    }
}

To remove the form of payment from the company I'm trying to do this.

/// <summary>
        /// Remove a forma de pagamento da empresa
        /// </summary>
        /// <param name="idEmp">ID da empresa</param>
        /// <param name="idFPG">ID da forma pagamento</param>
        /// <returns>JSON result</returns>
        public JsonResult deleteFormaPagto(long idEmp, long idFPG){
            try{
                Empresa empresa = dao.findObject(idEmp);
                FormaPagamento fp = fpDAO.findObject(idFPG);

                Boolean isDeleted = fpDAO.delete(fp);
                if (isDeleted){
                    jsonResposta.Add("status", "1");
                    jsonResposta.Add("msg", "Forma de pagamento removida com sucesso.");
                }else{
                    jsonResposta.Add("status", "0");
                    jsonResposta.Add("msg", "Erro tentando remover forma de pagamento.");
                }
            }catch (Exception e){
                jsonResposta.Add("status", "0");
                jsonResposta.Add("msg", e.Message);
            }
            return Json(jsonResposta);
        }
    
asked by anonymous 25.10.2016 / 16:49

1 answer

0

Solved. I just removed the item from the list and did the update. As the mapping this SaveUpdate() already does automatically.

It was like this.

/// <summary>
        /// Delete formapagamento da empresa
        /// </summary>
        /// <param name="idEmp">ID empresa</param>
        /// <param name="idFPG">ID formapagamento</param>
        /// <returns>JSON result</returns>
        public JsonResult deleteFormaPagto(long idEmp, long idFPG){
            try{
                Empresa empresa = dao.findObject(idEmp);
                FormaPagamento fp = empresa.formasPagto.SingleOrDefault(fpg => fpg.id == idFPG);                
                empresa.formasPagto.Remove(fp);

                Boolean isDeleted = dao.update(empresa);
                if (isDeleted){
                    jsonResposta.Add("status", "1");
                    jsonResposta.Add("msg", "Forma de pagamento removida com sucesso.");
                }else{
                    jsonResposta.Add("status", "0");
                    jsonResposta.Add("msg", "Erro tentando remover forma de pagamento.");
                }
            }catch (Exception e){
                jsonResposta.Add("status", "0");
                jsonResposta.Add("msg", e.Message);
            }
            return Json(jsonResposta);
        }
    
25.10.2016 / 19:07