流畅的NHibernate多对多在链接表上创建聚集索引
我有两个处于多对多关系的实体。
public class SecurityGroupMappingOverride : IAutoMappingOverride<SecurityGroup>
{
public void Override(AutoMapping<SecurityGroup> mapping)
{
mapping.HasManyToMany(x => x.Actions).ParentKeyColumn("securityGroupId").ChildKeyColumn("actionId").
LazyLoad().Table("ActionGroups");
mapping.HasManyToMany(x => x.Members).ParentKeyColumn("securityGroupId").ChildKeyColumn("userId").
LazyLoad().Inverse().Table("UserGroups");
mapping.Map(x => x.Name).Length(64);
}
}
因此,我想在表 UserGroups 的两列(userId、securityGroupId)上创建聚集索引。
或者只是在这两列上的用户组上创建主键,因为同时不能是两个相同的链接。
谢谢
I have two entities in a many-to-many relationship.
public class SecurityGroupMappingOverride : IAutoMappingOverride<SecurityGroup>
{
public void Override(AutoMapping<SecurityGroup> mapping)
{
mapping.HasManyToMany(x => x.Actions).ParentKeyColumn("securityGroupId").ChildKeyColumn("actionId").
LazyLoad().Table("ActionGroups");
mapping.HasManyToMany(x => x.Members).ParentKeyColumn("securityGroupId").ChildKeyColumn("userId").
LazyLoad().Inverse().Table("UserGroups");
mapping.Map(x => x.Name).Length(64);
}
}
So I want to create an clustered index on both columns (userId, securityGroupId) from table UserGroups.
Or just to create a Primary Key on UserGroups on them both columns, as at same time could not be two same links.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您希望 NHibernate 的 SchemaExport 工具为您生成这些索引/键。
对于多对多包(FluentNHibernate 的默认集合类型),SchemaExport 生成:
对于多对多集 strong>,它会生成:
...因此只需将
.AsSet()
添加到您的映射中即可。如果您考虑一下手袋和套装是什么,这确实是有道理的。套装中的元素应该是唯一的,而包则没有唯一性要求。
I assume you're wanting NHibernate's
SchemaExport
tool to generate these indexes/keys for you.For many-to-many bags (FluentNHibernate's default collection type), SchemaExport generates:
For many-to-many sets, it generates:
... so just add
.AsSet()
to your mapping.It makes sense, really, if you think about what bags and sets are. The elements of a set are supposed to be unique, whereas bags don't have a uniqueness requirement.
您需要将
IAutoMappingOverride
放在代码顶部。而且您只需要一个mapping.HasManyToMany
。如果您想要双向关系,则需要通过创建一个MemberMappingOverride
类来执行相反的映射,并在那里标记为反向。希望这有帮助。
You need
IAutoMappingOverride<Member>
instead at the top of your code. And you only need onemapping.HasManyToMany
. If you want a bidirectional relationship, you need to do the opposite mapping by making aMemberMappingOverride
class and there you mark inverse.Hope this helps.