public InspectionDetail(int id, InspectionModel context = null)
        {
            InitializeComponent();

            currentPatientId = id;

            if (context == null)
            {
                context = new InspectionModel();
            }
            else
            {
                using (var db = new PatientContext())
                {
                    var ins = context;
                    context.LeftEye  = db.Eyes.Single(p => p.Id == ins.LeftEyeId);
                    context.RightEye = db.Eyes.Single(p => p.Id == ins.RightEyeId);
                }
            }

            //给context的病人对象赋值
            using (var db = new PatientContext())
            {
                var patient = db.Patients.Single(p => p.Id == id);
                context.BirthDate = patient.BirthDate;
            }

            DataContext = context;
        }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     using (var db = new PatientContext())
     {
         var i = db.Patients.Count();
         DataContext = db.Patients.OrderBy(p => p.Name).ToList();
     }
 }
        private void DgCase_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgCase.SelectedItems.Count == 0)
            {
                return;
            }

            var selectedItem = dgCase.SelectedItems[0] as PatientModel;

            using (var db = new PatientContext())
            {
                var model = db.Patients.Include("Inspections").SingleOrDefault(p => p.Id == selectedItem.Id);
                var ins   = model.Inspections;
                dgInspections.ItemsSource = ins.OrderByDescending(p => p.Id);
                if (ins.Any())
                {
                    dgInspections.SelectedIndex = 0;
                }
            }
        }
        private async void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            //因为身份证用于计算年龄,所以身份证必填且符合规范
            var m = DataContext as PatientModel;

            using (var db = new PatientContext())
            {
                if (m.Id == 0)
                {
                    db.Patients.Add(m);
                }
                else
                {
                    var modelInDb = db.Patients.Single(p => p.Id == m.Id);
                    modelInDb.GetValueOfModel(m);
                }
                db.SaveChanges();
            }

            Close();
        }
        private async void ShowChart(bool age)
        {
            if (dgCase.SelectedItems.Count <= 0)
            {
                await this.ShowMessageAsync("错误", "选择一个病例后查看");

                return;
            }

            if (dgInspections.SelectedItems.Count <= 0)
            {
                await this.ShowMessageAsync("错误", "选择一个检查记录后查看");

                return;
            }

            var selectedItem = dgCase.SelectedItems[0] as PatientModel;

            var all = DataContext as List <PatientModel>;

            using (var db = new PatientContext())
            {
                var individualModel = db.Patients.Include("Inspections").SingleOrDefault(p => p.IDCardNumber == selectedItem.IDCardNumber);

                foreach (var inspection in individualModel.Inspections)
                {
                    inspection.LeftEye  = db.Eyes.SingleOrDefault(p => p.Id == inspection.LeftEyeId);
                    inspection.RightEye = db.Eyes.SingleOrDefault(p => p.Id == inspection.RightEyeId);
                }

                var currentInspection = dgInspections.SelectedItem as InspectionModel;
                currentInspection.LeftEye  = db.Eyes.SingleOrDefault(p => p.Id == currentInspection.LeftEyeId);
                currentInspection.RightEye = db.Eyes.SingleOrDefault(p => p.Id == currentInspection.RightEyeId);

                var chart = new Chart(individualModel, currentInspection, age);
                chart.ShowDialog();
            }

            DgCase_SelectionChanged(null, null);
        }
        private async void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dgCase.SelectedItems.Count <= 0)
            {
                await this.ShowMessageAsync("错误", "选择一个病例删除");

                return;
            }



            var selectedItem = dgCase.SelectedItems[0] as PatientModel;

            using (var db = new PatientContext())
            {
                db.Patients.Attach(selectedItem);
                db.Patients.Remove(selectedItem);
                db.SaveChanges();
            }

            Window_Loaded(null, null);
        }
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            var m = DataContext as InspectionModel;

            using (var db = new PatientContext())
            {
                m.PatientId = currentPatientId;
                if (m.Id == 0)
                {
                    db.Eyes.Add(m.LeftEye);
                    db.Eyes.Add(m.RightEye);
                    db.SaveChanges();

                    m.LeftEyeId  = m.LeftEye.Id;
                    m.RightEyeId = m.RightEye.Id;

                    db.SaveChanges();
                    db.Inspections.Add(m);
                }
                else
                {
                    var modelInDb = db.Inspections.Single(p => p.Id == m.Id);
                    modelInDb.GetValueOfModel(m);

                    var leftEyeInDb = db.Eyes.Single(p => p.Id == m.LeftEye.Id);
                    //赋值
                    var rightEyeInDb = db.Eyes.Single(p => p.Id == m.RightEye.Id);
                    leftEyeInDb.GetValueOfModel(m.LeftEye);
                    rightEyeInDb.GetValueOfModel(m.RightEye);

                    modelInDb.LeftEye  = leftEyeInDb;
                    modelInDb.RightEye = rightEyeInDb;
                }
                db.SaveChanges();
            }

            Close();
        }