Fluent nHibernate,除键外的自动递增数字
我遇到过这样的情况:Notebook
存储Pages
。起初,我的结构是这样的。
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
{
// ..
}
并像这样映射它......
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
自动递增的东西?现在,我只是在添加新页面时使用 Notebook.Pages.Count
方法在我的 Service
中处理此问题。但流畅的 nHibernate 看起来相当聪明。有什么方法可以将其直接集成到映射中,以便每当我添加新页面时,它都会获取计数并确保数字合适?
I have a situation where say, a Notebook
stores Pages
. At first, I had it structured like this.
class Notebook
{
// ...
public virtual IList<Page> Pages
{
get;
set;
}
}
class Page
{
// ..
public virtual int Number
{
get;
set;
}
}
That worked okay. Each page had a page number. I would have the ability to re-order pages in my UI, and update the number. No problem. Then I discovered I could use a Dictionary, and simplify it more...
class Notebook
{
// ...
public virtual IDictionary<int, Page> Pages
{
get;
set;
}
}
class Page
{
// ..
}
And map it like so..
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");
}
}
Huzzah, everything was right with the world. This works great. But I'm curious...
Is there any way to bind Number
to something that will make sure to auto-increment per Notebook
? Right now, I just handle this in my Service
by using the Notebook.Pages.Count
method when I add a new page. But fluent nHibernate seems pretty smart. Is there any way I can integrate it right into the mapping, so that anytime I add a new page, it just gets the count and makes sure the number is appropriate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您确实希望将其排除在映射之外。您会将应用程序逻辑(下一页应该去哪里)与模型(我如何存储下一页)混合在一起。此外,如果您使此列自动递增,则必须在每个表的基础上执行此操作。除非您为每本书创建一个新表,否则您(据我所知)只能有 1 个一致递增的键(因为在 NHibernate 中使用自动增量列的本机生成器是通过数据库完成的)。
I think you really want to keep this out of the mapping. You'd be mixing the application logic (Where should the next page go) with the model (how am I storing the next page). Also if you make this column auto-increment you'd have to do it on a per table basis. Unless you are creating a new table for each book, you'd (AFAIK) only be able to have 1 consistently incrementing key (as the native generator which uses auto increment columns in NHibernate is done via the database).