You cannot use NHibernate Code Mapping for a private field

What is the equivalent NHibernate code mapping for the following Fluent NHibernate code:

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

I spent all day trying to get this to work. Here is my code:

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

This answers my question: https://groups.google.com/forum/#!topic/nhusers/wiH1DPGOhgU . I 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: Bag cannot be taken out of use.

0
c # nhibernate nhibernate-mapping
Nov 28 '17 at 16:41
source share
2 answers

You can simply use the naming strategy:

 cfg.SetNamingStrategy(myStrategy); 

Where myStrategy is an implementation of INamingStrategy . See My Blog Post https://weblogs.asp.net/ricardoperes/nhibernate-conventions

0
Nov 29 '17 at 11:21
source share

You have several options for displaying an invisible field by code.

Here are some of them, please note that I have not tested them as I wrote, but should help you anyway:

 // 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"))); } } 
0
Dec 06 '17 at 2:26 on
source share



All Articles