2014-02-05 49 views
0

我正在嘗試爲Nhibernate使用ServiceStack IUserAuthRepository實現。 我在我的服務IOC容器中註冊了NHibernateUserAuthRepository,但我不知道如何告訴Nhibernate將AuthUser和角色映射到數據庫表。使用Nhibernate的Servicestack UserAuth持久性(使用ServiceStack.Authentication.NHibernate)

我的映射發生在容器實例化時ISessionFactory(使用FluentNhibernate)。

這裏的APPHOST代碼:

container.Register<ICacheClient>(new MemoryCacheClient()); 

container.Register<IDatabaseFactory>(c => new Oracle10DatabaseFactory(_DomainAssembly, _DomainAssemblyName, 
                  c.Resolve<ConfigurationParameters>())); 
// Register EventPublisher 
     container.RegisterAutoWiredAs<EventPublisher, IEventPublisher>().ReusedWithin(ReuseScope.Request); 
     container.RegisterAutoWiredAs<EventPublisherInterceptor, IInterceptor>().ReusedWithin(ReuseScope.Request); 

// Register Session & UnitOfWork 
container.Register<NHibernate.ISession>(item => 
container.Resolve<IDatabaseFactory>().SessionFactory.OpenSession(new EventPublisherInterceptor(container.Resolve<IEventPublisher>()))) 
      .ReusedWithin(ReuseScope.Request); 

container.Register<IUnitOfWork>(item => new UnitOfWork(container.Resolve<NHibernate.ISession>())).ReusedWithin(ReuseScope.Request); 

Plugins.Add(new AuthFeature(() => new AuthUserSession(), 
       new IAuthProvider[] { new CredentialsAuthProvider() })); 

這裏的Oracle10DatabaseFactory構造:

public Oracle10DatabaseFactory(Assembly assembly, string namespace, ConfigurationParameters parameters) 
{ 
    var fileCache = new ConfigurationFileCache(assembly, parameters.PathToConfigurationFolder); 
    var config = fileCache.LoadConfigurationFromFile(); 
    if (config == null) 
    {      
     var mapping = AutoMap.Assembly(assembly, new MappingConfiguration(new List<string>() { namespace })) 
        .Conventions.Add<ReferenceIndexConvention>() 
        .Conventions.Add<GuidIndexConvention>() 
        .UseOverridesFromAssemblyOf<MappingConfiguration>(); 


     this.sessionFactory = 
         Fluently.Configure() 
         .Database(OracleClientConfiguration.Oracle10.ConnectionString(c => c.FromConnectionStringWithKey("ZetesMobility_DataAccess_Connectionstring_Oracle")) 
          .UseReflectionOptimizer()) 


         .Mappings(m => m.AutoMappings.Add(mapping)) //.Add(baseMapping) 
         .CurrentSessionContext("web") 
         .ExposeConfiguration(c => c.BuildSchema(NHibernateExtensions.RecreateSchema())) 
         .ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote")) 
         .ExposeConfiguration(fileCache.SaveConfigurationToFile) 
         .BuildSessionFactory(); 

     config = fileCache.LoadConfigurationFromFile(); 
     this.sessionFactory = config.BuildSessionFactory(); 
    } 
    else 
    { 
     this.sessionFactory = config.BuildSessionFactory(); 
    } 
} 

我如何驗證類添加到映射配置?

非常感謝大家。

回答

1

在你Fluently.Config(),該部分映射(M - > MS ......,你需要告訴FluentNhibernate包括在包中的映射,像這樣:

  .Mappings(m => 
       m.FluentMappings 
        .AddFromAssemblyOf<UserAuthPersistenceDto>()) 

此致或許應該最終看起來像

  .Mappings(m => 
      { 
       m.AutoMappings.Add(mapping); 
       m.FluentMappings 
        .AddFromAssemblyOf<UserAuthPersistenceDto>(); 
      }) 
+0

現在我得到NHibernate的異常「無會話綁定到當前上下文」當我試圖創建默認用戶。userRepository.GetUserAuthByUserName(「管理員」)失敗。 –

+0

如何/你在哪裏打開你的會話(ISessionFactory.OpenSession())? –

+0

我正在試圖查看它,它很奇怪,因爲GitHub中的代碼與隨Nuget包一起提供的Api不同。公共NHibernateUserAuthRepository構造函數需要'ISessionFactory',但GitHub中的代碼是公共NHibernateUserAuthRepository(INHibernateSessionProvider sessionProvider)。然後我很難理解問題出在哪裏。 –

相關問題