关于初始化ActiveRecord的实体类对象
ActiveRecord使用ORM的实体类对象之前要先初始化,官方网站上面有这么一句
找了一些代码发现PortalUser也是从ActiveRecordBase继承的没错。后来发现Initialize方法有4种不同的版本。干脆把实体类的Modle的程序集HelloCastle.Model.dll都一起初始化了。
ActiveRecordStarter.Initialize( source, typeof(Blog) );
如果每个类都要这样初始化才能用的话那就麻烦了。所以它建议你放在Global文件的Application_ Start方法里面。但是使用
ActiveRecordStarter.Initialize( source, typeof(ActiveRecordBase));
这句话的时候提示出错。
You have accessed an ActiveRecord class that wasn't properly initialized. The only explanation is that the call to ActiveRecordStarter.Initialize() didn't include HelloCastle.DAL.PortalUser class
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: Castle.ActiveRecord.Framework.ActiveRecordException: You have accessed an ActiveRecord class that wasn't properly initialized. The only explanation is that the call to ActiveRecordStarter.Initialize() didn't include HelloCastle.DAL.PortalUser class
源错误:
行 64:
行 65: Public Overloads Shared Function FindAll() As PortalUser()
行 66: Return CType(ActiveRecordBase.FindAll(GetType(PortalUser)), PortalUser())
行 67: End Function
行 68:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: Castle.ActiveRecord.Framework.ActiveRecordException: You have accessed an ActiveRecord class that wasn't properly initialized. The only explanation is that the call to ActiveRecordStarter.Initialize() didn't include HelloCastle.DAL.PortalUser class
源错误:
行 64:
行 65: Public Overloads Shared Function FindAll() As PortalUser()
行 66: Return CType(ActiveRecordBase.FindAll(GetType(PortalUser)), PortalUser())
行 67: End Function
行 68:
找了一些代码发现PortalUser也是从ActiveRecordBase继承的没错。后来发现Initialize方法有4种不同的版本。干脆把实体类的Modle的程序集HelloCastle.Model.dll都一起初始化了。
ActiveRecordStarter.Initialize([Assembly].Load("HelloCastle.Model"), source)
它放在Application_ Start方法里面。这下不用每次访问数据实体对象都要重复去初始化了.