NHibernate / FluentNHibernate只读复合id
本文关键字:复合 id 只读 FluentNHibernate NHibernate | 更新日期: 2023-09-27 18:03:49
我正在尝试映射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页面,但即使产生相同的错误。
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中写的任何访问类型映射。
您可以尝试使用CamelCaseField()
访问:
CompositeId().KeyReference(x => x.JournalEntry).Access.CamelCaseField()
.KeyReference(x => x.Account).Access.CamelCaseField();
查看有关映射策略的文档
我觉得很傻,没有看完对KeyReference方法的评论。
下面是compositeId: 的工作原理public class JournalEntryItemMap : ClassMap<JournalEntryItem>
{
public JournalEntryItemMap()
{
CompositeId()
.KeyReference(x => x.JournalEntry, map => map.Access.CamelCaseField(Prefix.Underscore));
}
}