NHibernate通过代码映射:如何映射IDictionary? [英] NHibernate mapping by code: How to map IDictionary?

查看:77
本文介绍了NHibernate通过代码映射:如何映射IDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用按代码映射来映射这些实体:

How can I map these Entities using mapping-by-code:

public class Foo
{
    public virtual IDictionary<Bar, string> Bars { get; set; }
}

public class Bar
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

我找到了此线程,但是它没有映射实体,仅是简单类型.我尝试了许多映射,包括自动映射:

I found this thread, but it does not map an entity, only simple types. I tried many mappings, including automapping:

Map(x => x.Bars,
    m =>
    {
        m.Key(k => k.NotNullable(true));
        m.Cascade(Cascade.All);
    },

但是其中大多数会引发以下两个错误:

But most of them throw these two errors:

  1. 外键(栏[idx])必须具有与引用的主键(栏[FooId,idx])相同的列数.
  2. 表FoosToStrings中的关联引用未映射的类:System.String.

任何帮助将不胜感激.谢谢. :)

Any help will be highly appreciated. Thanks. :)

推荐答案

我认为这应该可行

Map(x => x.Bars,
    entryMap => entryMap.Key(k => k.Column("foo_id")),
    keymap => keymap.ManyToMany(m => m.Column("bar_Id")),
    elementMap => elementMap.Element(m => m.Column("value")));

这篇关于NHibernate通过代码映射:如何映射IDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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