开发者

FluentNHibernate One To Many Mapping

开发者 https://www.devze.com 2023-03-13 07:29 出处:网络
I am novice to NHibernate & FNH.Below is the basic scenario I am trying to achieve There are two entity mapper classes \"Customer\" & \"Order\"

I am novice to NHibernate & FNH. Below is the basic scenario I am trying to achieve There are two entity mapper classes "Customer" & "Order"

Customer

Table("CUSTOMERTEST");
            LazyLoad();
            Id(x => x.CustomerId).Column("CustomerId").GeneratedBy.Sequence("SYS");
            Map(x => x.CompanyName).Column("CompanyName");
            Map(x => x.ContactName).Column("ContactName");
            HasMany(x => x.Orders).KeyColumn("CustomerId").Cascade.All().Table("ORDERTEST").AsBag();

Order

Table("ORDERTEST");
            LazyLoad();
            Id(x => x.OrderId).Column("OrderId")开发者_运维技巧.GeneratedBy.Sequence("SYS");
            References(x => x.OrderedBy).Column("CustomerId");
            References(x => x.ProductDetails).Column("ProductId");
            Map(x => x.OrderDate).Column("OrderDate");
            Map(x => x.ShipToName).Column("ShipToName");

I am trying to fetch the order details for a particular customer though property as below in the customer entity class.

public virtual IList<Order> Orders {
            get { return new List<Order>(orders).AsReadOnly(); }
            protected set { orders = value; }
        }

But I am able to fetch the customer details in the customer entity object but he "Orders" for the particular customer is always empty. I am not doing any update operations, I just need to fetch the data from DB only. Amy I doing something wrong or how can we get this?

Thanks In Advance


In your Customer class:

public virtual IList<Order> Orders
        {
            get { return _orders; }
        }

private readonly IList<Order> _orders = new List<Order>();

also you can add public methods to add or remove items from Orders

0

精彩评论

暂无评论...
验证码 换一张
取 消