简体   繁体   English

如何使用实体框架将数据库表内容添加到 C# 中的组合框项目?

[英]How to add a database table content to combo box items in C# using Entity Framework?

I want to make a combobox read its items from a table in a database.我想让 combobox 从数据库中的表中读取其项目。 How should I write the code in Entity Framework v6.x?我应该如何在 Entity Framework v6.x 中编写代码?

Do you need to know how to read data from a database using EF or how to populate combo box items?您是否需要知道如何使用 EF 从数据库中读取数据或如何填充组合框项? Or both?或两者? Also, it is not clear whether are you using a web, Winforms, WPF, or another technology?此外,尚不清楚您使用的是 web、Winforms、WPF 还是其他技术? I guess you can split these 2 tasks and have 2 functions:我想您可以拆分这 2 个任务并具有 2 个功能:

  1. Obtaining the items from the database(read about DbContext class)从数据库中获取项目(阅读 DbContext 类)
private List<MyItems> GetItems()
{
    List<MyItems> items = dbContext.Items.ToList();
    return items;
}
  1. Populating the combo box function填充组合框 function
private void PopulateComboBox(List<MyItems> items)
{
    foreach (var item in items)
    {
        // your code here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM