我已经缩小到一个简单的方法,我试图从实体模型填充Observable集合。我不知道我是否正确地做到了。
注意:这似乎是设计师错误。我可以构建并运行程序......所以它出现了。
private ObservableCollection<LessonGroup> lessonGroups;
public ObservableCollection<LessonGroup> LessonGroups
{
get { return LessonGroups; }
set { lessonGroups = value; RaisePropertyChanged("LessonGroups"); }
}
private void GetLessonGroups()
{
//this using statement causes ArgumentException
using (MyEntities ae = new MyEntities())
{
foreach (LessonGroup lg in ae.LessonGroups)
{
LessonGroups.Add(lg);
}
}
}
例外情况如下:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at IMPACT.ARCTrainer.Model.ARCTrainerEntities..ctor() in C:\Users\Leland\Documents\Visual Studio 2010\Projects\ARCTrainer\Model\ARCTrainer.Designer.cs:line 56
at IMPACT.ARCTrainer.ViewModel.LessonsViewModel.GetLessonGroups() in C:\Users\Leland\Documents\Visual Studio 2010\Projects\ARCTrainer\ViewModel\LessonsViewModel.cs:line 171
at IMPACT.ARCTrainer.ViewModel.LessonsViewModel..ctor(ViewModelLocator viewModelLocator) in C:\Users\Leland\Documents\Visual Studio 2010\Projects\ARCTrainer\ViewModel\LessonsViewModel.cs:line 37
at IMPACT.ARCTrainer.ViewModel.ViewModelLocator..ctor() in C:\Users\Leland\Documents\Visual Studio 2010\Projects\ARCTrainer\ViewModel\ViewModelLocator.cs:line 46
答案 0 :(得分:1)
GetLessonGroups似乎是在LessonsViewModel类的构造函数中调用的。
此方法调用MyEntities,看起来这个Entity Framework上下文与GUI项目不同。
要解决您的问题,最好的方法是检查您是否处于设计模式。如果您使用的是MVVM Light,则可以检查IsInDesignMode属性(因此,仅当属性为false时才调用GetLessonGroups方法。)