1

我在使用实体框架 5 的 Windows 窗体应用程序(.net 4.0)中遇到问题。

我有一个 DbContext 实例作为类字段,格式如下:

protected MascoteAquariumEF model = new MascoteAquariumEF("name=MascoteAquariumEF");

这是我的连接字符串:

<connectionStrings>
   <add name="MascoteAquariumEF" 
     connectionString="metadata=res://*/MascoteAquariumDBContext.csdl|res://*/MascoteAquariumDBContext.ssdl|res://*/MascoteAquariumDBContext.msl;provider=System.Data.SqlClient;provider connection string='data source=vm_visual_stdo\sqlexpress2008;initial catalog=MascoteAquarium;integrated security=True;multipleactiveresultsets=True;App=EntityFramework'" 
     providerName="System.Data.EntityClient" />
</connectionStrings>

在设计器中,我看到了这个错误:表单没有得到真正的设计......但是运行项目没问题(没有错误)

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

任何想法??

4

1 回答 1

1

尝试仅传递名称值:

protected MascoteAquariumEF model = new MascoteAquariumEF("MascoteAquariumEF");

另外我认为最好在实例化上下文之前检查设计器中的表单:

protected MascoteAquariumEF model;

public YourForm()
{
    InitializeComponent();

    if (DesignMode)
        return; // you don't need to create context

    model = new MascoteAquariumEF("MascoteAquariumEF");
}
于 2012-12-15T14:14:56.823 回答