How to map dictionary with element referencing dictionary key?

26 views
Skip to first unread message

Filip Kinsky

unread,
Jun 25, 2011, 4:49:38 PM6/25/11
to nhu...@googlegroups.com
I'd like to map following classes, but I'm not able to figure out how to define the mappings (I'm using mapping by code):

public class Employee : EntityBase
{
public virtual string FirstName { get; protected set; }
public virtual string LastName { get; protected set; }
public virtual string EmpNumber { get; protected set; }
public virtual IDictionary<Month, Salary> Salaries { get; protected set; }
}

public class Salary
{
public virtual Month Month { get; protected set; }
public virtual decimal Amount { get; set; }
}

public class Month
{
public virtual int Year { get; protected set; }
public virtual int MonthNumber { get; protected set; }
}

The problem is I can't get Salary.Month mapped, because Month is also the key of Employee.Salaries dictionary. If I try to map the Month using following mapping code I get NHibernate.MappingException : Repeated column in mapping for collection: Employee.Salaries column: Year.

mapper.Class<Employee>(ca =>
                      {
         ca.Id(x => x.Id);
         ca.Property(x => x.FirstName, m => m.UniqueNotNull());
         ca.Property(x => x.LastName, m => m.NotNullable(true));
         ca.Property(x => x.EmpNumber, m => m.NotNullable(true));
         ca.ManyToOne(x => x.User, m => m.NotNullable(true));
        ca.Map(x => x.Salaries,
              m =>
              {
              m.Key(x => x.Column("EMPLOYEE_ID"));
              m.Table("EMPSALARIES");
              },
              m => m.Component(c =>
                                {
                                c.Property(x => x.Year);
                                c.Property(x => x.MonthNumber);
                                }),
              m => m.Component(c =>
                                {
                                c.Property(x => x.Amount);
                                c.Component(x => x.Month,
                                           x =>
                                            {
                                            x.Property(xx => xx.Year);
                                            x.Property(xx => xx.MonthNumber);
                                            });
                                }));
                      });

I was also trying to map the month as a component explicitely using mapper.Component() and than mapping Sallaries without specifying any key and element mapping, but result was just the same. Is there any trick how to achieve this?

Fabio Maulo

unread,
Jul 4, 2011, 11:38:56 AM7/4/11
to nhu...@googlegroups.com
c.Property(x => x.Year, map=> map.Column("KeyYear"));
c.Property(x => x.MonthNumber, map=> map.Column("KeyMonth"));

Filip Kinsky

unread,
Jul 5, 2011, 1:36:05 AM7/5/11
to nhu...@googlegroups.com
Thanks for your reply, Fabio. It unfortunately looks like there's no solution to map this dictionary and not to duplicate month+year columns in underlying DB table, which was my aim ;(
Reply all
Reply to author
Forward
0 new messages