Composite_Id on child collection

We have a legacy database with the table structure like this:

Child model needs to apply composite id, we got this part figured out,

public ChildMap(){
            Table("Child_Tab");
            CompositeId()
                .KeyProperty(x => x.TableName, "Table_Name")                                            
                .KeyReference(x => x.Trainee, "Row_Id");     

but the difficult part is to set up HasMany on parent mapping to filter the data from child based on table_name column.

FluentNHibernate doesn’t allow mismatch composite id setting, which means, if child has composite key, the parent must has the same composite key.

Our final work around is, switch to HasManyToMany, but the middle table still using child table, then we can take advantage of ChildWhere method:

public ParentMap(){
            Table("Parent_Tab");
            Id("Parent_Id");
            HasManyToMany(x => x.Children)
                .Table("Child_Tab")
                .ParentKeyColumns.Add(new []{"Row_Id"})
                .ChildKeyColumns.Add(new[] { "Table_Name", "Row_Id" })
                .ChildWhere("Table_Name = 'Parent_Tab'")
                .Not.LazyLoad()
                ;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s