在应用程序配置文件'X'未找到命名的连接字符串...但它的工作原理? [英] No connection string named 'x' found in application config file... but it works?

查看:344
本文介绍了在应用程序配置文件'X'未找到命名的连接字符串...但它的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了有关该错误的几种不同的问题,但没有似乎相当有什么我遇到。我只是跟着上CodeProject.com一个简单的,一个项目教程,更好地理解使用MVVM模式和Entity Framework(的 http://www.codeproject.com/Articles/873592/Tutorial-for-a-Basic-WPF-MVVM-Project-Using-实体)。我直接跟着这一切似乎很好地工作。

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.

不过我有点好奇,周围开始改变一些东西。我想知道更多关于如何在XAML工作的命名空间,所以我提出我的MainWindowViewModel成我命名的ViewModels一个新的文件夹。然后,我添加了一个新的命名空间,以我的XAML被称为虚拟机的适当位置,并设置我的数据上下文窗口:

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>



不过,当我这样做,我得到一个淡蓝色的下划线在Visual Studio中警告,上面写着没有连接字符串名为[myEntities]可以在应用程序配置文件中找到。但是,一个连接字符串存在于App.config文件,当我建立并运行它按预期工作程序。

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?

推荐答案

您正在声明在视图您的视图模型的实例。这导致VS实例化编辑器,依次运行VS进程中的构造函数代码中的类。

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.

您的构造函数试图连接到一个数据库。它寻找在当前运行的进程app.config文件的连接字符串。当前运​​行的进程的Visual Studio。我不相信有一个 devenv.exe.config 文件,如果有,它肯定没有你的连接字符串(和,请不要去创建一个,把你连通的在那里!)。

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!).

您的代码示数出来,而编辑器是用来捕捉在UI从UI组件的错误,并显示警告。它这样做,为您的代码。

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.

另一种解决方案是不设置的DataContext这种方式。在代码隐藏创建并分配它。这样一来,就可以执行检查,看看你。在设计并完全跳过过程

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.

这篇关于在应用程序配置文件'X'未找到命名的连接字符串...但它的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆