无法将NHibernate代码映射用于私有字段 [英] Cannot use NHibernate Code Mapping for a private field

查看:51
本文介绍了无法将NHibernate代码映射用于私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the equivalent NHibernate Code Mapping for the following Fluent NHibernate code:

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

我整个下午都在努力使它工作.到目前为止,这是我的代码:

I have spent all afternoon trying to get this to work. Here is my code so far:

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/论坛/#!topic/nhusers/wiH1DPGOhgU .我尝试从我的链接中复制和粘贴代码(见下文):

This answers my question: https://groups.google.com/forum/#!topic/nhusers/wiH1DPGOhgU. I have tried copying and pasting the code from my link (see below):

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

我得到的错误是:无法从用法中推断出袋子.

The error I get is: Bag cannot be inferred from the usage.

推荐答案

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

You have a couple of options to map a non-visible field by code.

这里有一些,请注意,我还没有按书面要求对它们进行过测试,但是无论如何应该对您有所帮助:

Here are some, please note that I haven't tested them yet as written, but should help you anyways:

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

这篇关于无法将NHibernate代码映射用于私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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