views:

40

answers:

1

I just created a user control. This control also makes use of my static Entity Framework class to load two comboboxes. All is well and runs without a problem. Design and runtime are working. Then when I stop the application all the forms that contain my UserControl don't work any more in design time. I just see two errors:

Error1: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Error 2: The variable ccArtikelVelden is either undeclared or was never assigned. (ccArtikelVelde is my UserControl)

Runtime everything is still working

My static EF Repositoy class:

public class BSManagerData
{
    private static BSManagerEntities _entities;
    public static BSManagerEntities Entities
    {
        get
        {
            if (_entities == null)
                _entities = new BSManagerEntities();
            return _entities;
        }
        set
        {
            _entities = value;
        }
    }
}

Some logic happening in my UserControl to load the data in the comboboxes:

private void LaadCbx()
{
    cbxCategorie.DataSource = (from c in BSManagerData.Entities.Categories
                               select c).ToList();
    cbxCategorie.DisplayMember = "Naam";
    cbxCategorie.ValueMember = "Id";
}

private void cbxCategorie_SelectedIndexChanged(object sender, EventArgs e)
{
    cbxFabrikant.DataSource = from f in BSManagerData.Entities.Fabrikants
                              where f.Categorie.Id == ((Categorie)cbxCategorie.SelectedItem).Id
                              select f;
    cbxFabrikant.DisplayMember = "Naam";
    cbxFabrikant.ValueMember = "Id";
}

The only way to make my forms work again, design time, is to comment out the EF part in the UserControl (see above) and rebuild. It's very strange, everything is in the same assembly, same namespace (for the sake of simplicity).

Anyone an idea?

+2  A: 

Looks like you're somehow executing database code in design mode. To prevent this, hunt down the control and method causing this, and use:

if (DesignMode)
    return

Also, it's a very bad idea to cache the database context statically. It will cause problems with multithreading, and also when you're doing inserts and deletes. The database context is meant to be used for a single "unit of work", is adding 2, and removing 3 other objects and calling SaveChanges once.

Sander Rijken
Thank you for your repliesI'm all for best practice and if using the static class is bad practice I have to see how I should do it in another way. The problem is that I got an error some time ago where I couldn't edit an object from another context. I read on StackOverflow that creating a static class was one of the solutions. That's why I went for this approach. I'm also not a big fan of every time when I need to access the DB, build a using statement around my operation. I'm new to EF, so any constructive advice is more then welcome!
Sven
Just tried what you proposed but still not good. Now I did the following:private void LaadCbx() { if (DesignMode) return; cbxCategorie.DataSource = (from c in BSManagerData.Entities.Categories select c).ToList(); cbxCategorie.DisplayMember = "Naam"; cbxCategorie.ValueMember = "Id"; }However, when I try to add the control to my form I get the following error: http://img716.imageshack.us/img716/6549/eferror.png
Sven
From where are you calling LaadCbx()? In case you're calling it from a constructor, try to remove it from the control's constructor. It's better (not just for this issue, but in general) to do as little as possible in the constructor, especially not DB connections. You should be able to use a hook like OnLoad to load the data instead.
Sander Rijken
Thanks for the adviceI placed it in the the Load event because that indeed seemed like a better location. I however had to place the DesignMode in there. Otherwise it would not work. Can't understand why it's complaining. Isn't the app.config visible for the UserControl? Could you also give me some advice concerning the EF statis class. What if I have a combobox on a form where I load objects from a context and I want to save an object containing the object from the combobox that I loaded with a previous context?How do I handle this?Detach/Attach?How can I achieve the repository pattern?
Sven
EF uses the App.Config of the *current application*. That means that when you're designing the controls inside Visual Studio, it'll use devenv.exe.config. The connection isn't listed there.Also because DB access can have other side-effects (slow down the designer, cause unwanted DB queries), it's best to turn this off at design time.
Sander Rijken
Thanks for the infoCan you also give me some advice on my second question in my previous comment? Thanks
Sven
I don't have *the* answer to that, it's probably better to ask new one
Sander Rijken