问题描述
我用一堆列定义了我的实体并创建了映射.
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();
}
在大多数情况下它工作良好,但是在某些情况下,我想跳过更新某些列(例如子集合ExtendedPropertyValues
).当我创建PurchaseRecord
对象时,我什至不必费心加载ExtendedPropertyValues
的数据.但是,如果该属性为null,NHibernate会尝试从数据库中删除子记录.
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.
我知道在某些情况下ExtendedPropertyValues
将永远不会更改.出于性能方面的考虑,我不想加载不需要的数据,是否有办法在不需要更新的情况下强制NH跳过指定的属性?
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?
感谢您的任何建议.
推荐答案
如果启用延迟加载,NHibernate将不会尝试加载任何子集合,它们将被初始化为仅在您访问它们时才加载的子代.如果将子集合设置为null,则有效地告诉NHibernate删除该关系中的所有条目(除非您将该关系标记为反向).
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不会尝试更新子集合,除非它们更改(将其设置为null会这样做).
NHibernate will not try to update the child collections unless they change (which setting it to null would do).
总而言之,启用延迟加载,并将ExtendedPropertyValues标记为反向,除非您更改ExtendedPropertyValues,否则不应更新它,除非您访问它,否则也不会加载ExtendedPropertyValues.
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.
这篇关于NHibernate跳过某些属性以进行更新,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!