This project is read-only.

How to have List of child elements in model

Topics: Writing modules
Oct 12, 2015 at 6:41 PM
I am creating a custom module that has various parent/child relations. I would like to have a list of child elements in my parent model so that a lazy load occurs if the list is referenced for display purposes. This works fine until I try to add/edit the parent record. I get a null exception error for the child elements. Is there any way to have nhibernate ignore the list when persisting the parent record?

Parent record model
    public class ProductType
        private IList<ProductField> _productFields;

        public virtual int Id { get; set; }
        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public virtual string Position { get; set; }
        public virtual IList<ProductField> ProductFields
            get { return _productFields ?? (_productFields = new List<ProductField>()); }
            set { _productFields = value; }
Oct 13, 2015 at 9:13 AM
This looks like a ViewModel? Not an NHibernate entity/record.
Oct 13, 2015 at 6:01 PM
This is the entity/record model for ProductType. It's just not a content item record.
Is it the IList of ProductField that makes you think otherwise? Are one-to-many relations not allowed in the model?
This model is being mapped to NHibernate and when I query the table via the repository (_repository.Table.Where(p => p.Id == id).FirstOrDefault();),
I'm getting the record I expect and a lazy load of ProductFields is occurring if/when I reference the list.
My problem is that when I try to save the ProductType record via the repository (_repository.Update(record)), NHibernate is trying to save the ProductField records as well which is what I am trying to avoid.
From what I've read about Fluent NHibernate mappings, this can be made to occur by defining ProductType as:
HasMany(x => x.ProductFields).Cascade.All();
I wanted to know if there is a way to override the mapping to not cascade the save or if there are attributes that I can decorate the IList field to achieve the same result or if what I am trying to do is simply not possible in Orchard.
Oct 14, 2015 at 8:55 AM
Ah I see, I was confused with your naming of your record model.

Maybe this will help you:
At the very bottom there is a chapter about NHibernate mapping configuration.