No connection string named 'x' found in application config file... but it works?

c# entity-framework-6 mvvm wpf xaml

Question

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.com to get a better understanding of using the MVVM pattern and Entity Framework (http://www.codeproject.com/Articles/873592/Tutorial-for-a-Basic-WPF-MVVM-Project-Using-Entity). I followed it directly and everything seemed to work perfectly.

However I got a little curious and started changing some things around. I wanted to know more about how the namespaces worked in XAML so I moved my MainWindowViewModel into a new folder that I named ViewModels. Then I added a new namespace to my XAML called "vms" with the appropriate location, and set my data context for the window:

<Window x:Class="CpMvvMWithEntity.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vms="clr-namespace:CpMvvMWithEntity.ViewModels"
    xmlns:local="clr-namespace:CpMvvMWithEntity"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
   <vms:MainWindowViewModel />
</Window.DataContext>

But when I do this I get a light blue underline warning in Visual Studio that reads "No connection string named [myEntities] could be found in the application config file." But a connection string exists in the App.config file, and when i build and run the program it works as expected.

Any ideas as to why this is happening?

1
1
7/5/2016 6:27:15 PM

Accepted Answer

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.

7
5/23/2017 11:51:29 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow