简体   繁体   English

映射NHibernate多对多

[英]Mapping NHibernate Many-to-Many

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?? 根据下面的类,映射ModuleVariable的最佳方法是什么,对我来说,这就像具有特殊属性的多对多关系一样?

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 . 要在这种关系上具有多对多关系并具有额外的属性,您必须使ModuleVariable成为域实体,并将其与ModuleVariable分开映射。

Module and Variable will have a collection of ModuleVariable entities and ModuleVariable will have a many-to-one reference to the other two. ModuleVariable将具有ModuleVariable实体的集合,而ModuleVariable将具有对其他两个的多对一引用。 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: 如果表ModuleVariable没有其他列,则可以这样使用:

<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. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM