我有一种情况,说一个Notebook
存储Pages
。起初,我有这样的结构。流利的nHibernate,自动递增数字以外的一个键
class Notebook
{
// ...
public virtual IList<Page> Pages
{
get;
set;
}
}
class Page
{
// ..
public virtual int Number
{
get;
set;
}
}
工作正常。每个页面都有一个页码。我将有能力在我的用户界面中重新排序页面,并更新编号。没问题。后来我发现我可以用一个字典,更简化它...
class Notebook
{
// ...
public virtual IDictionary<int, Page> Pages
{
get;
set;
}
}
class Page
{
// ..
}
和MAP它像这样..
public class NotebookMap : ClassMap<Notebook>
{
public NotebookMap()
{
// Specify the Entity Primary Key
Id(x => x.Id);
// one notebook will have a list of pages with page numbers
HasMany<int, Page>(x => x.Pages)
.DictionaryKey<int>("Number")
.DictionaryValue<Page>("Page")
.ForeignKey("Notebook")
.Schema("Notebook").Table("Pages");
Schema(Schemas.Collections); Table("Notebooks");
}
}
好哇,一切是正确的与世界同步。这很好。但我很好奇......
有没有什么办法可以将Number
绑定到什么东西上,以确保每个Notebook
自动递增?现在,我只需在我的Service
中使用Notebook.Pages.Count
方法处理此问题,当我添加一个新页面时。但流利的nHibernate看起来很聪明。有没有什么方法可以将它直接集成到映射中,以便我随时添加一个新页面,它只是获得计数并确保编号是合适的?
但是,如果我在应用程序逻辑中保留这个,那么继续使用这种方法应该没问题?我不会将它保留在应用程序逻辑中,但是在我的书中,任何可以减少出错机会的东西都是很好的。正如我的例子所示,我有点担心我实际上必须使用``而不是``。它只在'Type'是第一个时才有效 - 这对我来说毫无意义。 –
Ciel
2011-02-01 20:12:15