使用 FluentNHibernate 在多个列上添加唯一约束
我有一个类,有一个主键和 2 个外键。外国组合必须是唯一的。我看不出有什么办法可以做到这一点(至少自从 SetAttribute 被弃用以来)。
James 通过 SetAttribute 谈到了这一点: 如何创建多列索引或 NHibernate 的唯一约束
I have a class that has a Primary Key as well as 2 foreign keys. Foreign combinations must be unique. I do not see a way to do this (at least since SetAttribute was deprecated).
James touched on this with SetAttribute:
How to create a Multi-Column Index or Unique Constraint with NHibernate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能对其他人有用,唯一约束的 FNH 映射是这样完成的:
mapping.References(x => x.FirstClass).UniqueKey("unique123");(x => x.SecondClass).UniqueKey("unique123");
mapping.References
另外,解释一下,这只是在db中构建约束,但开发者负责拦截重复插入尝试,否则将抛出
SqlException
,表示违反了 UNIQUE KEY 约束。来自 FNH 群组
This might be useful to someone else, FNH mapping of unique constraint is accomplished like this:
mapping.References<FirstClass>(x => x.FirstClass).UniqueKey("unique123");
mapping.References<SecondClass>(x => x.SecondClass).UniqueKey("unique123");
Further, it is explained that this only builds the constraint in the db, but that the developer is responsible to intercept duplicate insert attempts, otherwise an
SqlException
will be thrown saying an UNIQUE KEY constraint was violated.from the FNH group