Fluent NHibernate enforce Not Nullable on Foreign Key Reference

后端 未结 2 1150
别那么骄傲
别那么骄傲 2021-01-03 03:06

Just getting my feet wet with some Fluent NHibernate AutoMap conventions, and ran into something I couldn\'t figure out. I assume I\'m just not looking in the right place...

相关标签:
2条回答
  • 2021-01-03 04:02

    It seems that IPropertyConvention is only called on simple properties of your classes. If your property references another class, you need to use IReferenceConvention too.

    Try this:

    public class FluentConvention : IPropertyConvention, IReferenceConvention  
    {      
        public void Apply(IPropertyInstance instance)
        {          
            instance.Not.Nullable();      
        }
    
        public void Apply(IManyToOneInstance instance)
        {
            instance.Not.Nullable();
        }
    }      
    
    0 讨论(0)
  • 2021-01-03 04:12

    You can override the auto-mapped properties as part of your AutoMap in Fluenttly.Configure().

    So you can do this:

    .Override<Job>(map => map.References(x => x.Group).Not.Nullable())
    

    It's not exactly convenient if you have a lot of classes that need this though.

    Edit: You can also specify the override in a class that implements IAutoMappingOverride like so:

        public class JobMappingOverride : IAutoMappingOverride<Job>
        {
                public void Override(AutoMapping<Job> mapping)
                {
                        mapping.References(x => x.Group).Not.Nullable();
                }
        }
    

    and include it like so:

        .UseOverridesFromAssemblyOf<JobMappingOverride>()
    

    This would keep your fluent configuration a little cleaner.

    0 讨论(0)
提交回复
热议问题