nhibernate验证器:无法编译映射文件:mapping_by_code

时间:2014-03-05 05:31:52

标签: nhibernate nhibernate-mapping

公共类EmployeeDetails         {

        public virtual string Address { get; set; }
        public virtual string Email { get; set; }
        public virtual double Salary { get; set; }
    }

 public EmployeeMap()//constuctor
        {   

           Define(x => x.Address).LengthBetween(20, 50);
            ValidateInstance.By((instance, context) => instance.Id != Guid.Empty)
                               .WithMessage("NhVal :");
            Define(x => x.Email).NotNullable();
            Define(x => x.Salary).Digits(5);
        }

这是我运行并检查案例的测试

public void Insert()         {

        var emp = new EmployeeDetails
            {

                Address = "deccan shivaji nagar pune",
                Email = "shivajinagar@gmail.com",
                Salary = 14000,

            };
        EmployeeQueries.Insert(emp);
        Assert.IsTrue(true);
    }


 using (var session = NhibernateConfig.SessionOpen())

            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(employee);
                    transaction.Commit();
                    session.Close();
                }
            }

public static void App_start(Configuration cfg)         {

        Env.SharedEngineProvider = new NHibernateSharedEngineProvider();
        var validatorEngine = Env.SharedEngineProvider.GetEngine();

        var nhvConfiguration = new FluentConfiguration();

        nhvConfiguration.SetDefaultValidatorMode(ValidatorMode.OverrideExternalWithAttribute);

        nhvConfiguration.Register(Assembly.GetExecutingAssembly().ValidationDefinitions())
                        .IntegrateWithNHibernate 
                        .ApplyingDDLConstraints() 
                        .RegisteringListeners(); 

        validatorEngine.Configure(nhvConfiguration);

        // init engine on global level
        cfg.Initialize(validatorEngine);
    }

    private static Configuration ConfigNHibernet()
    {
        var configure = new Configuration();
        configure.SessionFactoryName("Buildit");
        configure.DataBaseIntegration(db =>
            {
                db.Dialect<MsSql2008Dialect>(); 
                db.Driver<SqlClientDriver>();
                db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                db.IsolationLevel = IsolationLevel.ReadCommitted;
                db.ConnectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=Nhdatabaseschema;Integrated Security=True";
                db.Timeout = 50;
                db.LogFormattedSql = true;  
                db.LogSqlInConsole = true;  
  db.AutoCommentSql = true;
            });
        return configure;
    }

    private static HbmMapping GetMappings(Configuration config)//Mapping section
    {

        var mapper = new ModelMapper();


                 mapper.AddMappings(Assembly.GetAssembly(typeof(EmployeeDetails)).GetTypes());

        var domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
            config.AddMapping(domainMapping);
        return domainMapping;
    }
mapping section for nhibernate validator check whether it is correct or not

    public static ISession SessionOpen()
    {                                        
        return _sessionFactory.OpenSession();
    }

这是我遇到错误的会话:无法编译映射文档:mapping_by_code

1 个答案:

答案 0 :(得分:0)

在设置中添加

App_start(_nhConfig); 

和映射部分

var mapper = new ModelMapper();
mapper.AddMapping<EmployeeMap>();
var mapping = mapper.CompileMappingFor(new[] { typeof(EmployeeDetails) });
return mapping;  

并为验证器创建另一个类,不要在映射类

中包含它