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;
}
}
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" />
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.