在流利的nhibernate私人收集映射 [英] Private collection mapping in fluent nhibernate

查看:113
本文介绍了在流利的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 );

而你的班级是

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;组; } 

让我走上正轨。


How can I map this:

public class Customer
{
   private IList<Order> _orders;
   public IEnumerable<Order> 
   GetAllOrders()
   {
      return _orders;     
   }
}

On the project page are some samples but none is about this situation. There is this sample:

// 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();  

But it assumes that Account has public field Customers and that scenario is different as mine. I tried some possible options but none works:

HasMany(x => Reveal.Propertie("_orders"))

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

HasMany(x => x.Orders)
    .Access.AsCamelCaseField(Prefix.Underscore);

and your class is

public class Customer
{
    private IList<Order> _orders = new List<Order>();

    public IEnumerable<Order> Orders 
    { 
        get { return _orders; }
    }
}

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:

HasMany<Order>(Reveal.Property<Customer>("_orders")) etc.

The collection must be exposed as a protected virtual property to allow proxying:

protected virtual IList<Order> _orders { get; set; }

This answer put me on the right track.

这篇关于在流利的nhibernate私人收集映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆