Delete an item from many-to-many relationship

纵然是瞬间 提交于 2019-12-06 13:02:36
Radim Köhler

The answer is (I am really sure) here: NHibernate Deleted object would be re-saved by cascade

Let me re-phrase that for your case, what could happen:

  1. we remove an GrupoArquivo from ArquivoRetorno.GrupoModulos collection.
  2. During that transaction, unit of work, we also tuch and therefore load the GrupoModulo
  3. GrupoModulo gets initiated - and NOW - collection of Arquivos is loaded. So the reference to removed GrupoArquivo is kept there
  4. NHibernate MUST inform: Deleted object would be re-saved by cascade

Solution(s):

  • be sure that the GrupoModulo is never loaded (stays as proxy)
  • or (the way I use) Remove() the GrupoArquivo also from GrupoModulo.Arquivos
  • or do NOT use CASCADE mapping on GrupoArquivo side:

(do not use the cascade)

HasManyToMany(x => x.Arquivos)
    .Table(Const.TB_EMAIL_GRUPO_ARQUIVO)
    .ParentKeyColumns.Add(Const.ID_GRUPO, Const.ID_MODULO)
    .ChildKeyColumn(Const.ID_ARQUIVO)
    //.Cascade.SaveUpdate()
    // here
    .Cascade.None()
    .Not.LazyLoad();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!