I have a very basic question with nhibernate and repository pattern:
I have a repository (aggregate) and this is a parent. If I want to delete a child, should I have a single delete method that handles deletion of both the child and parent or should I have different methods?
How should I handle deleting a parent which has a child. Any sample code开发者_运维知识库 would be appreciated.
Are you using hbm mapping files or Fluent NHibernate to set up your mappings?
If you are using mapping files, one option would be to set the cascade attribute, e.g.:
...
<bag name="Children" lazy="true" cascade="delete" >
<key column="ParentId" />
<one-to-many class="Child" />
</bag>
...
cascade="delete" will result in deletes of the parent being cascaded to it's associated objects. Cascade can be specified as cascade="all|none|save-update|delete" so you can specify which operations should be cascaded from the parent to the associated objects.
Note .Cascade.All()
:
HasMany(item => item.ItemFeedbackItems)
.KeyColumn("ItemID")
.Cascade.All()
.LazyLoad()
.Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore);
精彩评论