0

I am trying my hand at NHibernate's built-in mapping by code. I've got it mostly working now. My problem is how do I configure which properties on my objects are required in the database within the convention? I'm guessing this would be some sort of attribute markup?

I know if I do the mappings by hand, I can configure them as required, but how do I do this with conventions?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Origin
  • 1,943
  • 13
  • 33
  • The following post might be helpful: http://stackoverflow.com/questions/2605930/fluent-nhibernate-automap-convention-for-not-null-field -- I recommend looking at all of the answers, rather than just the accepted answer. – David Tansey May 31 '15 at 15:09
  • Thanks, but this applies to to Fluent mapping, not the mapping-by-code. I understand I could write my own attribute and logic to handle this with the configuration possible - but I thought this might be built in or already done by someone. – Origin May 31 '15 at 18:50

1 Answers1

0

Simply use nullable types, for example in the class,

public class Foo
{
   public int Bar { get; set; }
   public int? baz { get; set; }
}

Bar will not be nullable, while Baz will be nullable in the db when use mapping by code.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • What about things like References? For example: `public MyClass Test {get; set;}` . This defaults to Nullable because a class is nullable. – Origin Jun 01 '15 at 13:13
  • http://stackoverflow.com/questions/1791582/fluent-nhibernate-enforce-not-nullable-on-foreign-key-reference, try using automapping override public class JobMappingOverride : IAutoMappingOverride { public void Override(AutoMapping mapping) { mapping.References(x => x.Group).Not.Nullable(); } } – Low Flying Pelican Jun 01 '15 at 13:20
  • That's for Fluent NHibernate – Origin Jun 01 '15 at 17:34
  • Automapping is not Fluent NHibernate, http://fluentnhibernate.wikia.com/wiki/Auto_mapping – Low Flying Pelican Jun 01 '15 at 23:19