【发布时间】:2014-06-25 13:47:29
【问题描述】:
如何使用按代码映射来映射此字典:
public class User
{
public virtual Dictionary<Option, bool> Options { get; set; } }
}
数据库如下所示:
User UserOptions Option
--- --- ---
Id Id Id
UserID
OptionID
bool_column
我尝试了该映射(查看here):
Map(x => x.Dictionary,
m =>
{
m.Key(k => k.Column("UserID"));
m.Table("UserOptions");
},
k => k.ManyToMany(m =>
{
m.Column("OptionID");
}),
v => v.Element(m =>
{
m.Column("bool_column");
})
);
但是有一个错误:
An association from the table UserOptions refers to an unmapped class: System.Boolean
【问题讨论】:
-
是的,如果将
k.ManyToMany替换为k.Element则会出现另一个错误:SELECT useroption0_.UserID as UserID0_, useroption0_.bool_column as bool_column0_, useroption0_.idx as idx0_ FROM UsersOptions useroption0_ WHERE useroption0_.UserID=?我需要字典键中的实体,所以这里选择ManyToMany 是正确的(根据notherdev.blogspot.ru/2012/02/mapping-by-code-map.html)。
标签: nhibernate nhibernate-mapping-by-code