1

问题描述:该模型一次只适用于一个用户。一旦我一次获得多个用户,我就会遇到与未关闭 SqlDataReader 有关的严重错误。当我像这样关闭延迟加载时:

persistenceModel.Conventions.OneToManyConvention = (prop => prop.SetAttribute("lazy", "false"));

很好,但性能很慢。这使用 MVC Beta 1

有什么想法吗?

下面是我的全局 ASAX 的片段以及我的 SessionFactory 初始化代码。

*********** 这是我的 GLOBAL.ASAX ********

public class MvcApplication : NinjectHttpApplication
{
    public static IKernel Kernel { get; set; }

    protected override void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.IgnoreRoute("WebServices/*.asmx");

        routes.MapRoute("CreateCategoryJson", "Admin/CreateCategoryJson/{categoryName}");
        routes.MapRoute("User", "Admin/User/{username}", new { controller="Admin", action="user" });

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Session_Start(object sender, EventArgs e)
    {
        if (Session["rSkillsContext"] == null)
        {
            string logonName = this.User.Identity.Name.Replace("NUSOFTCORP\\", string.Empty);
            rSkillsContext context = new rSkillsContext(logonName);
            Session.Add("rSkillsContext", context);
        }
    }

    protected override IKernel CreateKernel()
    {
        log4net.Config.XmlConfigurator.Configure();
        Kernel = new StandardKernel(new RepositoryModule(), new AutoControllerModule(Assembly.GetExecutingAssembly()), new Log4netModule());
        return Kernel;
    }
}

***** 这是我的 NHibernateHelper.cs ******

    private ISessionFactory CreateSessionFactory()
    {
        var configuration = MsSqlConfiguration
                                .MsSql2005
                                .ConnectionString.FromConnectionStringWithKey("ConnectionString")
                                .ShowSql()
                                .Raw("current_session_context_class", "web")
                                .ConfigureProperties(new Configuration());

        var persistenceModel = new PersistenceModel();

        persistenceModel.Conventions.GetForeignKeyName = (prop => prop.Name + "ID");
        persistenceModel.Conventions.GetForeignKeyNameOfParent = (prop => prop.Name + "ID");
        // HACK: changed lazy loading
        persistenceModel.Conventions.OneToManyConvention = (prop => prop.SetAttribute("lazy", "false"));

        persistenceModel.addMappingsFromAssembly(Assembly.Load(Assembly.GetExecutingAssembly().FullName));
        persistenceModel.Configure(configuration);

        return configuration.BuildSessionFactory();
    }
4

1 回答 1

1

看起来你没有正确处理你的会话(一个月前我在 Ninject 和 NHibernate 上遇到了同样的错误)。它应该在请求开始时开始,并应在结束时处理。您能否提供开始和处理您的休眠会话的代码片段?

于 2009-02-15T10:26:39.013 回答