0

My project is a Windows Application, targeting .NET Framework 4.5., referencing EntityFramework 6.1.1.

My main form has a UserControl, which has a second usercontrol within. The nested UserControl constructor has something similar to:

public ucNestedUserControl()
{
   InitializeComponent();
   listAll();
}

listAll is a method which pulls loads data from SQL Server database tables (using EntityFramework).

I did some minor changes in database (removed delete cascade from some relationships), and after opening my project and double clicking (step 1) the main Windows Form in Solution Explorer, I noticed that Visual Studio is firing the nested UserControl constructor (step 2) and reading the database records with listAll method.

enter image description here

How can I avoid this nested UserControl constructor from loading/connecting to database while in design mode? Is this Visual Studio 2013 default behavior?

enter image description here

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
  • You can't. Use the DesignMode property to prevent code from running in design mode. – Hans Passant Aug 27 '14 at 00:07
  • @Hans it could be a valid answer. It seems this piece of code is working: `public static bool IsInDesignMode() { return System.Reflection.Assembly.GetExecutingAssembly().Location.Contains("VisualStudio"); }` – Junior Mayhé Aug 27 '14 at 00:16

0 Answers0