我正在使用 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