I'm using Fluent NHibernate to map a a class that has a collection of strings like this:
public class Foo {
public virtual ICollection<string> Strings { get; set; }
}
public class FooMap : ClassMap<Foo> {
public FooMap() { HasMany(f => f.Strings).Element("SomeColumnName"); }
}
When I write a unit test using the PersistenceSpecification
class included in the FNH package, it fails:
[TestMethod]
public void CanMapCollectionOfStrings() {
var someStrings = new List<string> { "Foo", "Bar", "Baz" };
new PersistenceSpecification<Foo>(CurrentSession)
.CheckList(x => x.Strings, someStrings) // MappingException
.VerifyTheMappings();
}
This test throws NHibernate.MappingException: No persister for: System.String
when calling CheckList()
. However, if I try to persist the object myself, it works just fine.
[TestMethod]
public void CanPersistCollectionOfStrings() {
var foo = new Foo {
Strings = new List<string> { "Foo", "Bar", "Baz" };
};
CurrentSession.Save(foo);
CurrentSession.Flush();
var savedFoo = CurrentSession.Linq<Foo>.First();
Assert.AreEqual(3, savedFoo.Strings.Count());
// Test passes
}
Why is the first unit test failing?
CheckComponentList method is probably the right way in this case:
var someStrings = new List<string> { "Foo", "Bar", "Baz" };
new PersistenceSpecification<Foo>(CurrentSession)
.CheckComponentList(x => x.Strings, someStrings)
.VerifyTheMappings();
This code work well for me (NH 3.1, FNH 1.2). Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With