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

3

6

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?

Nephritic answered 13/3, 2010 at 16:3 Comment(1)
I stopped reading as soon as I read the words "static Entity Framework class." Stop doing that. Now. The ObjectContext is not designed to be used this way.Leodora
R
9

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.

Recovery answered 13/3, 2010 at 16:9 Comment(7)
Thank you for your replies I'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!Nephritic
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: img716.imageshack.us/img716/6549/eferror.pngNephritic
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.Recovery
Thanks for the advice I 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?Nephritic
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.Recovery
Thanks for the info Can you also give me some advice on my second question in my previous comment? ThanksNephritic
I don't have the answer to that, it's probably better to ask new oneRecovery
F
2

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 !

Frumpy answered 7/4, 2011 at 5:57 Comment(1)
Thank you, man! I have just spent 3 hours, trying to understand, what i'm doing wrong. I was trying to re-create entity model, created a few test solutions... And the answer was simple: NOT TO WRITE DATABASE CODE IN USER CONTROL LOAD EVENT!Verecund
G
1

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.

Galinagalindo answered 8/11, 2015 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.