2012-10-06 2 views
0

나는이유창함 NHibernation 고유 한 문자열 매핑

이 있습니다이 테이블
아이디 (INT, PK, 인공 지능),
단어 (VARCHAR (25)) 나는 새로운 하나의 단어를 추가 할 사용자를 허용하고 있다고 내 데이터베이스에.
이미 존재하는 경우 Nhibernate가 새로운 단어를 건너 뛰도록합니다.
어떻게 유창하게 매핑 할 수 있습니까? 그게 가능하니?

감사

답변

1

매핑 :

Map(x => x.Word).Unique(); // just to make sure it is unique 

코드 :

void AddWords(ICollection<string> words) 
{ 
    var existingWords = session.QueryOver<UniqueWord>() 
     .WhereRestrictionOn(w => w.Word).In(words) 
     .Select(w => w.Word) 
     .List<string>(); 

    foreach(var word in words.Except(existingWords)) 
    { 
     session.Save(new UniqueWord(word)); 
    } 
    session.Flush(); 
} 
관련 문제