可选的多对一/引用会导致插入时外键违规

发布于 2024-12-11 11:54:20 字数 1139 浏览 0 评论 0原文

我目前有以下关系:ProductUom -> ProductImage

它们都有相同的主键:PROD_IDUOM_TYPE

我将它们映射如下:

public ProductUomMap()
{
    Table("PROD_UOM");

    CompositeId()
        .KeyReference(x => x.Product, "PROD_ID")
        .KeyProperty(x => x.UomType, "UOM_TYPE");

    References(x => x.Image)
        .Columns(new string[] { "PROD_ID", "UOM_TYPE" })
        .Not.Update()
        .Not.Insert()
        .NotFound.Ignore()
        .Cascade.All();
}

public ProductImageMap()
{
    Table("PROD_UOM_IMAGE");

    CompositeId()
        .KeyReference(x => x.ProductUom, new string[] {"PROD_ID", "UOM_TYPE"});

    Map(x => x.Image, "PROD_IMAGE").Length(2147483647);
}

每当我创建 ProductUom 对象时具有 ProductImage 时,它会尝试首先插入 ProductImage,这会导致外键违规。我发誓这曾经与我拥有的映射一起工作,但现在不行了。

我需要将 ProductImage 作为 Reference (多对一),因为这里的关系是可选的,并且我希望能够延迟加载产品图像。如果我使用 HasOne (一对一)映射,插入确实可以正常工作,但是当我这样做时我无法延迟加载,并且查询 ProductUom 似乎会导致问题。

我在这里缺少什么吗?如何修改这个映射以获得我想要的?

I currently have the following relationship: ProductUom -> ProductImage

They both have the same primary keys: PROD_ID and UOM_TYPE

I have them mapped like this:

public ProductUomMap()
{
    Table("PROD_UOM");

    CompositeId()
        .KeyReference(x => x.Product, "PROD_ID")
        .KeyProperty(x => x.UomType, "UOM_TYPE");

    References(x => x.Image)
        .Columns(new string[] { "PROD_ID", "UOM_TYPE" })
        .Not.Update()
        .Not.Insert()
        .NotFound.Ignore()
        .Cascade.All();
}

public ProductImageMap()
{
    Table("PROD_UOM_IMAGE");

    CompositeId()
        .KeyReference(x => x.ProductUom, new string[] {"PROD_ID", "UOM_TYPE"});

    Map(x => x.Image, "PROD_IMAGE").Length(2147483647);
}

Whenever I create a ProductUom object that has a ProductImage it tries to insert the ProductImage first which results in a foreign key violation. I swear this was working at one time with the mapping that I have but it doesn't now.

I need the ProductImage to be a Reference (many-to-one) because the relationship here is optional and I want to be able to lazy load product images. The inserts do work correctly if I use a HasOne (one-to-one) mapping but the I cannot lazy load when I do this and querying a ProductUom seems to cause issues.

Is there something that I'm missing here? How can this mapping be modified to get what I want?

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

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

发布评论

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

评论(1

歌入人心 2024-12-18 11:54:20

你可以使用 LazyLoaded 属性吗?然后你可以使用类似的东西

Join("PROD_UOM_IMAGE", join =>
{
    join.KeyColumn("PROD_ID", "UOM_TYPE");
    join.Optional();
    join.Map(x => x.Image, "PROD_IMAGE").Length(2147483647).LazyLoad();
}

另一个选项是:

Id().GeneratedBy.Foreign(x => x.ProductUom);

但不能在这里测试它,我正在移动设备上写

can you use LazyLoaded Properties? Then you could use something like this

Join("PROD_UOM_IMAGE", join =>
{
    join.KeyColumn("PROD_ID", "UOM_TYPE");
    join.Optional();
    join.Map(x => x.Image, "PROD_IMAGE").Length(2147483647).LazyLoad();
}

another option is:

Id().GeneratedBy.Foreign(x => x.ProductUom);

can't test it here though, i'm writing on Mobile

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