Nhibernate 组件映射 : Parent Object null in Value Object while querying from database

标签 nhibernate fluent-nhibernate nhibernate-mapping value-objects

我将我的值对象项映射为具有以下映射配置的组件

{
            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/

相关文章:

c# - 从头开始使用 NHibernate : tips for a new, 大型应用程序

c# - 在 MySQL、NHibernate 和 C# 中使用自动增量

c# - 如何映射NHibernate变量表引用?

nhibernate - 创建 T4 模板时,如何利用应用配置和其他文件资源?

nhibernate - Composite Id 和 Restrictions.IdEq 或与 Linq 中的比较未按预期工作

c# - 如何保存已存在于 NHibernate session 中的临时对象?

fluent-nhibernate - 如何使用 fluentnhibernate 只加载某些列?

c# - Fluent NHibernate - 应用自定义类型的约定

c# - 使用 NHibernate 将行插入数据库表而不在插入行后调用 scope_identity

nhibernate - Fluent NHibernate 中的多个 UniqueKey