views:

311

answers:

1

Hi,

I have a one to many relationship with the following config

 HasMany(x => x.Staff)  
       .Inverse()  
       .Cascade.All();

But I get a collection failed to initialize error.

Dont I have to specify the foreignkey here, examples I found do not????

How does it know which is the foreign key?

EDIT: Looking closer at the exception the sql is trying to use field Staff_id when I have said it is StaffID??

Malcolm

+5  A: 

Try

 HasMany(x => x.Staff)
   .KeyColumnNames.Add("StaffID")
   .Inverse()  
   .Cascade.All();

Staff_id is the auto configure default, although you can set what conventions auto-configure uses.

If you're mapping the collection to an IList<T>, you'll want to add AsBag() or NHibernate will complain about a missing "idx" column. If you want to lazy load the collection add .LazyLoad(). And I usually go with .Cascade.AllDeleteOrphan().

Jamie Ide

related questions