且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

流利的NHibernate:如何在多对多连接表上创建聚集索引?

更新时间:2022-12-30 20:49:43

我不知道Fluent是否直接支持它(如果没有,只需包含xml)您可以使用辅助数据库对象

 < nhibernate-mapping> 
< database-object>
< create>在UsersInRole(UserId,RoleId)上创建聚集索引ix< / create>
< drop> drop index UsersInRole.ix< / drop>
< / database-object>
< / nhibernate-mapping>


In order to use my Fluent NHibernate mappings on SQL Azure, I need to have a clustered index on every table. The default heap tables that Fluent NHibernate creates for many-to-many joins obviously don't do this as they don't have primary keys.

I want to be able to tell one side of the relationship to create a clustered index for its join table, but I'm not sure how. Here's what my mappings look like:

 public class UserMap : ClassMap<User>{
    public UserMap()
    {
        Table("Users");
        Id(x => x.UserId).GeneratedBy.Identity().Column("UserId");
        Map(x => x.UserName).Unique().Not.Nullable().Length(DataConstants.UserNameLength).Column("UserName");
        Map(x => x.Email).Unique().Not.Nullable().Length(DataConstants.EmailAddressLength).Column("Email");
        Map(x => x.Password).Not.Nullable().Length(DataConstants.PasswordHashLength).Column("Password");
        HasMany(x => x.Clicks).Cascade.AllDeleteOrphan();
        HasManyToMany(x => x.Roles).Cascade.SaveUpdate().Table("UsersInRole").ParentKeyColumn("UserId").
            ChildKeyColumn("RoleId");

    }
}

Please let me know if you need any more information!

I don't know if Fluent supports it directly (if not, just include the xml), but you can do it with Auxiliary Database Objects

<nhibernate-mapping>
  <database-object>
    <create>create clustered index ix on UsersInRole(UserId, RoleId)</create>
    <drop>drop index UsersInRole.ix</drop>
  </database-object>
</nhibernate-mapping>