开发者

Fluent NHibernate with ManyToMany and Custom Link Table

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.AllDelet开发者_如何学PythoneOrphan();
    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.


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 (-:


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜