Nhibernate 组件映射:从数据库查询时值对象中的父对象为 null

发布于 2024-09-30 00:28:23 字数 1215 浏览 6 评论 0原文

我使用以下映射配置将值对象项映射为组件,

{
            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 检索时,项行中的产品属性为空。

我需要在映射中传递任何参考吗?

请帮忙

谢谢你,

马尔

I am mapping my value Object Item as component withthe folowing mapping configuration

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


}

I am able to insert data to Database but while retrieving back by Product Id, the Product property in Item Line is null.

Do I need to pass any References in Mapping>

Please help

Thank you,

Mar

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

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

发布评论

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

评论(1

轻许诺言 2024-10-07 00:28:23

好的。通过反复试验解决。

添加 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"); 
        } 

希望这对某人有帮助。

Ok. Solved by trial and error.

Add 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"); 
        } 

hope this helps someone.

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