views:

1489

answers:

3

Hi, I am new to fluent NHinbernate, now I face one problem with mapping composite keys. Can anyone pointing out the URL or sample please?

+4  A: 

There's a UseCompositeId method.

public class EntityMap : ClassMap<Entity>
{
  public EntityMap()
  {
    UseCompositeId()
      .WithKeyProperty(x => x.Something)
      .WithReferenceProperty(x => x.SomethingElse);
  }
}
James Gregory
Note that in new versions of NHibernate UseCompositeId is replaced with CompositeId, and WithKeyProperty is just KeyProperty
Rob Walker
+1  A: 

if this is your first class

public class EntityMap : ClassMap<Entity>
{
  public EntityMap()
  {
    UseCompositeId()
      .WithKeyProperty(x => x.Something)
      .WithReferenceProperty(x => x.SomethingElse);
  }
}

here is the second with a reference on Entity

public class SecondEntityMap : ClassMap<SecondEntity>
    {
      public SecondEntityMap()
      {
        Id(x => x.Id);

        ....

        References<Entity>(x => x.EntityProperty)
          .WithColumns("Something", "SomethingElse")
          .LazyLoad()
          .Cascade.None()
          .NotFound.Ignore()
          .FetchType.Join();

      }
    }
Lars Hildebrandt

related questions