nHibernate 复合密钥类类型不匹配 [英] nHibernate Composite Key Class Type Mismatch

查看:27
本文介绍了nHibernate 复合密钥类类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复合键的旧表,该表的复合键映射到其他 3 个表,因为该表中有其他属性,因为它不是一个简单的映射表,所以我不能使用多对多集的解决方案映射这个.

I have a legacy table with composite keys that are mapped to 3 other tables, since this table have other attributes in it, since it is not a simple mapping table, I can't use the many-to-many set solution to map this.

以下是我所做的:

<class name="classA" table="A">
<composite-id name="ID" class="AKey">
  <key-many-to-one name="Id_one" class="One" column="Id_one" />
  <key-many-to-one name="Id_two" class="Two" column="Id_two" />
  <key-many-to-one name="Id_three" class="Three" column="Id_three" />
</composite-id>

AKey只是一个保存三个id的struct,Id_one、Id_two、Id_three在各自的类中都定义为int.

AKey is merely a struct that holds the three ids, and Id_one, Id_two, and Id_three are all defined as int in their respective class.

public struct Akey {
    public int Id_one { get; set; }
    public int Id_two { get; set; }
    public int Id_three { get; set; }
}

这编译得很好,但是当我尝试运行它时,它给了我一条错误消息:

This compiles fine, but when I try to run it, it gives me an error message:

NHibernate.QueryException : NHibernate.Criterion.SimpleExpression 中的类型不匹配:ID 预期类型 AKey,实际类型 System.Int32

NHibernate.QueryException : Type mismatch in NHibernate.Criterion.SimpleExpression: ID expected type AKey, actual type System.Int32

请告知我做错了什么或遗漏了什么.

Please advise on what I have done wrong or missed.

非常感谢!

推荐答案

如果你打算使用多对一的键,你可以把这个类:

If you are going to use key-many-to-one you would put the class:

public class Akey {
    public virtual One One {get; set;}
    public virtual Two Two {get; set;}
    public virtual Three Three {get; set;}
}

否则,如果您想要 Id,您只需将它们映射为 classA 的属性:

Otherwise if you want the Id you just map them as properties of classA:

 <composite-id>
     <key-property name="Id_one" column="Id_one" />
     <key-property name="Id_two" column="Id_two" />
     <key-property name="Id_three" column="Id_three" />
 </composite-id>

.

public class classA {
    public virtual int Id_one {get; set;}
    public virtual int Id_two {get; set;}
    public virtual int Id_three {get; set;}

    // ... rest of props ...
}

或者像你这样的复合体:

Or as a composite like you have:

 <composite-id name="ID" class="AKey">
     <key-property name="Id_one" column="Id_one" />
     <key-property name="Id_two" column="Id_two" />
     <key-property name="Id_three" column="Id_three" />
 </composite-id>

.

public class AKey {
    public virtual int Id_one {get; set;}
    public virtual int Id_two {get; set;}
    public virtual int Id_three {get; set;}
}

public class classA {
    public virtual AKey ID {get; set;}

    // ... rest of props ...
}

终于……

 <composite-id>
   <key-many-to-one name="Id_one" class="One" column="Id_one" />
   <key-many-to-one name="Id_two" class="Two" column="Id_two" />
   <key-many-to-one name="Id_three" class="Three" column="Id_three" />
 </composite-id>

.

public class classA {
    public virtual One One {get; set;}
    public virtual Two Two {get; set;}
    public virtual Three Three {get; set;}

    // ... rest of props ...
}

我不知道您是否可以使用结构体,因为我不擅长 C# 中的结构体.

Going to punt on whether you can use a struct because I'm not competent on them in C#.

这篇关于nHibernate 复合密钥类类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆