1

I have a class Unit (e.g. of a Company) which has many relationships with other Units. I cannot change the database structure. The db looks something like:

Units:
UnitID
UnitName
...

UnitToUnitRelationships:
FromUnitID
ToUnitID
RelationshipType

I need to restrict the RelationshipType when querying. I currently have a Unit class with a ISet ChildUnits and ISet ParentUnits (since under different relationship types you may have different parents). I have no need for the RelationshipType in my domain; I would only ever be concerned with one particular Type. If I were writing my own SQL, I would just make sure all my queries had RelationshipType='FOO' either in the ON or WHERE clause.

I'm using Fluent to do the mapping; my existing child mapping looks like this:

HasManyToMany(x => x.ChildUnits).AsSet().Cascade.None().
 Table("UnitToUnitRelationships").Not.LazyLoad().Fetch.Subselect().
 ParentKeyColumn("ToUnitID").ChildKeyColumn("FromUnitID").BatchSize(1000);

I think I may need to create a UnitToUnitRelationship class and mess with that, but if there's an easier way, I'd certainly prefer that, especially since as noted, I don't need the property in my domain.

1 Answer 1

1

You should look at .Where() and/or .ChildWhere() methods

  1. .Where() aplyed to reference table
  2. .ChildWhere() applyed to child table

So, I think that you should use .Where("RelationshipType='FOO'") to resolve your issue.

1
  • Yes, that did the trick! Very useful, did not know about that particular method on the mapping class. Sep 12, 2011 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.