在UserControl中的EF看不到app.config? [英] EF in a UserControl can't see the app.config?

查看:187
本文介绍了在UserControl中的EF看不到app.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了一个用户控件。
此控件还使用我的静态Entity Framework类加载两个组合框。
一切都很好,运行没有问题。设计和运行时都在工作。
然后当我停止应用程序时,包含我的UserControl的所有表单在设计时不再工作。我只看到两个错误:



Error1:
配置中找不到指定的命名连接,不适用于EntityClient提供程序,或无效。



错误2:
变量 ccArtikelVelden 未声明或从未分配。
(ccArtikelVelde是我的 UserControl



运行时间仍然工作



我的静态EF Repositoy类:

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

我的UserControl中发生了一些逻辑在组合框中加载数据:

  private void LaadCbx()
{
cbxCategorie.DataSource = (从c在BSManagerData.Entities.Categories
中选择c).ToList();
cbxCategorie.DisplayMember =Naam;
cbxCategorie.ValueMember =Id;
}

private void cbxCategorie_SelectedIndexChanged(object sender,EventArgs e)
{
cbxFabrikant.DataSource =从BS中的f在BSManagerData.Entities.Fabrikants
其中f .Categorie.Id ==((Categorie)cbxCategorie.SelectedItem).Id
select f;
cbxFabrikant.DisplayMember =Naam;
cbxFabrikant.ValueMember =Id;
}

使我的表单再次工作,设计时间的唯一方法就是评论在UserControl中的EF部分(见上文)并重建。
这是非常奇怪的,一切都在同一个程序集,相同的命名空间(为了简单起见)。



任何一个想法?

解决方案

看起来你在某种程度上以设计模式执行数据库代码。为了防止这种情况,请追踪控制和方法导致这一点,并使用:

  if(DesignMode)
return

此外,静态缓存数据库上下文是一个非常糟糕的主意。这将导致多线程的问题,并且当您进行插入和删除时。数据库上下文用于单个工作单元,正在添加2,并删除3个其他对象,并调用 SaveChanges 一次。


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?

解决方案

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.

这篇关于在UserControl中的EF看不到app.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆