No connection string named 'x' found in ap

2019-01-20 06:28发布

问题:

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:

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.



回答2:

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.



回答3:

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.