不能对私有字段使用 NHibernate 代码映射

Cannot use NHibernate Code Mapping for a private field

以下 Fluent NHibernate 代码的等效 NHibernate 代码映射是什么:

Map(x => x.Orders).Access.CamelCaseField(Prefix.Underscore);

我整个下午都在努力让它发挥作用。到目前为止,这是我的代码:

public class Customer
{
    private readonly IList<Order> _orders = new List<Order>();
    public virtual Guid ID { get; set; }
    public virtual string LastName { get; set; }
    public IEnumerable<Order> Orders 
    {
        get { foreach (var order in _orders) yield return order; }
    }

    internal void AddOrder(Order order)
    {
        _orders.Add(order);
    }
}  

public class CustomerMap : ClassMapping<Customer>
{
    public CustomerMap()
    {
        Id<Guid>(x => x.Id);

        Component(x => x.LastName, y =>
        {
            y.Property<string>(z => z.LastName);
        });

        Bag(x => x._orders, collectionMapping =>
        {
            collectionMapping.Table("CustomerOrders");
            collectionMapping.Cascade(Cascade.None);
            collectionMapping.Key(k => k.Column("CustomerId"));
        },
        map => map.ManyToMany(p => p.Column("OrderId")));
    }
}

这回答了我的问题:https://groups.google.com/forum/#!topic/nhusers/wiH1DPGOhgU。我已经尝试从我的 link 中复制并粘贴代码(见下文):

  this.Bag(
                    r => "privatefieldtomap",
                    map =>
                        {
                            map.Access(Access.Field);
                            map.Table("table");
                            map.Key(k => k.Column("foreignkey"));
                        },
                    r => r.Element(m => m.Column("columntomap")));

我得到的错误是:Bag cannot be inferred from the usage。

您可以只使用命名策略:

cfg.SetNamingStrategy(myStrategy);

其中 myStrategyINamingStrategy 的实现。 在 https://weblogs.asp.net/ricardoperes/nhibernate-conventions

查看我的博客 post

您有几个选项可以通过代码映射不可见字段。

这里有一些,请注意我还没有按照写的那样测试它们,但无论如何应该对你有帮助:

// Option 1:
public class CustomerMap : ClassMapping<Customer>
{
    public CustomerMap()
    {
        // ...

        Bag<Order>("_orders", collectionMapping =>
        {
            //...
        },
        map => map.ManyToMany(p => p.Column("OrderId")));
    }
}

// Option 2: No strings, but I'm not sure if this one would really work as is.
public class CustomerMap : ClassMapping<Customer>
{
    public CustomerMap()
    {
        // ...

        Bag(x => x.Orders, collectionMapping =>
        {
            collectionMapping.Access(Accessor.Field),

            //...
        },
        map => map.ManyToMany(p => p.Column("OrderId")));
    }
}

// Option 3: No strings, but more convoluted.
public class Customer
{
    internal class PrivatePropertyAccessors
    {
        public static Expression<Func<Customer, IEnumerable<Order>>> OrdersProperty = c => c._orders;
    }

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

    public IEnumerable<Order> Orders
    {
        get { foreach (var order in _orders) yield return order; }
    }
}

public class CustomerMap : ClassMapping<Customer>
{
    public CustomerMap()
    {
        // ...

        Bag(Customer.PrivatePropertyAccessors.OrdersProperty, collectionMapping =>
        {
            //...
        },
        map => map.ManyToMany(p => p.Column("OrderId")));
    }
}