【问题标题】:Populating a listbox from database with Entity Framework使用实体框架从数据库中填充列表框
【发布时间】:2015-04-30 22:25:51
【问题描述】:

我正在开发一个 .NET 应用程序,但我遇到了问题。 我尝试使用实体框架从 MySQL 数据库填充列表框。 这是我的代码:

public MainForm()
        {
            InitializeComponent();
            try
            {
                hospitaldbEntities context = new hospitaldbEntities();
                PatientlistBoxId.DataSource = context.patients;
                PatientlistBoxId.DisplayMember = "FirstName";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

我附上来自解决方案资源管理器的图片:

最后是我的耐心课:

public partial class patient
    {
        public patient()
        {
            this.examinations = new HashSet<examination>();
            this.diseases = new HashSet<disease>();
            this.drugsensitivities = new HashSet<drugsensitivity>();
        }

        public int PatientID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public System.DateTime DateOfBirth { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }
        public System.DateTime DateOfCreate { get; set; }
        public string Category { get; set; }
        public int CNP { get; set; }

        public virtual ICollection<examination> examinations { get; set; }
        public virtual ICollection<disease> diseases { get; set; }
        public virtual ICollection<drugsensitivity> drugsensitivities { get; set; }
    }

提前感谢您的帮助。

编辑

当我尝试运行应用程序时,它会引发错误:

【问题讨论】:

  • 发生了什么?有什么错误?
  • 我添加了更多细节。
  • 异常告诉你确切地要做什么。

标签: c# .net entity-framework ado.net


【解决方案1】:

你必须至少使用:

PatientlistBoxId.DataSource = context.patients.ToList();

考虑使用BindingList<T>

顺便说一句:

考虑不使用实体类型作为 ListBox 的 ItemType。

考虑使用using 上下文来处理您的上下文,即更清楚地处理您的上下文范围。

====================

更多细节

//this class allows to separate the winform (equivalent to a view) from 
//the data layer, you can see it as a ViewModel
public class FormNamePatient {
    public Int32 Id { get; set;}
    public String FirstName { get; set;}
    public String LastName { get; set;}
}

private BindingList<FormNamePatient> _patients;
try {
   //the using guarantees the disposing of the resources 
   //(avoiding memory link and so on)
   using ( hospitaldbEntities context = new hospitaldbEntities() ) {
       _patients = new BindindList<FormNamePatient>(
           context.patients.Select(
              x => new FormNamePatient {
                  Id = x.Id,
                  FirstName = x.FirstName,
                  LastName = x.LastName,
              }).ToList() // this materialize the list
                          // in other (bad ?) words, this allows the list
                          //to live out of the context (<=> using)
       );       
   }
   PatientlistBoxId.DataSource ) = _patients;
   //now if you alter (not erase, but edit, change...) _patients you 
   //update the control
   PatientlistBoxId.DisplayMember = "FirstName";
} catch (Exception ex) {
  Console.WriteLine(ex);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多