在应用程序配置文件中找不到名为'x'的连接字符串...但它有效? [英] No connection string named 'x' found in application config file... but it works?

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

问题描述

我已经看到与这个错误有关的几个不同的问题,但是似乎没有什么是我正在经历的。我刚刚在CodeProject.com上简单的一个项目教程,以更好地了解使用MVVM模式和实体框架( 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称为vms与适当的位置,并设置我的数据上下文的窗口:

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.

/ p>

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天全站免登陆