Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何将n列主键映射到nHibernate_.net_Nhibernate_Fluent Nhibernate_Composite Primary Key - Fatal编程技术网

.net 如何将n列主键映射到nHibernate

.net 如何将n列主键映射到nHibernate,.net,nhibernate,fluent-nhibernate,composite-primary-key,.net,Nhibernate,Fluent Nhibernate,Composite Primary Key,我有一个表,有两列作为PK(复合主键) 如何将它们映射到hbm.xml中的“Id” <id name="A" /> 如何使用fluent nhibernate完成此操作?为nhibernate尝试此操作 <composite-id name="Key" class="KeyClass"> <key-many-to-one name="first" column="firstColumn" lazy="proxy" class="FirstClass"

我有一个表,有两列作为PK(复合主键)

如何将它们映射到
hbm.xml
中的“Id”

<id name="A" />  


如何使用fluent nhibernate完成此操作?

为nhibernate尝试此操作

<composite-id name="Key" class="KeyClass"> 
  <key-many-to-one name="first" column="firstColumn" lazy="proxy" class="FirstClass"/>
  <key-many-to-one name="second" column="secondColumn" lazy="proxy" class="SecondClass"/>
</composite-id>

而对于流畅的冬眠

<composite-id>  
  <key-property type="Int32" name="first" column="firstColumn" />  
  <key-property type="Int32" name="second" column="secondColumn" />  
</composite-id>  


希望对您有所帮助…:-)

我建议使用nHibernate中的复合主键

对于hbm.xml文件:

<hibernate-mapping>   
<class table="TableName" name="Namespace.ClassName, ClassName">
<composite-id>
    <key-property name="IdPropertyOne" column="ColumnOne" />
    <key-property name="IdPropertyTwo" column="ColumnTwo" />
</composite-id>
<property name="PropertyName" column="ColumnName" type="String"></property>
</class>
</hibernate-mapping>

NHibernate文档描述了如何使用和映射

您也可以使用

对于Fluent NHibernate:

public class ClassNameMap: ClassMap<ClassName>
{
    public ClassNameMap()
    {
        CompositeId().
            .KeyReference(x => x.A, "A")
            .KeyReference(x => x.B, "B");
    }
}
公共类ClassNameMap:ClassMap
{
公共类名称映射()
{
复合ID()。
.KeyReference(x=>x.A,“A”)
.KeyReference(x=>x.B,“B”);
}
}

引用不再有效。最好在这里复制答案,并附上参考资料,而不仅仅是使用参考资料。
public ClassName(){
    CompositeId().
        KeyProperty(x => x.IdPropertyOne,"ColumnOne")
        KeyProperty(x => x.IdPropertyTwo,"ColumnTwo")
}
public class ClassNameMap: ClassMap<ClassName>
{
    public ClassNameMap()
    {
        CompositeId().
            .KeyReference(x => x.A, "A")
            .KeyReference(x => x.B, "B");
    }
}