流畅的 NHibernate 与 ManyToMany 和自定义链接表

发布于 2024-10-05 19:41:41 字数 1890 浏览 2 评论 0原文

我有以下架构,当我删除一侧的一个对象时,它似乎试图删除另一侧的对象。我对正确使用的 Cascade 选项有些困惑,而且我认为 Oren 对它们的简短描述没有用,所以请不要引用这些选项。

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 
}

在我的映射覆盖中,我有:

对于商店:

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

对于产品:

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

当我尝试删除具有关联的 StoreProducts 的商店时,似乎 NHIbernate 不仅尝试删除 StoreProducts,还尝试删除产品。

以下是我的约定:

 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();
}

我做错了什么?

谢谢!

ps:我不想用代码压垮人们,但如果需要的话可以发布更多内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2024-10-12 19:41:41

谢谢,CrazyDart - 我认为这是我尝试过但没有成功的事情之一。我最终做的是添加一个 StoreProducts 覆盖,如下所示:

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
}

似乎有效,但 QA 尚未尝试破坏它(-:

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

℉服软 2024-10-12 19:41:41

我猜你需要关闭 StoreProduct 上的级联。如果不设置它很难测试。我在 Store 和 Product 上看到级联,但在 StoreProduct 上将其关闭。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文