0
votes

I have a [User] table/class and a [Company] table/class and there is a link-table [UserCompany] between them.

When editing a User, beside basic information people also could change that user's access Companies, so I do the map like this in UserMap.cs:

HasManyToMany(u => u
  .Companies)
  .Cascade.SaveUpdate()
  .Table("UserCompany")
  .ParentKeyColumn("UserId")
  .ChildKeyColumn("CompanyCode")
  .Not.LazyLoad();

Also in CompanyMap.cs I set inverse like this:

HasManyToMany(c => c.Users)
  .Inverse()
  .Table("UserCompany")
  .ParentKeyColumn("CompanyCode")
  .ChildKeyColumn("UserId");    

The problem now is: I could update [User] information/table, plus the linking data in [UserCompany] table. However, the Fluent Nhibernate also update the [Company] table which I don't need at all.

Is there any way I could let FN not update Company table?

1

1 Answers

2
votes

To stop cascading updates just remove the

.Cascade.SaveUpdate()

from your Many-to-many mapping.

It could be a bit confusing. In comparison with the cascading used on <list>s and <map>s. In that case, the update is done directly on the child table (parent has more children... child contains the ParentId - cascade is reasonable to do operation on the child record)

But here we are working with a pair table. The relation is stored there. And this table will be always managed by NHibernate (implicit cascading).

The setting .Cascade.SaveUpdate() goes to other end of the many-to-many relation. To company table in our case. It could be handy.. but you can omit that and get everything running as expected.