流畅的nHibernate, I字典混乱
使用以下类..
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; }
}
我尝试使用 Fluent nHibernate 来映射它们。
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");
}
}
这是行不通的。我得到了例外。
Trait.Id getter 发生异常
现在,如果我将字典更改为如下所示。
public class Sheet
{
public virtual int Id { get; set; }
public virtual IDictionary<int, Trait> Influences { get; set; }
}
基本上将 int 作为键,将 Trait 作为值(不是我想要的),它确实有效。谁能解释一下这一点,以及我如何才能更恰当地重现我想要做的事情?
我认为原因是因为当我指定
HasManyToMany
时,我指定了集合的 Value 元素。然而这不是我的本意。
我想通过键的名称而不是值的名称来查找内容。虽然我意识到这在技术上是一个“可接受的”解决方案,但它有点违反了字典惯例。如果可能的话,我更愿意采用更惯例而不是解决方案的方法 - 并且我想更好地了解幕后实际发生的情况。
Using the following classes..
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; }
}
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.
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; }
}
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要的是 hbm 中的
映射将为您提供的内容。我相信您需要的是AsEntityMap
,而不是AsMap
。不过,看看这个帖子,其中讨论了关于地图流畅映射的重写(八月),这使得上述所有内容都过时了。编辑:对于
AsEntityMap
和其他选项,请查看 这个SO问题并回答另外,你说你在最新版本 - 最新的发布是1.1,但主干是2.0,并且非常不同,而且更进一步。如果您使用的不是 2.0+,您将看不到大多数帮助线程中发布的方法,如上面链接的方法。
What you want is what
<composite-index>
mapping in hbm would give you. I believe that instead ofAsMap
, you'd wantAsEntityMap
. 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.EDIT : For
AsEntityMap
, and other options, take a look at this SO question and answerAlso, 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.