1

我已经缩小到一个简单的方法,我试图从实体模型中填充一个 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
4

1 回答 1

1

GetLessonGroups 似乎是在您的类 LessonsViewModel 的构造函数中调用的。

此方法调用 MyEntities,看起来此实体框架上下文与 GUI 项目不是同一个项目。

要解决您的问题,最好的方法是检查您是否处于设计模式。如果您使用的是 MVVM Light,则可以检查 IsInDesignMode 属性(因此,仅当属性为 false 时才调用 GetLessonGroups 方法)。

于 2012-06-05T20:52:32.510 回答