gpt4 book ai didi

c# - 未找到连接字符串,但仅在设计时

转载 作者:行者123 更新时间:2023-11-30 12:30:59 25 4
gpt4 key购买 nike

您好,我是 WPF 和 C# 的新手,所以如果这只是一个愚蠢的问题,请放轻松:

我正在使用 VS2012 Entity Framework 创建一个预先存在的数据库模型,我想将一些表绑定(bind)到一些 ListView...现在,我知道有不同的方法可以做到这一点,但是当我尝试使用 ObjectDataProvider 时,我在设计时遇到错误“在应用程序配置文件中找不到名为 ... 的连接字符串”。

奇怪的是,如果我运行我的应用程序,一切正常,我只是想删除那个丑陋的错误消息,并希望在我的列表广告设计时获得数据(这就是为什么我想使用对象数据提供者)。

以下是我的部分代码:

App.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="BibliotecaEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;attachdbfilename=&quot;G:\PROGETTI-SOFTWARE\c# tests\Biblioteca\Biblioteca\biblioteca-db.mdf&quot;;initial catalog=BibliotecaDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>

ListaScaffali.xaml:

<Window x:Class="Biblioteca.ShelvesList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Biblioteca;assembly="
Title="ShelvesList" Height="341" Width="609">

<Window.Resources>
<ObjectDataProvider ObjectType="{x:Type local:BooksDB_Handler}" MethodName="TuttiGliScaffali" x:Key="ScaffaliObjSrc" />
</Window.Resources>

<Grid>
<Grid>
<ListView Name="listaScaffali" ItemsSource="{Binding Source={StaticResource ScaffaliObjSrc}}">
<ListView.View>
<GridView>
<GridViewColumn Header="Scaffali Disponibili:" DisplayMemberBinding="{Binding Scaffale}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Grid>
</Window>

BooksDB-Handler.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.Entity;
using System.Collections.ObjectModel;

namespace Biblioteca
{
public static class BooksDB_Handler
{

...

public static ObservableCollection<Scaffali> TuttiGliScaffali()
{
using (var ctx = new BibliotecaEntities())
{

var reader = from d in ctx.Scaffali
select d;
return new ObservableCollection<Scaffali>(reader);
}
}

...

}
}

在我的数据库中,“Scaffali”表只有两列:“ScaffaliID”(身份)和“Scaffale”。

我在某处读到这可能是 Visual Studio 错误并尝试了各种方法:

  1. 多次重建
  2. 避免在路径中使用#
  3. 为 32 位编译(我运行的是 Win8 x64)

但到现在还没有运气......

感谢您的友好回答。

-= 2013 年 4 月 3 日更新 =-

到目前为止我还没有找到导致这种奇怪行为的条件是什么,无论如何我发现如果我只是按顺序构建(而不是重建)两次错误消息就会消失并且一切似乎都可以正常工作。 ..

最佳答案

我遇到了同样的问题,我发现在设计时,IDE 似乎没有读取 App.config 来获取连接字符串,因此它在建立连接时崩溃了。我最终做的是检查代码是否处于设计时并手动创建一些用于设计目的的项目。

我使用以下代码来查明环境是否处于设计模式:

public static bool IsInDesignMode
{
get
{
#if Silverlight
return !HtmlPage.IsEnabled;;
#else
return DesignerProperties.GetIsInDesignMode(new DependencyObject());
#endif
}
}

然后在我要返回的代码中执行以下操作:

if (IsInDesignMode)
{
GetSampleData();
}
else
{
GetQueryData();
}

我只是实现了这两个方法。

可能有更好的解决方案,但这对我来说效果很好。

关于c# - 未找到连接字符串,但仅在设计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14903727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com