Private collection mapping in fluent nhibernate

前端 未结 3 436
日久生厌
日久生厌 2020-12-17 01:22

How can I map this:

public class Customer
{
   private IList _orders;
   public IEnumerable 
   GetAllOrders()
   {
      return _o         


        
3条回答
  •  生来不讨喜
    2020-12-17 01:23

    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 _orders = new List();
    
        public IEnumerable 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(Reveal.Property("_orders")) etc.
    

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

    protected virtual IList _orders { get; set; }
    

    This answer put me on the right track.

提交回复
热议问题