开发者

Nhibernate Updating - Solutions for updating children on an entity?

Looking for some advice on how to update a collection on an entity. In a web app - we have a multiselect listbox allowing a user to assign and remove child entit开发者_开发问答ies from a list of available entities. The user would select the relevant children to associate with the parent. i.e. Product with multiple Categories it could belong to. Once the user is satisfied, they submit and we update the entities.

What is the preferred way to update(delete removed children, add new children) to the collection taking performance into account. I would not want to run several sql statements to fetch each child and add it to the parent.

Cheers

Mappings Attached:

 public class ParentMap : EntityMapping<Parent>
    {
        public ParentMap()
        {
            Map(x => x.Name);
            HasMany(x => x.Children)
                .Cascade.AllDeleteOrphan()
                .Access.LowerCaseField(Prefix.Underscore);
        }
    }

    public class ChildMap : EntityMapping<Child>
    {
        public ChildMap()
        {
            References(x => x.Parent);
        }
    }

 public abstract class EntityMapping<TEntity> : ClassMap<TEntity> where TEntity : EntityBase
    {
        protected EntityMapping()
        {
            Id(x => x.Id, "Id")
                .UnsavedValue("00000000-0000-0000-0000-000000000000")
                .GeneratedBy.GuidComb();
            OptimisticLock.Version();
            Version(entity => entity.Version);
        }
    }


Establish a cascade relation between parent and child entities and force it to act on all operations like update, delete, etc. You must define cascade behavior in you HBM mapping files. For more info: http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-mapping

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜