从基本形式访问连接字符串会导致设计器错误 [英] Accessing connection string from base form causes designer error

查看:75
本文介绍了从基本形式访问连接字符串会导致设计器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vb.net Windows窗体应用程序,在其中我正在使用Windows窗体的继承.我有一个基本表单,提供了所有继承表单都将使用的一些基本布局和方法.这包括以基本形式 constructor 初始化实体框架上下文.

I have a vb.net windows forms application in which I'm using inheritance for the windows forms. I have a base form that provides some of the basic layout and methods that will be used with all inheriting forms. This includes the initialization of an entity framework context in the base form constructor.

Dim dataContext As New CCEntities()

然后,子窗体可以利用此上下文.该应用程序可以编译并运行,没有错误,但是当我尝试在设计器中查看子窗体时,将显示一条错误消息,指出"在配置中找不到指定的命名连接,或者不打算与指定的连接一起使用". EntitlyClient provider或无效.如果我将初始化行移到子窗体构造函数中,则设计器将正确显示.

The child form can then make use of this context. The application compiles and runs without error, but when I try to view the child form in the designer, an error is displayed stating, "The specified named connection is either not found in the configuration, not intended to be used with the EntitlyClient provider, or not valid." If I move that initialization line into the child form constructor, the designer displays without error.

为解决此问题,我在初始化时在基本表单构造函数中将连接字符串指定为:

In an attempt to troubleshoot this, I specified the connection string at initialization time in the base form constructor as:

Dim dataContext As New CCEntities(System.Configuration.ConfigurationManager.ConnectionStrings.Item("CCEntities").ConnectionString)

将该引用添加到连接字符串之后,子窗体现在显示"对象引用未设置为对象的实例."

After adding that reference to the connection string, the child form now displays "Object reference not set to an instance of an object."

但是,如果我将实体连接字符串硬编码到该初始化行中,那么设计器就可以正常工作.

However, if I hard-code the entity connection string into that initialization line, the designer works fine.

似乎基本表单无法访问app.config文件.如何获得基本表单以正确访问app.config文件并使设计器正常工作?

推荐答案

您需要做的是防止处于设计时"状态的代码运行.

What you will need to do is prevent the code from running when you are in "Design time".

if(System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
{
     //do connection stuff here
}

这篇关于从基本形式访问连接字符串会导致设计器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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