3

I'm refactoring a fluent nHibernate mapping, and I can't seem to figure this one out. I want to remap a property with type List<decimal> to a child table, but using a single HasMany if possible.

Right now we have: Map(x => x.DecimalList); Which gives us a nice type of varbinary(8000)

In my attempts to move this to an ordered child table I've tried:

HasMany(x => x.DecimalList)
.Table("ParentTable_DecimalList")
.KeyColumn("Id")
.Element("Amount")
.KeyColumn("ParentId")
.Cascade.AllDeleteOrphan();

And this gives me the relationship, with two columns: ParentId and Amount. The only problem is that I also want to put an Order or Primary Key/ID column on the child table to ensure that we preserve the list's ordering no matter what.

Is there a way to add a strong int Primary Key column and / or an Order column without busting this out to a more complex child object/map?

Alex Moore
  • 3,415
  • 1
  • 23
  • 39

1 Answers1

4

Map it as a list

HasMany(x => x.DecimalList)
...
.AsList(x => x.WithColumn("ListPosition")

By default, it is mapped as a bag, which doesn't preserve the order.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • I've come to the conclusion that having a list like this without a child object container can be a bit of a code smell. Unless of course the list is truly a list of value objects that have no identity. I eventually refactored to a child object container layout, but it's still nice to know how to do it this way. Thanks again! – Alex Moore Nov 21 '11 at 14:11
  • Adding AsList(x => x.WithColumn("ListPosition") is creating null object in the list . Any help – Sahi Sep 15 '17 at 12:23
  • Ask a new question. Have complete but minimal code to reproduce the issue. – Stefan Steinegger Sep 19 '17 at 11:21