Mapping by code convention bags and so on

25 views
Skip to first unread message

Alexander Kot

unread,
Aug 18, 2011, 5:45:06 AM8/18/11
to nhu...@googlegroups.com
Hello all

I am playing with 3.2 mapping by code convention

I have
public class MyEntity : Entity
{
  ..................
  public virtual IEnumerable<string> LinesBag { get; set;  }

}

And next mapping:

m_mapper.BeforeMapBag += (insp, prop, map) =>
{
    var tn = String.Concat(prop.GetContainerEntity(insp).Name, "_", prop.ToColumnName()).ToUpperInvariant();
    map.Table(tn);
    map.Key(km =>
    {
        km.Column(cm =>
        {
            var cn = String.Concat(prop.GetContainerEntity(insp).Name, "_ID").ToUpperInvariant();
            cm.Name(cn);
            cm.Index(String.Concat("IFK_", cn));
        });

        km.ForeignKey(String.Concat("FK_", tn, "_2_", prop.GetContainerEntity(insp).Name.ToUpperInvariant()));

    }

            );
};

m_mapper.Class<MyEntity>(map =>
{
  //....................
    map.Bag(x => x.LinesBag,
            colmap => { },
            xmap =>
            {
                xmap.Element(em =>
                {
                    em.Column("Element");
                });
            }
        );
});

As result NHibernate generate next script

create table MYENTITY_LINESBAG (MYENTITY_ID UNIQUEIDENTIFIER not null, Element NVARCHAR(255) null)
create index IFK_MYENTITY_ID on MYENTITY_LINES (MYENTITY_ID)
alter table MYENTITY_LINESBAG add constraint FK_MYENTITY_LINESBAG_2_MYENTITY foreign key (MYENTITY_ID) references MYENTITY

The questions:
1) Can i define column "Element" (It's name size and so on) not in explicit mapping but in convention rules BeforeMapBag and so on?
2) Can i define somehow covering index for this case
    create index IFK_MYENTITY_ID on MYENTITY_LINES (MYENTITY_ID, Element)
    ?


Alexander Kot

unread,
Aug 18, 2011, 10:02:55 AM8/18/11
to nhu...@googlegroups.com
It seems to me that i have found answer to 1st question myself
m_mapper.BeforeMapElement += (insp, prop, map) =>
      {
          ...........
          map.Column(cm => cm.Name("Element"));
      };

What about 2d one?
Reply all
Reply to author
Forward
0 new messages