Nhibernate component mapping: parent object in value object during query from database

I map my Object Item value as a component to the next display 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 can insert data into the database, but returning by product ID, the Product property in the line item is null.

Do I need to pass any links to Mapping>

Please, help

Thank,

Mar

+3
source share
1 answer

Ok It is 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.

+5
source

Source: https://habr.com/ru/post/1772373/


All Articles