Hi I'm fairly new to WPF and C#, so please be easy on me if this is just a silly question:
I'm using VS2012, Entity Framework to create a model of a pre-exisiting database and I would like to bind some tables to some ListView... Now, I know there are different ways to do that but when I try to use an ObjectDataProvider I get an error at design time "No connection string named ... could be found in the application config file".
The strange thing is that if I run my application everything is working as it should, I just would like to remove that ugly error message and hopefully to get data in my list ad design time (that's why I would like to use an objectdataprovider).
Here are some parts of my code:
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);
}
}
...
}
}
In my DB the Table "Scaffali" has only two columns: "ScaffaliID" (identity) and "Scaffale".
I read somewhere that this could be a Visual Studio bug and tried various things:
- rebuild several times
- avoid # in paths
- compile for 32bits (I'm running Win8 x64)
but till now had no luck...
Thanks for your kind answer.
-= UPDATE 04/03/2013 =-
So far I still haven't found what are the conditions causing this strange behaviour, anyway I found that if I just build (not rebuild) two times in sequence the error message just disappear and everything seems to work as it should...