views:

21

answers:

1

I have a existing parent-child relationship I am trying to map in Fluent Nhibernate:

[RatingCollection] --> [Rating]

Rating Collection has:

  • ID (database generated ID)
  • Code
  • Name

Rating has:

  • ID (database generated id)
  • Rating Collection ID
  • Code
  • Name

I have been trying to figure out which permutation of HasMany makes sense here. What I have right now:

HasMany<Rating>(x => x.Ratings)
    .WithTableName("Rating")
    .KeyColumnNames.Add("RatingCollectionId")
    .Component(c => { c.Map(x => x.Code);
                      c.Map(x => x.Name); );

It works from a CRUD perspective but because it's a bag it ends up deleting the rating contents any time I try to do a simple update / insert to the Ratings property. What I want is an indexed collection but not using the database generated ID (which is in the six digit range right now).

Any thoughts on how I could get a zero-based indexed collection (so I can go entity.Ratings[0].Name = "foo") which would allow me to modify the collection without deleting/reinserting it all when persisting?

A: 

First of all, you're using an old version of Fluent NHibernate; WithTableName has been deprecated in favor of Table.

An IList can be accessed by index so a Bag should work. I'm not sure why you have mapped Rating as a Component or what the effect of that is. This is a standard collection mapping:

HasMany(x => x.Ratings).KeyColumn("RatingCollectionId")
    .Cascade.AllDeleteOrphan().Inverse()
    .AsBag().LazyLoad();
Jamie Ide
Good catch on the version - I thought it had been upgraded to 1.0 RTM but it appears not. Are you suggesting then that the mapping for the underlying type be moved to a separate mapping class rather than as a component? In the actual database the Description column name is "Rating_Description", I didn't bother to show it in the example because I thought it was irrelevant.
Colin Bowern
Yes, Rating should be mapped as a separate object.
Jamie Ide

related questions