nhibernate以三元关系保存字典 [英] nhibernate saving a dictionary in a ternary relation

查看:112
本文介绍了nhibernate以三元关系保存字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

班级:

class Track
{
    ...
    public virtual IDictionary<People, Role> PeopleRoles { get; set; }
}

映射:

<class name="Track" ...>
   ...
   <map name="PeopleRoles" table="track_people_role">
   <key column="track_id"/>
   <map-key-many-to-many column="people_id" class="People"/>
   <many-to-many column="role_id" class="Role"/>
  </map>
</class>

当我执行以下代码时,我可以看到track_people_role表中插入的行为47637,10,1

When I execute the code below I can see the row inserted in track_people_role table as 47637,10,1

    Person p = PersonManager.GetById(10);
    Role pr = RoleManager.GetById(1);
    Track t = TrackManager.GetById(47637);

    t.PeopleRoles.Add(p, pr);
    TrackManager.Save(t);

当我以不同的角色执行上一个操作后,出现已添加具有相同键的项"错误

after when I execute the previous with a different role I get "An item with the same key has already been added" error

    Person p = PersonManager.GetById(10);
    Role pr = RoleManager.GetById(2);
    Track t = TrackManager.GetById(47637);

    t.PeopleRoles.Add(p, pr);
    TrackManager.Save(t);

任何想法,我如何能设法插入第二行.

Any ideas how i can manage to insert the second row.


映射:

<class name="TrackPersonRole, App.Data" table="trackpeoplerole" lazy="true">
    <composite-id>
    <key-many-to-one name="Person" class="Person" column="people_id"/>
    <key-many-to-one name="Role" class="Role" column="role_id"/>
    </composite-id>
</class>

<class name="Track" ...>
 ...
 <bag name="TrackPeopleRoles">
     <key column="track_id"/>
     <one-to-many class="TrackPersonRole"/>
 </bag>
</class>


    TrackPersonRole tpr = new TrackPersonRole();
    tpr.Person = PersonManager.GetById(10);
    tpr.Role = RoleManager.GetById(2);
    entity.PeopleRoles.Add(tpr);
    TrackManager.SaveOrUpdate(entity);

生成以下更新语句,而不是插入内容.

generates the below update statement, instead of an insert.

UPDATE trackpeoplerole SET track_id = 47637 /* ?p0 */
WHERE people_id = 10 /* ?p1 */ AND role_id = 2 /* ?p2 */

推荐答案

错误消息正确:您正在向字典中添加重复的键.

The error message is correct: you are adding a duplicate key to a dictionary.

它甚至不是来自NHibernate-它是由Dictionary类生成的.

It does not even come from NHibernate - it's generated by the Dictionary class.

如果Track可以在多个Role中具有相同的Person,则必须将track_person_role表映射为实体:

If a Track can have the same Person in more than one Role, you'll have to map the track_person_role table as an entity:

class Track
{
    ...
    public virtual ICollection<TrackPersonRole> PeopleRoles { get; set; }
}

class TrackPeopleRole
{
    public virtual Person Person { get; set; }
    public virtual Role Role { get; set; }
}

映射:

<class name="Track" ...>
  ...
  <bag name="TrackPeopleRoles">
    <key column="track_id"/>
    <one-to-many class="TrackPersonRole"/>
  </map>
</class>

关于inversecascade的更多详细信息,但从此开始.

There are more details regarding inverse and cascade, but start with this.

这篇关于nhibernate以三元关系保存字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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