在应用程序配置文件中找不到名为“x"的连接字符串......但它有效吗?

No connection string named #39;x#39; found in application config file... but it works?(在应用程序配置文件中找不到名为“x的连接字符串......但它有效吗?)
本文介绍了在应用程序配置文件中找不到名为“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.

关于为什么会发生这种情况的任何想法?

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"的连接字符串......但它有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)