Accessing connection string from base form causes designer error

时间秒杀一切 提交于 2019-12-11 02:56:35

问题


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()

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.

It appears that the base form is unable to access the app.config file. How can I get the base form to properly access the app.config file and make the designer work appropriately?


回答1:


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
}


来源:https://stackoverflow.com/questions/12602326/accessing-connection-string-from-base-form-causes-designer-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!