views:

141

answers:

2

Hey all,

I am trying to map a legacy database here and I'm running into a problem. In my schema I have a concept of Modules and a concept of Variables. Each Module consists of one or more Variables and each of these Variables has properties specific to that Module. A Varable sits on a Relation.

Based on the classes below what is the best way to map ModuleVariable which looks, to me, like a many-to-many relationship with special properties??

Here are the classes:

public class Relation
{
    public virtual string RelationId
    {
        get;
        set;
    }
}

public class Variable
{
    public virtual string VariableId
    {
        get;
        set;
    }

    public virtual Relation RelationId
    {
        get;
        set;
    }
} 

public class Module
{
    public virtual string ModuleId
    {
        get;
        set;
    }
}

public class ModuleVariable
{       
    public virtual Module ModuleId
    {
        get;
        set;
    }

    public virtual Variable VariableId
    {
        get;
        set;
    }

    public virtual Relation RelationId
    {
        get;
        set;
    }

    public virtual Variable DownloadID
    {
        get;
        set;
    }

    public virtual Variable UploadID
    {
        get;
        set;
    }

    public string Repeatable
    {
        get;
        set;
    }
}
+1  A: 

To have a many-to-many relationship with extra properties on the relationship like that, you'll have to make ModuleVariable a domain entity and map it separately from Module and Variable.

Module and Variable will have a collection of ModuleVariable entities and ModuleVariable will have a many-to-one reference to the other two. Something like:

<!-- Module mapping -->
<bag name="ModuleVariables" inverse="true">
    <key column="Module_id" />
    <one-to-many class="ModuleVariable" />
</bag>

<!-- ModuleVariable mapping -->
<many-to-one name="Module" column="Module_id" />
Stuart Childs
A: 

The many-to-many only works on table with no extra properties. This because many-to-many represents a collection of items inside of other entity.

If your table ModuleVariable does not have others columns, you could use like that:

<bag name="Modules" table="MODULE_VARIABLE" cascade="save-update" lazy="true" >
  <key>
    <column name="Variable_Id" not-null="true"/>
  </key>
  <many-to-many class="Module">
    <column name="Module_Id" not-null="true"/>
  </many-to-many>
</bag>

And in your domain classes:

public IList Modules { get; set; }

And for use, you should Add the modules:

variable.Modules.Add(module);

Hopes to help.

Calebe Santos