我将我的值对象项映射为具有以下映射配置的组件
{
Table("Product");
Not.LazyLoad();
Id(x => x.Id, "id");
Map(x => x.Number, "number");
Map(x => x.Name, "name");
Map(x => x.Description, "description");
Map(x => x.Status, "status");
HasMany(x => x.ItemLines).Component(
m =>
{
m.Map(x => x.ItemId, "itemid");
m.Map(x => x.Qty, "quantity");
}).Table("productitems").KeyColumn("itemid");
}
Class structure
public class ItemLine
{
public Product Product { get; set; }
public Guid ItemId { get; set; }
public int Qty { get; set; }
public ItemLine()
{
}
public ItemLine(Product product, Guid itemId, int qty)
{
Product = product;
ItemId = itemId;
Qty = qty;
}
//Equality and GetHashCode implemented.....
}
我能够将数据插入数据库,但在通过产品 ID 检索时,项目行中的产品属性为空。
我是否需要在映射中传递任何引用>
请帮忙
谢谢,
三月
最佳答案
好的。通过反复试验解决。
添加 m.ParentReference(x => x.Product);
{
Table("Product");
Not.LazyLoad();
Id(x => x.Id, "id");
Map(x => x.Number, "number");
Map(x => x.Name, "name");
Map(x => x.Description, "description");
Map(x => x.Status, "status");
HasMany(x => x.ItemLines).Component(
m =>
{
m.Map(x => x.ItemId, "itemid");
m.Map(x => x.Qty, "quantity");
m.ParentReference(x => x.Product);
}).Table("productitems").KeyColumn("itemid");
}
希望这对某人有帮助。
关于Nhibernate 组件映射 : Parent Object null in Value Object while querying from database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4064366/