功能NHibernate,IDictionary的困惑 [英] Fluent nHibernate , IDictionary Confusion

查看:144
本文介绍了功能NHibernate,IDictionary的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的类..

public class Trait
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}
public class Sheet
{
    public virtual int Id { get; set; }
    public virtual IDictionary<Trait, int> Influences { get; set; }
}



我曾尝试使用功能NHibernate映射他们,因为这样的。

I have tried to map them using Fluent nHibernate, as such.

public class TraitMap : ClassMap<Trait>
{
    public TraitMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Table("Traits");
    }
}
public class SheetMap : ClassMap<Sheet>
{
    public SheetMap()
    {
        Id(x => x.Id);

        HasManyToMany<Trait>(x => x.Influences)
            .Schema("Sheets")
            .Table("Influences")
            .ParentKeyColumn("Trait")
            .ChildKeyColumn("Sheet")
            .AsMap<int>("Rating")
            .Cascade.All();

        Table("Sheets");
    }
}

这是行不通的。我得到的异常。

This does not work. I get the exception.

异常发生Trait.Id

Exception occurred getter of Trait.Id

现在,如果我改变了字典到这个样子。

Now, if I change the Dictionary up to look like this..

public class Sheet
{
    public virtual int Id { get; set; }
    public virtual IDictionary<int, Trait> Influences { get; set; }
}



基本上使得INT的关键,而特质的价值(不是我想要的),它的工作。谁能解释这一点,我怎么能更恰当地再现什么,我试图做?

Basically making the int the Key, and the Trait the value (not what I want), it does work. Can anyone explain this, and how I can more appropriately reproduce what I am trying to do?

我觉得理由是因为当我指定 HasManyToMany<&特质GT; 我指定集合元素的值。然而,这不是我的本意。

I think the reasoning is because when I specify HasManyToMany<Trait> I am specifying the Value element of the collection. However this is not my intention.

我想通过密钥,而不是值的名称的名称来查找东西。当我意识到这是一个技术上的可接受的解决方案,它那种违背了字典约定。我更愿意接手解决方案的方法更惯例,如果在所有可能的 - 我想更好地了解什么是引擎盖下实际发生的

I want to look things up by the Name of the Key, not the Name of the Value. While I realize this is technically an 'acceptable' solution, it kind of goes against the Dictionary convention. I'd prefer to take a more convention over solution approach, if at all possible - and I'd like to better understand what is actually going on under the hood.

推荐答案

你想要的是什么<复合指数> 在HBM映射会给你。我相信,而不是 AsMap ,你会想要 AsEntityMap 。但是,看看哪些谈到这个线程 。流利映射为地图重写(八月),使上述所有过时的

What you want is what <composite-index> mapping in hbm would give you. I believe that instead of AsMap, you'd want AsEntityMap. However, look at this thread which talks about a rewrite (in august) of the fluent mapping for Maps that makes all of the above obsolete.

修改:对于 AsEntityMap ,和其他选项,看看这太问题 < A HREF =http://stackoverflow.com/questions/2424180/fluentnhibernate-idictionaryentity-valueobject/2467417#2467417>并回答

EDIT : For AsEntityMap, and other options, take a look at this SO question and answer

  HasMany<Trait>(x => x.Influences)
    .KeyColumn("key column name")
    .AsEntityMap("referenced column name")
    .Entity("dict value", v=> v.Type<int>());



另外,你说你在最新版本 - 最新的发布为1.1,但树干是2.0,而且是非常不同的,更远一起。如果你不是在2.0+,你不会看到张贴在大多数的帮助线程象那个上面链接的方法。

Also, you say you're on the latest version - the latest release is 1.1, but trunk is 2.0, and is very different, and much further along. If you're not on 2.0+, you wouldn't see the methods posted in most of the help threads like the one linked above.

这篇关于功能NHibernate,IDictionary的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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