views:

31

answers:

1

I'm using Fluent NHibernate/Automapping and I have a class that has a collection, and each item in that collection has its own collection.

public class A
{
   public virtual IList<ClassB> Items { get; set; }
}

public class B
{
   public virtual IList<ClassC> ChildItems { get; set; }
}

This is related to this question, which was never answered, but was resolved by the OP keeping the parent object on the child object and marking it as not null.

public class C
{
  [NotNull]
  public virtual ClassB Parent { get; set; }

  // Other stuff
}

Is this the only way to set a foreign key as not null in Fluent NHibernate? Probably a silly question, but there is no reason for me to ever know what the parent object is, so having these properties would be kind of useless. If this is the only way, are there any disadvantages to doing this? Would it even be worth it if my code could just handle the relationships instead?

A: 

Assuming you're using fluent mappings instead of automapping, you can use this in your mapping file:

HasMany(x => x.ChildItems).Not.Null();
Daniel T.