您好我是WPF和C#的新手,所以如果这只是一个愚蠢的问题,请对我很轻松:
我正在使用VS2012,Entity Framework来创建一个预先存在的数据库的模型,我想将一些表绑定到一些ListView ... 现在,我知道有不同的方法可以做到这一点,但是当我尝试使用ObjectDataProvider时,我在设计时遇到错误“在应用程序配置文件中找不到名为...的连接字符串”。
奇怪的是,如果我运行我的应用程序一切都正常工作,我只想删除那个丑陋的错误消息,希望能够在我的列表广告设计时获取数据(这就是为什么我想使用一个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="G:\PROGETTI-SOFTWARE\c# tests\Biblioteca\Biblioteca\biblioteca-db.mdf";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错误并尝试了各种各样的事情:
但到现在为止没有运气......
谢谢你的回答。
- =更新04/03/2013 = -
到目前为止,我仍然没有找到导致这种奇怪行为的条件,无论如何我发现如果我只是按顺序构建(不重建)两次,错误消息就会消失,所有内容似乎都应该正常工作。 ..
答案 0 :(得分:2)
我遇到了同样的问题,我发现在设计时,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();
}
我只是实现了这两种方法。
可能有更好的解决方案,但这对我来说效果很好。
答案 1 :(得分:0)
您是否尝试过自己使用ConnectionString构造DataContext对象,而不是让框架使用配置文件中指定的框架?我通常更喜欢自己构造该对象,因此我可以轻松地将其移动到另一个程序集并注入连接依赖项。不过那是另一回事了。在你的情况下,我建议你试试这些东西:
自己构建上下文
var connectionStringBuilder = new EntityConnectionStringBuilder { 元数据=“{metadata}”, Provider =“{provider}”, ProviderConnectionString =“{来自配置的连接字符串}” };
var ctx = new BibliotecaEntities(connectionStringBuilder.ToString());
我只是给出一个提示,这样你就可以试试。我没有尝试过全部细节。希望它有所帮助。
答案 2 :(得分:0)
这个解决方案看起来很有希望,但我还没有让它为我工作......
来源:http://developernote.com/2012/03/rich-design-time-experience-with-wpf-and-ef-in-visual-studio-2010/
有时,在设计时查看数据的能力极大地促进了WPF控件的创建。如果我使用数据绑定控件开发具有复杂UI的WPF控件,我通常会定义一个属性来访问一些典型的数据项:
public static ComplexData DesignTimeItem
{
get
{
using (DatabaseContext db = new DatabaseContext())
{
var products = from p in db.products.Include("ProductType")
where p.product_id == 131
select p;
product product = products.First();
return new ComplexData(product, "100 kg");
}
}
}
然后我在设计时直接在XAML中使用此属性:
<UserControl x:Class="SomeControl"
...
xmlns:local="clr-namespace:ControlNamespace"
DataContext="{Binding Source={x:Static local:ComplexData.DesignTimeItem}, Mode=OneWay}"
...
>
然后我需要做最后一步。问题是默认实体上下文构造函数(DatabaseContext)使用运行时应用程序配置文件app.config中包含的连接字符串:
<connectionStrings>
<add name="MyContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string="password=******;User Id=myuser;server=myserver;database=mydb;Persist Security Info=True;charset=utf8"" providerName="System.Data.EntityClient"/>
</connectionStrings>
但在设计时,应用程序配置文件是 devenv.exe.config ,它不包含连接字符串,因此我的WPF控件的创建失败,导致设计器无法加载。要解决此问题,我只需将 app.config 中包含的连接字符串复制到“C:\ Program Files(x86)\中的 devenv.exe.config Microsoft Visual Studio 10.0 \ Common7 \ IDE \“并重新启动Visual Studio。