在流利的nhibernate私人收集映射 [英] Private collection mapping in fluent nhibernate
问题描述
如何映射:
public class Customer
{
private IList< Order> _命令;
public IEnumerable< Order>
GetAllOrders()
{
return _orders;
$ b 在项目页面上有一些样例,但没有一个是关于这个情况。
有这样的例子:
$ p $ // model
public class Account
{
private IList< Customer> customers = new List< Customer>();
公共IList< Customer>客户
{
获得{return customers; }
$ b //映射
HasMany(x => x.Customers)
.Access.AsCamelCaseField();
但是,它假设Account具有公共字段Customers,并且该场景与我的不同。我尝试了一些可能的选择,但没有任何作用:
$ p $ HasMany(x => Reveal.Propertie(_ orders))
私有字段在简单的属性映射中工作正常,但是集合映射
完全不同。任何想法?感谢解决方案最简单的解决方案是将您的集合公开为公共属性Orders而不是GetAllOrders()方法。然后你的映射是
$ p $ HasMany(x => x.Orders)
.Access.AsCamelCaseField(Prefix.Underscore );
而你的班级是 编辑添加:刚做完这个,正确的答案是: 集合必须作为保护虚拟属性以允许代理: How can I map this: On the project page are some samples but none is about this situation.
There is this sample: But it assumes that Account has public field Customers and that scenario is different as mine. I tried some possible options but none works: Private fields works fine in simple property mapping but collection mapping
is quite different. Any idea? Thanks The easiest solution is to expose your collection as a public property Orders instead of the GetAllOrders() method. Then your mapping is and your class is If that doesn't work for you, it is possible to map private properties using Fluent NHibernate's Reveal mapping. Edited to add: Having just done this, the correct answer is: The collection must be exposed as a protected virtual property to allow proxying: This answer put me on the right track. 这篇关于在流利的nhibernate私人收集映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre > public class Customer
{
private IList< Order> _orders =新列表< Order>();
public IEnumerable< Order>订单
{
get {return _orders; }
$ b $ p $如果这不适合你,可以使用Fluent NHibernate的Reveal映射映射私有属性。
HasMany< Order>( Reveal.Property< Customer>(_ orders))等等。
受保护的虚拟IList< Order> _orders {get;组; }
public class Customer
{
private IList<Order> _orders;
public IEnumerable<Order>
GetAllOrders()
{
return _orders;
}
}
// model
public class Account
{
private IList<Customer> customers = new List<Customer>();
public IList<Customer> Customers
{
get { return customers; }
}
}
// mapping
HasMany(x => x.Customers)
.Access.AsCamelCaseField();
HasMany(x => Reveal.Propertie("_orders"))
HasMany(x => x.Orders)
.Access.AsCamelCaseField(Prefix.Underscore);
public class Customer
{
private IList<Order> _orders = new List<Order>();
public IEnumerable<Order> Orders
{
get { return _orders; }
}
}
HasMany<Order>(Reveal.Property<Customer>("_orders")) etc.
protected virtual IList<Order> _orders { get; set; }