使用Entity Framework从数据库填充列表框

时间:2015-04-30 22:25:51

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

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

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

我附上解决方案资源管理器中的图片: enter image description here

最后这是我的耐心课:

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; }
    }

提前感谢您的帮助。

修改

当我尝试运行该应用程序时,它会抛出一个错误: enter image description here

1 个答案:

答案 0 :(得分: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);
}