未在用户控件中初始化?

时间:2011-01-12 21:03:11

标签: c# winforms nhibernate castle-activerecord

在用户控件中使用时,为什么AR未正确初始化的任何想法?我的初始化正确执行AFAIK,因为我的父表单能够成功查询。但是,当我尝试在用户控件中查询时,我得到Warning 1 An ActiveRecord class (Binder) was used but the framework seems not properly initialized. Did you forget about ActiveRecordStarter.Initialize() ? 0 0。这阻止我将用户控件拖到窗体上。如果有帮助,我会在Program.Main()中初始化AR。

这是stacktrace:

at Castle.ActiveRecord.ActiveRecordBase.EnsureInitialized(Type type)
at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType, DetachedCriteria detachedCriteria, Order[] orders)
at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType)
at Castle.ActiveRecord.ActiveRecordBase`1.FindAll()
at TxLocates.BinderTabReferenceControl.BinderTabReferenceControl_Load(Object sender, EventArgs e) in C:\dev\TxLocates\src\TxLocates\BinderTabReferenceControl.cs:line 56
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)  

1 个答案:

答案 0 :(得分:2)

Visual Studio设计器为您的用户控件执行OnLoad,但不运行Program.Main()。因此,ActiveRecord未在设计器的上下文中初始化。

解决方法:使用DesignMode property检测设计模式以避免执行与ActiveRecord相关的代码,或尝试从其他事件调用ActiveRecord。