Connection string not found, but only during development

Hi, I'm pretty new to WPF and C #, so please be calm if this is just a dumb question:

I am using VS2012, Entity Framework to create a model of a pre-existing database, and I would like to bind some tables to some ListView ... Now I know that there are different ways to do this, but when I try to use ObjectDataProvider, I get a message about error during development "There is no connection string in the application configuration line ...".

It is strange that if I run my application, everything works as it should, I just want to remove this ugly error message and hopefully get the data in my graphic design (that I would like to use 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=&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); } } ... } } 

In my database, the "Scaffali" table has only two columns: "ScaffaliID" (identity) and "Scaffale".

I read somewhere that this could be a Visual Studio error and tried different things:

  • rebuild several times
  • avoid # in transit
  • compilation for 32 bit (I am running Win8 x64)

but still no luck ...

Thanks for your kind reply.

- = UPDATE 03/03/2013 = -

So far, I still have not found what conditions cause this strange behavior, in any case, I found that if I just build (don't rebuild) twice in a row, the error message just disappears and everything seems to work like this as it should ...

+4
source share
3 answers

I had the same problem, and it turned out that during development, the IDE did not seem to read App.config to get the connection string so that it would work when the connection was established. What I finished is to check if the code is at design time and manually create some elements for design purposes.

I use the following code to find out if the environment is in development mode:

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

Then in my code, where I return the elements, I do the following:

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

And I just implement these two methods.

Maybe the best solution, but it worked for me.

+2
source

Have you tried building the DataContext using ConnectionString yourself, instead of letting the framework use the one specified in the configuration file? Usually I prefer to build this object myself, so I can easily transfer it to another assembly and introduce a connection dependency. But that is another story. In your case, I would recommend you try the following:

  • Save connection string in webconfig.
  • Create context yourself

    var connectionStringBuilder = new EntityConnectionStringBuilder {Metadata = "{metadata}", Provider = "{provider}", ProviderConnectionString = "{connection string from config}"};

    var ctx = new BibliotecaEntities (connectionStringBuilder.ToString ());

I just give a hint so you can try. I have not tried this in full detail. Hope this helps.

0
source

This solution looked promising, but I still need to get it to work for me ...

Source: http://developernote.com/2012/03/rich-design-time-experience-with-wpf-and-ef-in-visual-studio-2010/

Sometimes the ability to see data during development greatly facilitates the creation of WPF controls. If I develop a WPF control with a complex user interface using data-bound controls, I usually define a property to access some typical data element:

 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"); } } } 

and then I use this property directly in XAML during development:

 <UserControl x:Class="SomeControl" ... xmlns:local="clr-namespace:ControlNamespace" DataContext="{Binding Source={x:Static local:ComplexData.DesignTimeItem}, Mode=OneWay}" ... > 

Then I need to take the last step. The problem is that the default object context constructor (DatabaseContext) uses the connection string included in the app.config application time application configuration file:

 <connectionStrings> <add name="MyContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;password=******;User Id=myuser;server=myserver;database=mydb;Persist Security Info=True;charset=utf8&quot;" providerName="System.Data.EntityClient"/> </connectionStrings> 

but during development, the application configuration file is devenv.exe.config , which does not include a connection string, and therefore creating my WPF control prevents the developer from loading. To solve this problem, I just copy the connection string included in app.config to devenv.exe.config located in "C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \" and restart Visual Studio .

0
source

All Articles