SexyMF
SexyMF

Reputation: 11155

Fluent NHibernation unique string mapping (Ignoring Duplicates)

I have this table
id (int,pk,ai),
word(varchar 25)

Lets say that I am allowing users to add new single words to my database.
I want Nhibernate to skip adding new word if it is already exists.
How can I map it in fluent? Is that doable?

Thanks

Upvotes: 0

Views: 117

Answers (1)

Firo
Firo

Reputation: 30813

mapping:

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

code:

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();
}

Upvotes: 1

Related Questions