不能将NHibernate代码映射用于私有字段

时间:2017-11-28 16:41:56

标签: c# nhibernate nhibernate-mapping

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

List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='não pode ser nulo', propertyPath=idPessoa, rootBeanClass=class digifred.model.global.Pessoas, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]] with root cause

javax.validation.ConstraintViolationException: Validation failed for classes [digifred.model.global.Pessoas] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='não pode ser nulo', propertyPath=idPessoa, rootBeanClass=class digifred.model.global.Pessoas, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]

我整个下午都试图让这个工作。到目前为止,这是我的代码:

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

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

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

我得到的错误是:无法根据使用情况推断行李。

2 个答案:

答案 0 :(得分:0)

您可以使用命名策略:

cfg.SetNamingStrategy(myStrategy);

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

上的博文

答案 1 :(得分:0)

您可以通过代码映射不可见字段。

以下是一些,请注意我还没有按照书面形式对它们进行测试,但无论如何应该对你有所帮助:

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