0

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?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Samuel
  • 631
  • 1
  • 10
  • 26

1 Answers1

2

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.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Hi Radim, thanks a lot for your explanation. The "implicit cascading" sounds quite powerful from Fluent NHibernate! :) – Samuel May 24 '13 at 05:31
  • Hi Radim, how about if I just want to "add/save" happen on [Conmpany]table as well, but no "Update"? – Samuel May 24 '13 at 05:53
  • I can hardly imagine, that it would really fit to your needs. Imagine that you count on the fact, that during creation of the user, Company is added (persisted). Then you just update the User... append new Company (not persisted)... The company won't be persisted... while the reference will (in fact it fail). So this scenario is most likely not possible, I'd say... but maybe you will find some ;) – Radim Köhler May 24 '13 at 06:05
  • OK, let's say SaveUpdate() must stay together, then is it possibile to skip some columns? (I still want to "read" them, just when saving/updating, do not insert those columns' value...) Sorry for asking you again... – Samuel May 24 '13 at 06:23
  • Please, try to take a look at the 5.1.3. class - http://nhforge.org/doc/nh/en/index.html#mapping-declaration-class. The point 7. There is the answer, how to update entity/class only for dirty/changed columns... That could help ;) – Radim Köhler May 24 '13 at 06:36