如何使用 Fluent-nhibernate 更改多列索引中的列顺序?
如何更改多列索引中的列顺序?
即:
mapping.References(x => x.SomeReference).SetAttribute("index", "IX_index");
mapping.Map(x => x.SomeField).SetAttribute("index", "IX_index");
产生以下架构:
create index IX_index on ApplicantProgramDatas (SomeField, SomeReferenceId)
但我想得到:
create index IX_index on ApplicantProgramDatas (SomeReferenceId, SomeField)
How to change the column order in a multi-column index?
I.e:
mapping.References(x => x.SomeReference).SetAttribute("index", "IX_index");
mapping.Map(x => x.SomeField).SetAttribute("index", "IX_index");
Produces the following Schema:
create index IX_index on ApplicantProgramDatas (SomeField, SomeReferenceId)
But I want to get:
create index IX_index on ApplicantProgramDatas (SomeReferenceId, SomeField)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用在 NHibernate 中定义索引。或 IAuxiliaryDatabaseObject。
在 hbm.xml 文件中:
NB可以位于同一 hbm.xml 文件中的类映射之前或之后,允许您将索引定义、触发器等与其应用的对象一起保存。
另一个选项是 NHibernate.Mapping.IAuxiliaryDatabaseObject:
鉴于您使用的是 Fluent NHibernate,IAuxiliaryDatabaseObject 可能会更适合您。只需在构建时公开您的配置,然后调用:
NB NHibernate.Mapping.SimpleAuxiliaryDatabaseObject 是 NHibernate 的一部分。如果您所需要做的只是为数据库对象提供创建/删除脚本,则不必自己编写它。
我快速浏览了 Fluent NHibernate 代码库,没有看到任何对 IAuxiliaryDatabaseObject 的直接支持。因此,这是公开您的配置对象并自己提供所有 IAuxiliaryDatabaseObjects 的问题。编写一些代码来扫描映射程序集以查找实现 IAuxiliaryDatabaseObject 的类型,然后对它们进行遍历以传递给 cfg.AddAuxiliaryDatabaseObject(obj) 并不是太困难。
您可以在 NHibernate 文档中找到有关辅助数据库对象的更多信息:
http ://nhibernate.info/doc/nh/en/index.html#mapping-database-object
You can define an index in NHibernate using <database-object> or IAuxiliaryDatabaseObject.
In a hbm.xml file:
N.B. <database-object> can go before or after a class mapping in the same hbm.xml file allowing you to keep your index definitions, triggers, etc. with the object to which they apply.
The other option is NHibernate.Mapping.IAuxiliaryDatabaseObject:
Given that you're using Fluent NHibernate, IAuxiliaryDatabaseObject will probably work better for you. Just expose your configuration when building it and then call:
N.B. NHibernate.Mapping.SimpleAuxiliaryDatabaseObject is part of NHibernate. You don't have to write it yourself if all you need to do is supply create/drop scripts for a database object.
I took a quick look in the Fluent NHibernate codebase and didn't see any direct support for IAuxiliaryDatabaseObject. So it is a matter of exposing your configuration object and supplying all your IAuxiliaryDatabaseObjects yourself. It wouldn't be too difficult to write some code that scan through your mapping assembly looking for types that implement IAuxiliaryDatabaseObject and then foreach'ing over them to pass to cfg.AddAuxiliaryDatabaseObject(obj).
You can find more information about auxiliary database objects in the NHibernate docs:
http://nhibernate.info/doc/nh/en/index.html#mapping-database-object
我想这是不可能的。 “FluentNHibernate.MappingModel.MappedMembers.AcceptVisitor()”在引用之前迭代属性:
因此,在多列索引中,您将始终在引用之前拥有属性。
顺便说一句,没有一个 ORM 能让你设置重要的索引选项,比如集群、过滤等。
I guess it is impossible. 'FluentNHibernate.MappingModel.MappedMembers.AcceptVisitor()' iterates properties before references:
As a result, you will always have properties before references in multi-column index.
BTW none of the ORMs will give you ability to set non-trivial index options like clustered, filtered, etc.
让我建议覆盖 SchemaExport。通过反射存在私有字段访问器,它需要完全信任模式。如果这种方式不能满足你的需求,可以考虑重写SchemaExport(相对轻量级)
Let me suggest to override SchemaExport. There is private field accessor via reflection, it requires full trust mode. If this approach does not suit your needs, consider rewriting SchemaExport (relatively light class)