Nhibernate验证注释不生成有效模式

时间:2013-03-13 12:46:20

标签: nhibernate nhibernate-validator

我使用nhibnernate生成模式,但它没有生成它不考虑约束:

 public class Person
    {
        public virtual int Id { get; set; }

        [NotNull]
        public virtual string name { get; set; }
    }

public class PersonMap : ClassMapping<Person>
    {
        public PersonMap()
        {
            Id(x => x.Id);
            Property(x => x.name);
        }
    }

public class PersonVal : ValidationDef<Person>
    {
        public PersonVal()
        {
            Define(x => x.name).NotNullable();
        }
    }

我将验证附加到配置如下:

    public static void SetupNHibernateValidator(Configuration config)
            {
                var nhvConfiguration = new FluentConfiguration();

                nhvConfiguration
                    .SetDefaultValidatorMode(ValidatorMode.UseAttribute)
                    .Register(Assembly.Load("ClassLibrary2").ValidationDefinitions());

                nhvConfiguration
                    .IntegrateWithNHibernate
                    .ApplyingDDLConstraints()
                    .RegisteringListeners();


                //nhvConfiguration.Register(new [] { typeof(Person) });    
                nhvConfiguration.Register(new [] { typeof(PersonVal) });

                ValidationEngine = new ValidatorEngine();
                ValidationEngine.Configure(nhvConfiguration);

                ValidatorInitializer.Initialize(config, ValidationEngine);
            }

        public static ISessionFactory BuildSessionFactory()
        {
            const string ConnectionString = @"XYZ";

            _nhConfiguration = new Configuration( db =>
db.Dialect<MsSql2008Dialect>();
                db.Driver<SqlClientDriver>();
                db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                db.IsolationLevel = IsolationLevel.ReadCommitted;
                db.ConnectionString = connection;
                db.Timeout = 10;);


            ValidationProvider.SetupNHibernateValidator(_nhConfiguration);
            var mapping = GetMappings();
            _nhConfiguration.AddDeserializedMapping(mapping, "ClassLibrary2");
            return _sessionFactory = _nhConfiguration.BuildSessionFactory();
        }

,生成的输出为:

create table Person (
        Id INT not null,
       name NVARCHAR(255) null,
       primary key (Id)
    )

可能是什么问题? NAME字段仍然定义为NULL,它应该是NOT NULL。

对不起伙计们,我知道它的代码太多......

感谢您的帮助...

修改

我正在使用代码映射

using NHibernate.Validator.Cfg.Loquacious;
    public class PersonVal : ValidationDef<Person>


using NHibernate.Mapping.ByCode.Conformist;
        public class PersonMap : ClassMapping<Person>

using NHibernate.Validator.Constraints;
            public class Person

1 个答案:

答案 0 :(得分:0)

问题在于:

Register(Assembly.Load("ClassLibrary2").ValidationDefinitions()

我已将其替换为:

Register(Assembly.Assembly.GetExecutingAssembly().ValidationDefinitions())