我试图映射2个实体。
JournalEntry.cs
class JournalEntry
{
private readonly IList<JournalEntryItem> _items;
public JournalEntry()
{
_items = new List<JournalEntryItem>();
}
public IList<JournalEntryItem> Items
{
get { return _items; }
}
}
JournalEntryItem.cs
class JournalEntryItem
{
private readonly JournalEntry _journalEntry;
private readonly Account _account;
public JournalEntryItem(JournalEntry journalEntry, Account account)
{
_journalEntry = journalEntry;
_account = account;
}
public JournalEntry JournalEntry
{
get { return _journalEntry; }
}
public Account Account
{
get { return _account; }
}
}
JournalEntryItemMap.cs
class JournalEntryItemMap : ClassMap<JournalEntryItem>
{
public JournalEntryItem()
{
CompositeId()
.KeyReference(x => x.JournalEntry).Access.Field()
.KeyReference(x => x.Account).Access.Field();
// Tried this too, obviously won't work anyway
// CompositeId()
// .KeyReference(x => x.JournalEntry).ReadOnly()
// .KeyReference(x => x.Account).ReadOnly();
}
}
此映射引发异常:
Could not find a setter for property 'JournalEntry' in class Macsauto.Domain.Accounting.Entities.JournalEntryItem'
我所知道的唯一解决方法是制作受保护的财产。
还有其他解决方法吗?
更新
试过这个但仍然无法正常工作:
JournalEntryItemMap.cs
class JournalEntryItemMap : ClassMap<JournalEntryItem>
{
public JournalEntryItem()
{
CompositeId()
.KeyReference(Reveal.Member<JournalEntryItem, JournalEntry>("_journalEntry")).Access.Field()
.KeyReference(x => x.Account).Access.Field();
}
}
更新 我尝试了FluentNHibernate wiki page的方法,但即使这样也会产生同样的错误。
JournalEntryItem.cs
class JournalEntryItem
{
// Removed the _ prefix just to make it even more similar with
// those on FluentNHibernate wiki page
private readonly JournalEntry journalEntry;
public JournalEntryItem(JournalEntry journalEntry)
{
this.journalEntry = journalEntry;
}
public JournalEntry JournalEntry
{
get { return journalEntry; }
}
}
JournalEntryItemMap.cs
class JournalEntryItemMap : ClassMap<JournalEntryItem>
{
public JournalEntryItem()
{
CompositeId().KeyReference(x => x.JournalEntry).Access.CamelCaseField();
// Other tried combinations, all throws same no setter error
// CompositeId().KeyReference(x => x.JournalEntry).Access.ReadOnlyPropertyThroughCamelCaseField();
// CompositeId().KeyReference(x => x.JournalEntry).Access.LowerCaseField();
}
}
现在我好奇,是否与版本问题有关?
我使用NHibernate 3.3.1.4000和FluentNHibernate 1.4.0.0。
FluentNHibernate是否跟上NHibernate的步伐?它几乎像NHibernate一样忽略了我在FluentNHibernate中编写的任何访问类型映射。
答案 0 :(得分:1)
您可以尝试使用CamelCaseField()
访问权限:
CompositeId().KeyReference(x => x.JournalEntry).Access.CamelCaseField()
.KeyReference(x => x.Account).Access.CamelCaseField();
查看关于映射策略的documentation。
答案 1 :(得分:1)
对于不通读KeyReference方法的评论感到愚蠢。
以下是它对compositeId的作用:
public class JournalEntryItemMap : ClassMap<JournalEntryItem>
{
public JournalEntryItemMap()
{
CompositeId()
.KeyReference(x => x.JournalEntry, map => map.Access.CamelCaseField(Prefix.Underscore));
}
}
请注意,访问设置位于KeyReference方法中。