I'm having difficulty mapping a database using Fluent NHibernate. The database I'm working on is brownfield and some of the relationships are poorly implemented.
The mapping I'm trying to do is described by the following tables:
+----------+ +----------+ +---------------+ +-------------------+
| ToolA | | ToolB | | ToolPurpose | | ToolInstruction |
+==========+ +==========+ +===============+ +===================+
| ToolA_Id | | ToolB_Id | | Purpose_Id | | ToolInstruction_Id|
| Name | | Name | | ToolA_Id | | Purpose_Id |
+----------+ +----------+ | ToolB_Id | | Instruction |
| Purpose | +-------------------+
+---------------+
What I'm looking to do is to map the table ToolA with the table ToolInstruction to get a class such as:
public class ToolA
{
public virtual int _ToolA_id { get; set; }
public virtual string _Name { get; set; }
public virtual string _Instruction { get; set; }
}
OR
public class ToolA
{
public virtual int _ToolA_id { get; set; }
public virtual string _Name { get; set; }
public virtual ToolInstruction _ToolInstruction{ get; set; }
}
public class ToolInstruction
{
public virtual string _Instruction { get; set; }
}
What I think is causing me difficulty is that the ToolPurpose table has a row for every ToolA and ToolB entry such that for a ToolA entry the ToolB_Id is null and vice versa.
For example:
+--------------------------------------------------+
|Purpose_Id | ToolA_Id | ToolB_Id | Purpose |
|===================================================
|1 | 1 | null | "purpose here" |
|2 | null | 1 | "purpose here" |
+--------------------------------------------------+
I've tried a few approaches to this and they have shown signs of working when using the PersistenceSpecification.VerifyTheMappings() method but not when I've tried pulling data from a backup database because I start running into null reference exceptions.
I've tried mapping ToolPurpose as a linker class and I've tried mapping it by using the Join method in the ToolA mapping class. I've also alternated between using HasOne and HasMany to see which was suitable.
I'm hoping someone can point me in the right direction by laying out how they would do it or if they see any problems in terms of how I'm approaching it.