I have one-to-many relationship between category and product. They are linked via categoryId field in the mapping. The category record will never get deleted, but the products children should get deleted. I send different payloads (based on logic) to my service via json. My product array I send one time is like this:
"products": [{Id=0, Name="p1"}]
it adds it fine and then 2nd time adds it fine:
"products": [{Id=111, Name="p1"},{Id=0, Name="p2"}]
Here the DB properly shows:
|-------|------|------------|
| Id | Name | CategoryId |
|-------|------|------------|
| 111 | p1 | 123 |
| 222 | p2 | 123 |
But if I am sending empty array of products, I'm expecting that both product records in DB will be deleted:
"products": []
but they don't.
Here is my NHibernate Fluent setup:
public CategoryMap()
{
Table("Category");
LazyLoad();
Id(q => q.Id).GeneratedBy.Identity().Column("Id");
HasMany(q => q.Products)
.KeyColumn("CategoryId")
.Cascade
.AllDeleteOrphan()
.Inverse();
}
and in my child:
public ProductMap()
{
Table("Product");
LazyLoad();
Id(x => x.Id).Column("Id").GeneratedBy.Identity();
Map(x => x.Name).Column("Name");
References(x => x.Category).Column("CategoryId").Not.Nullable();
}
The result here:
|-------|------|------------|
| Id | Name | CategoryId |
|-------|------|------------|
| 111 | p1 | 123 |
| 222 | p2 | 123 |
I have also tried making my QuestionId nullable in DB and in my child configuration:
References(x => x.Category).Column("CategoryId").Nullable();
The result here:
|-------|------|------------|
| Id | Name | CategoryId |
|-------|------|------------|
| 111 | p1 | NULL |
| 222 | p2 | NULL |
I want the children to be completely wiped out. What am I doing wrong? Please note that I am want to keep the parent record.