C# 从数据库设计时填充用户控件属性
我正在使用Windows窗体和SQLite数据库用C#开发一个应用程序 我正在创建的控件需要从数据库检索数据,以便它可以填充自定义属性上的可选选项。但是,当我尝试在代码中执行此操作时:C# 从数据库设计时填充用户控件属性,c#,entity-framework,sqlite,properties,user-controls,C#,Entity Framework,Sqlite,Properties,User Controls,我正在使用Windows窗体和SQLite数据库用C#开发一个应用程序 我正在创建的控件需要从数据库检索数据,以便它可以填充自定义属性上的可选选项。但是,当我尝试在代码中执行此操作时: public AddCategoryControl() { InitializeComponent(); if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
public AddCategoryControl()
{
InitializeComponent();
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
{
using (ResourceTypeRepository repo = new ResourceTypeRepository())
{
_resourceTypeList = repo.GetAllResourceTypes();
if (!Object.ReferenceEquals(ResourceTypeList[0], null))
{
ResourceType = ResourceTypeList[0];
}
}
}
}
然后我转到VisualStudio中的设计模式,我收到这个错误:“在应用程序配置文件中找不到名为‘WinterContext’的连接字符串
这对我来说没有意义,因为当我实际运行应用程序时,它可以很好地找到数据库信息。不过,作为参考,这里是我的App.Config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
<add name="WinterContext" connectionString="metadata=res://*/Data.Database.WinterEngineModel.csdl|res://*/Data.Database.WinterEngineModel.ssdl|res://*/Data.Database.WinterEngineModel.msl;provider=System.Data.SQLite;provider connection string='data source="C:\Users\Tyler\Documents\Visual Studio 2010\Projects\WinterEngine\WinterEngineToolset\Data\WinterEngineDatabase.s3db"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
我不知道该怎么办,我的搜索也没有结果。希望你们中的一位能提供帮助
提前感谢!您无法在设计时填充控件,因为连接到数据库的代码没有执行。好吧,这很有意义。因此,我想没有比硬编码值更好的选择了?假设它也无法从XML文件中读取…不是真的。您想实现什么?我很抱歉简单地尝试使选项动态化,并减少将ID转换为DTO所需的代码量。但是如果不可能,那么我将不得不使用另一种解决方案。