gpt4 book ai didi

c# - 除非我调用 session.Flush(),否则 NHIbernate 和 Rhino-Security 不会保存

转载 作者:行者123 更新时间:2023-11-30 22:44:08 25 4
gpt4 key购买 nike

我在使用 nHibernate 和 Rhino.Security 时遇到问题。在为获得正确的配置设置而奋斗了几个小时之后,我终于让代码运行起来没有任何错误。但是,除非我调用 session.Flush(),否则不会将任何条目保存到数据库中。

看网上的各种例子;我不应该调用 flush。

这是我的配置代码:

var cfg = new Configuration()
.SetProperty(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName)
.SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, "………")
.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.UseSecondLevelCache, "true")
.SetProperty(Environment.UseQueryCache, "true")
.SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName)
.AddAssembly("GA.CAP.Website")
;

Security.Configure<AspNetUser>(cfg, SecurityTableStructure.Prefix);

var factory = cfg.BuildSessionFactory();

var session = factory.OpenSession();


var authorizationRepository = new AuthorizationRepository(session);
IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);
var permissionService = new PermissionsService(authorizationRepository, session);
IoC.Container.RegisterInstance<IPermissionsService>(permissionService);
var authService = new AuthorizationService(permissionService, authorizationRepository);
IoC.Container.RegisterInstance<IAuthorizationService>(authService);

测试代码:

    authorizationRepository.CreateUsersGroup("GAAdmins");
var group = authorizationRepository.GetUsersGroupByName("GAAdmins");

GetUsersGroupByName 调用返回 null。如果我在两个调用之间添加一个 session.Flush 调用,它会正常工作并返回该组。

基于各种博客中的示例,例如this one ,我不应该调用冲洗。此外,Rhino.Security 代码中包含的测试用例不会进行任何刷新,如下所示:

这是直接来自 Rhino.Security 的测试用例夹具:

    // on first deploy
Operation operation = authorizationRepository.CreateOperation("/Account/View");
// when creating account
UsersGroup group = authorizationRepository.CreateUsersGroup("Belongs to " + account.Name);

// setting permission so only associated users can view
permissionsBuilderService
.Allow(operation)
.For(group)
.On(account)
.DefaultLevel()
.Save();

// when adding user to account
authorizationRepository.AssociateUserWith(user, group);

bool allowed = authorizationService.IsAllowed(user, account, "/Account/View");
Assert.True(allowed);

我是否缺少某些设置?

谢谢,

瑞克

最佳答案

这是预料之中的,RS 使用与您的代码相同的 session ,并且在内部调用 Flush 可能会导致意想不到的后果。提交您的交易或调用 Flush

关于c# - 除非我调用 session.Flush(),否则 NHIbernate 和 Rhino-Security 不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3571196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com