I\'ve seen several different questions relating to this error but none seem to be quite what I\'m experiencing. I just followed a simple, one project tutorial on CodeProject
Building the application and working in editor mode are two different activities for the editor. The editor has to parse/build the code in states which the code can successfully compile and failure states as well; each time it has to give end results; as if you have compiled and built the application.
moved my MainWindowViewModel into a new folder
Since you moved the location the full build compile will link in the parts necessary to create an application and as you see it works. But for the editor, when it is parsing MainWindowViewModel
which was at the top level, it may have a cached version location of the app.config which was at the parent level and not the new level.
Regardless its editor processing heuristics are causing the file not to be found for the current location of the moved file.
Microsoft claims on their website that it is an Entity Framework issue. However, I (and others) found something strange though. When you close the file causing the error, the error itself also goes away. I am not sure if this is normal behaviour of VS, or what else the reason for this is.
If you put the datacontext in the code-behind and remove the datacontext from the XAML file the error is also gone and it still compiles just fine.
DataContext = new MainWindowViewModel();
I always try to keep my code-behind empty, but from what I understand there is little to none downside to that in this case.
You're declaring an instance of your view model in the view. That results in VS instantiating your class within the editor, which in turn runs your constructor code within the VS process.
Your constructor is attempting to connect to a database. It's looking for a connection string in the app.config file for the currently running process. The currently running process is Visual Studio. I don't believe there is a devenv.exe.config
file, and if there is, it definitely doesn't have your connection string (and, please, don't go create one and put your connex in there!).
Your code is erroring out, and the editor is designed to capture errors from UI components and show warnings in the UI. It's doing that for your code.
The solution is to not perform work in your constructor. You really shouldn't be doing long-running ops in there. Where you should move your database logic can only be determined by you and your application design.
An alternative is to accept this can happen and handle your error better. Perhaps log the missing connection string. Or catch the exception and move on. Not the best design decision there.
Another solution is to not set your DataContext this way. Create it in the codebehind and assign it there. That way, you can perform a check to see if you're in the designer and skip the process altogether.
Last option: no harm, no foul. Just forget the error in the designer. It doesn't harm your application in the least. Move on to more important tasks.