Nhibernate cannot add + save items to my one to many collection
I seem to have major problems getting a one to many relationship to work in nhibernate
and my classes are
public class Kitten
{
public virtual int? Id { get; set; }
public virtual String Name { get; set; }
}
public class Product
{
public Product()
{
Kittehs = new List<Kitten>();
}
public virtual int? ProductId { get; set; }
public virtual string ProductName { get; set; }
public virtual UnitOfMeasure UOM { get; set; }
public virtual IList<Kitten> Kittehs { get; set; }
}
And here's my a snippet program:
First:
public class ProductRepository
// snip
public void Save(Product saveObj)
{
using (var session = GetSession())
{
开发者_Go百科 using(var trans = session.BeginTransaction())
{
session.SaveOrUpdate(saveObj);
trans.Commit();
}
}
}
and then the calling code:
var pNew = new Product { ProductName = "Canned Salmon" ,UOM = uomBottle};
var tiddles = new Kitten() {Name = "Tiddles"};
pNew.Kittehs.Add(tiddles);
productRepository.Save(pNew); //ERROR here
When I call productRepository.Save
I get
{"The type NHibernate.Collection.Generic.PersistentGenericSet1[Acme.Model.Kitten] can not be assigned to a property of type System.Collections.Generic.IList1[Acme.Model.Kitten] setter of Acme.Model.Product.Kittehs"}
so I'm assuming the mapping is wrong somehow but I can't see where.
Well... you have a Set and then a List for Acme.Model.Kitten
... Try to look at your mapping files.
You're using public virtual IList<Kitten> Kittehs { get; set; }
in your Product class but inside your mapping this same property is mapped to a Set
.
Bag maps to IList
精彩评论