FluentNHibernate 一对多映射

发布于 2024-11-15 07:23:41 字数 1254 浏览 1 评论 0原文

我是 NHibernate 和 NHibernate 的新手。 FNH。以下是我想要实现的基本场景 有两个实体映射器类“Customer”和“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();

订单

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

我试图通过客户实体类中的以下属性获取特定客户的订单详细信息。

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

但我能够获取客户实体对象中的客户详细信息,但特定客户的“订单”始终为空。我没有进行任何更新操作,我只需要从数据库中获取数据。艾米,我做错了什么,或者我们怎样才能得到这个?

提前致谢

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

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

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

发布评论

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

评论(1

烟花易冷人易散 2024-11-22 07:23:41

在您的 Customer 类中:

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

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

您还可以添加公共方法来添加或删除订单中的项目

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

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