Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...