可选的多对一/引用会导致插入时外键违规
我目前有以下关系:ProductUom -> ProductImage
它们都有相同的主键:PROD_ID
和 UOM_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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以使用 LazyLoaded 属性吗?然后你可以使用类似的东西
另一个选项是:
但不能在这里测试它,我正在移动设备上写
can you use LazyLoaded Properties? Then you could use something like this
another option is:
can't test it here though, i'm writing on Mobile