开发者

Add a unique constraint on multiple columns with FluentNHibernate

开发者 https://www.devze.com 2023-01-27 20:40 出处:网络
I have a class that has a Primary Key as well as 2 foreign keys.Foreign combinationsmust be unique.I do not see a way to do this (at least since SetA开发者_运维问答ttribute was deprecated).

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 SetA开发者_运维问答ttribute was deprecated).

James touched on this with SetAttribute: How to create a Multi-Column Index or Unique Constraint with NHibernate


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

0

精彩评论

暂无评论...
验证码 换一张
取 消