NHibernate skips certain properties to update, possible?

≡放荡痞女 提交于 2019-12-25 08:57:42

问题


I defined my entities with bunch of columns and created mapping.

public class PurchaseRecord {
    public virtual int? Id {
        get;
        set;
    }

    public virtual DateTime? PurchasedDate {
        get;
        set;
    }

    public virtual string Comment {
        get;
        set;
    }

    public virtual IList<PurchaseRecordExtendedProperty> ExtendedPropertyValues {
        get;
        set;
    }

public class PurchaseRecordMap : ClassMap<PurchaseRecord> {
    public PurchaseRecordMap() {
        Table("PurchaseRecords");

        Id(x => x.Id, "RecordID").GeneratedBy.Identity();

        Map(x => x.PurchasedDate, "PurchaseDate").Not.Nullable();
        Map(x => x.Comment, "Comment");

        HasMany(x => x.ExtendedPropertyValues).KeyColumn("ExtendedPropertyID").Cascade.All();
    }

It works well in most of the cases, howerver in some certain situation I want to skip updating certain column (such as child collection ExtendedPropertyValues). When I create the PurchaseRecord object I don't even bother to load the data of ExtendedPropertyValues. But if the property is null NHibernate tries to delete the child records from database.

I know there are some scenario that the ExtendedPropertyValues will never be changed. For performance consideration I don't want to load the data I don't need, is there a way I can force NH to skip designated properties if I don't need to update?

Thanks for any suggestion.


回答1:


If you enable lazy loading, NHibernate will not try to load any child collections, they will be initialized to a proxy which will only load them if you access them. If you set the child collection to null, that is effectively telling NHibernate to delete all entries in that relationship (unless you mark the relationship as inverse).

NHibernate will not try to update the child collections unless they change (which setting it to null would do).

In summary, enable lazy-loading, and mark ExtendedPropertyValues as inverse, and it should not update it unless you change ExtendedPropertyValues, it also will not load ExtendedPropertyValues unless you access it.



来源:https://stackoverflow.com/questions/13059770/nhibernate-skips-certain-properties-to-update-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!