1

I have the following schema, and when I delete one of the objects on one many side, it seems to be trying to delete the objects on the other many side. Am somewhat confused about the proper Cascade options to use, and I don't find Oren's brief description of them to be useful, so please don't quote those back.

public class Store {
public virtual IList<StoreProduct> StoreProducts { get; set; }
}

public class Product {
public virtual IList<StoreProduct> StoreProducts { get; set; }
}

public class StoreProduct {
public virtual Store Store { get; set; }
public virtual Product Product { get; set; }
public virtual Decimal Cost { get; set; } //this is why I have a custom linking class 
}

In my mapping overrides, I have:

For Store:

mapping.HasMany(x => x.StoreProducts).Cascade.AllDeleteOrphan().Inverse;

For Product:

mapping.HasMany(x => x.StoreProducts).Cascade.AllDeleteOrphan().Inverse;

When I try to delete a Store that has associated StoreProducts, it seems that NHIbernate tries to delete not only the StoreProducts, but the Products.

Here are my conventions:

 return c =>
                       {
                           c.Add<ForeignKeyConvention>();
                           c.Add<HasManyConvention>();
                           c.Add<HasManyToManyConvention>();
                           c.Add<ManyToManyTableNameConvention>();
                           c.Add<PrimaryKeyConvention>();
                           c.Add<ReferenceConvention>();
                           c.Add<EnumConvention>();
                           c.Add<TableNameConvention>();
                           c.Add<CascadeAll>();
                           c.Add(DefaultCascade.All());
                       };

HasManyConvention:

public void Apply(IOneToManyCollectionInstance instance)
{
    instance.Key.Column(instance.EntityType.Name + "Fk");
    instance.Cascade.AllDeleteOrphan();
    instance.Inverse();
}

What am I doing wrong?

Thanks!

p.s.: I don't want to overwhelm people w/code, but can post more if needed.

2 Answers 2

1

Thanks, CrazyDart - I think that is among the things I tried without success. What I ended up doing was adding a StoreProducts override that looks like this:

public class StoreProductOverride: IAutoMappingOverride<StoreProduct>
{
    #region IAutoMappingOverride<StoreProduct> Members

    public void Override(AutoMapping<IndicatorStrategy> mapping)
    {
        mapping.References(x => x.Store).ForeignKey("StoreFk").Cascade.SaveUpdate();
        mapping.References(x => x.Producty).ForeignKey("ProductFk").Cascade.SaveUpdate();
    }

    #endregion
}

Seems to work, but QA hasn't tried to break it yet (-:

0

You need to turn off the cascading on StoreProduct is my guess. Its hard to test without setting it up. I see the cascade on Store and Product, but turn it off on StoreProduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.