"Database name not found in config file" weirdness

"Database name not found in config file" weirdness

Old forum URL: forums.lhotka.net/forums/t/9171.aspx


lukky posted on Sunday, July 04, 2010

Hi,

VS2010's designer throws a ConfigurationErrorsException with above message when I put a call to CustomerList.Get() in the ctor of my ViewModel<T> derived class, though executing the program runs fine.

A bit of background:

I have an OrderViewModel derived from ViewModel<Order>. In the Windows' xaml, I declare a resource of type OrderViewModel.

On the OrderViewModel, I added a DependencyProperty for my CustomerList, and it's when trying to fill the list in the ctor that the designer pukes. Again, running the code doesn't give any exception and does retrieve the CustomerList.

CustomerList derives from ReadOnlyListBase<CustomerList, Customer>

Any ideas ? 

 

RockfordLhotka replied on Monday, July 05, 2010

The designer creates an instance of the view (XAML). If your view directly references the viewmodel like this:

<my:ReallyCoolViewModel x:Key="ViewModel" />

then as the XAML is loaded by the designer, the designer will create an instance of your type. Of course that triggers the constructor, which calls the data portal. And all this is running inside of Visual Studio, so it uses the app.config file for Visual Studio - which obviously doesn't have your database connection string.

There are a few other scenarios where this can happen - but it is a recurring theme - as the designer instantiates the XAML, that triggers some of your code that uses the data portal - which fails for perfectly understandable reasons.

There are a couple solutions.

One is to use a CollectionViewSource as a resource, instead of directly using your viewmodel:

<CollectionViewSource x:Key="ViewModelViewSource" />

This is what the designer will do if you use drag-and-drop data binding. This doesn't automatically create an instance of your type, since some other code must run to hook your viewmodel to the Source property of the CollectionViewSource.

Another option is to put an "is in design time" check into your viewmodel's constructor. That's this line of code:

if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))

 

lukky replied on Monday, July 05, 2010

Rocky,

I had intuitively used the "design time" check, and it did solve the problem, though I don't think using this kind of check is clean when doing something other than custom controls. Maybe I'm wrong.

As for the CollectionViewSource, is there an example of using it in the CSLA samples ?

Thanks

RockfordLhotka replied on Monday, July 05, 2010

The C# SL sample called CslaMvvmSl was created using the VS10 designer features, so it uses CVS resources. It also uses a version of Bxf (before I put it in CodePlex) to manage the view <-> viewmodel wire-ups, etc.

Curelom replied on Wednesday, July 07, 2010

This article may help, though I haven't tried it yet with CSLA.  It shows you how to setup sample data rather than calling the object.

 

http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx

 

Copyright (c) Marimer LLC