I have a Word model for a dictionary/thesaurus:
public class Word
{
public virtual string Text { get; set; }
public virtual IList<Word> Synonyms { get; set; }
public virtual IList<Word> Antonyms { get; set; }
}
Where each Word has many Synonyms and Antonyms. Word has a mapping of:
public class WordMapping : ClassMap<Word>
{
Id(x => x.Text);
HasMany(x => x.Synonyms);
HasMany(x => x.Antonyms);
}
In my controller, I have a simple lookup method for a word:
public ActionResult Word(string word)
{
using (var session = MvcApplication.SessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var result = session.QueryOver<Word>()
.Where(w => w.Text == word)
.SingleOrDefault();
if(result == null)
RedirectToAction("InvalidWord");
ViewBag.Synonyms = result.Synonyms.Select(t => t.Text);
ViewBag.Antonyms = result.Antonyms.Select(t => t.Text);
return View(result);
}
}
}
When I print out the ViewBag collections in my view, they are both the same. They are some seemingly arbitrary selection of elements from both bags, but not the whole bags.
Update: Below is my code to commit the Words to the database, if that helps. When I print out words
after the commit, all the synonyms are correct.
List<Word> words;
...
using (var session = MvcApplication.SessionFactory.OpenSession())
{
// populate the database
using (var transaction = session.BeginTransaction())
{
foreach (var word in words)
session.Save(word);
transaction.Commit();
}
}
PrintWords(words);