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);
}