문제

NHibernate 및 SQL Server를 사용하여 클래스 상속 기기를 구현하려고합니다. 계층 구조 전략 (매핑 파일의 하나의 테이블 및 여러 서브 클래스 요소) 당 테이블에서 잘 작동했습니다.

그러나 서브 클래스 전략 (N + 1 테이블 및 n 하나의 매핑 파일에 결합 된 서브 클래스 요소) 당 테이블은 서브 클래스 테이블에 약간의 제약 조건을 넣고 싶기 때문에 내 경우에는 더 의미가 있습니다. 나는 Mas Nhibernate 문서에 따르면 테이블 사이에 주요 주요 연관성이 있어야합니다. FK 제약 조건을 서브 클래스 PK에서 마스터 클래스 PK로 넣으려고했지만 여전히 작동하지 않습니다. 다음과 같은 예외를 얻습니다.

nhibernate.exceptions.genericadoexception : 삽입 할 수 없음 : [jobflow.models.entities.contactperson] [SQL : ContactPersons (이메일, 회사, personID) 값 (?, ?,?)에 삽입 .sqlexection : Identity_insert가 OFF로 설정된 경우 'ContactPersons'표에서 ID 열에 대한 명시 적 값을 삽입 할 수 없습니다.

Identity_insert를 ON으로 설정하려고했지만 도움이되지 않은 것 같습니다 ...

도움이 되었습니까?

해결책

You need to declare the column in Persons as int identity, but in ContactPersons as just int - only one table needs to keep track of the identity value. Your foreign keys/primary keys are fine, this is the only issue you need to tackle.

NHibernate will insert into Persons, get the new identity value using SCOPE_IDENTITY(), and then use that value explicitly for the insert into ContactPersons.

다른 팁

I'm creating the schema with NH's SchemaExport. It creates a foreign key on the subclasses primary key:

alter table ContactPersons 
add constraint FK81E543E94B3FF9C3 
foreign key (Persons_FK) 
references Persons

Persons_FK is declared in the mapping file

<joined-subclass name="ContactPersons">
  <key column="Persons_FK"/>
  ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top