Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF in a UserControl can't see the app.config?

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?

like image 927
Sven Avatar asked Mar 13 '10 16:03

Sven


3 Answers

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.

like image 70
Sander Rijken Avatar answered Oct 22 '22 11:10

Sander Rijken


I faced the same problem,

In my case , I have added some database codes in user control loading event which were using some libraries, which weren't loaded till runtime.

Hence it is advisable, not to write any database code in user control load event.

Hope , this helps you !

like image 2
SHRI Avatar answered Oct 22 '22 09:10

SHRI


this error show if you call the function "LaadCbx()" on constructor of userControl.

because the initialization on entity framework exist into this function.

the solution is to call this function "LaadCbx()" in the constructor of the parent form.

like image 1
Mohamad Chami Avatar answered Oct 22 '22 09:10

Mohamad Chami