I am learning nHibernate and I was trying one to many mapping. Here are the two tables Product and Product Type.
namespace NHibernateSample.Models
{
public c
The first issue seems to be related to wrong setting of the .hbm.xml file.. which always must have (see e.g. MappingException: No persister for - NHibernate - Persisting an Adapter)
(see configuration)The second question (in comment)
Here I insert a product. I want to insert some ProductType to the database like
prod.ProductTypes = new
.... How can I do thatProduct prod = new Product(); prod.Name = "Q3"; prod.Category = "Audi"; prod.Discontinued = false; session.Save(prod); ...
solution is to adjust the mapping of the collection to be using cascading:
(what are all these settings on the
- check here)
I would adjust this POCO defintion
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
//public virtual IList ProductTypes { get; set; }
IList _productTypes;
public virtual IList ProductTypes
{
get { return _productTypes ?? (_productTypes = new List()); }
set { _productTypes = value; }
}
}
(that is just to be sure that list is initiated either by NHibernate on load or by us in other cases)
and then we just have to assign both sides
// product
Product prod = new Product();
prod.Name = "Q3"; prod.Category = "Audi";
prod.Discontinued = false;
// product type
ProductType productType = new ProudctType();
...
// both sides assigned
prod.ProductTypes.Add(productType);
productType.Product = prod;
session.Save(prod);
And we also should adjust the mapping to be readonly for value type property
To get more details I would not miss this: