0

Im working on a legacy database, that cant be changed.
I have a table (Table1) with a many-to-one relationship with another table (Table2) which has a composite key.
If everything was strait forward the mapping for table1 could look like this:

<class name="Table1" table="Table1">
  <id name="Id" column="I_ID"></id>
  <many-to-one name="Table2" class="Table2">
    <column name="I_TABLE2_ID1"></column>
    <column name="I_TABLE2_ID2"></column>
  </many-to-one>
</class>

My problem is that I dont have no I_TABLE2_ID2 column in Table1.
So I would like to use a default value instead. Is there any way to accomplish this in the mapping file?

EDIT1:
The following mapping seem to work:

<class name="Table1" table="Table1">
  <id name="Id" column="I_ID"></id>
  <property name="Table2Id1" column="I_TABLE2_ID1"></property>
  <bag name="Table2" where="I_ID2 = 12">
    <key column="I_ID1" property-ref="Table2Id1"></key>
    <one-to-many class="Table2"/>
  </bag>
</class>

Also I had to change the type of the Table2 property to an IList, but guess I could use a private property to make the entity look better...

Vegard
  • 35
  • 1
  • 8

1 Answers1

0

Maybe you should specify this relationship as a one-to-many instead and specify a where clause on the collection.

http://www.nhforge.org/doc/nh/en/index.html#collections-mapping

Cole W
  • 15,123
  • 6
  • 51
  • 85
  • Thanks for the reply! Was it something like this you ment? I guess its also possible to skip the relationship in the mapping and add it in the linq query instead? – Vegard Nov 01 '11 at 13:56
  • Yes this is what I meant. You can bypass the where clause in the bag if you want and filter the collections manually in the code. – Cole W Nov 01 '11 at 14:09