Wpf

        private Member _selectedMember;
        public Member SelectedMember
        {
            get
            {
                if (_selectedMember == null)
                {
                    try
                    {
                        _selectedMember = Members.FirstOrDefault();
                    }
                    catch(Exception)
                    {
                    }
                }
                return _selectedMember;
            }
            set
            {
                _selectedMember = value;
                OnPropertyChanged("SelectedMember");
                if (SelectedItem != null)
                {
                    IsItemAvailable(SelectedItem);
                }
                BorrowList = new ObservableCollection(context.Borrows.Where(a => a.MemberId == SelectedMember.MemberId));
                if (SelectedMember != null)
                {
                    FinedBorrows = GetFineHistoryForMember(SelectedMember);
                }
            }
        }
        private ObservableCollection _Members;
        public ObservableCollection Members
        {
            get { return _Members; }
            set
            {
                _Members = value;
                OnPropertyChanged("Members");
            }
        }
        
        #region Commands
        ICommand _NewMember;
        public ICommand NewMember
        {
            get
            {
                if (_NewMember == null)
                {
                    _NewMember = new RelayCommand(NewMemberFunction, a => true);
                }
                return _NewMember;
            }
        }
     
        ICommand _SaveMember;
        public ICommand SaveMember
        {
            get
            {
                if (_SaveMember == null)
                {
                    _SaveMember = new RelayCommand(SaveMemberFunction, a => SelectedMember!=null);
                }
                return _SaveMember;
            }
        }
   
        ICommand _DeleteMember;
        public ICommand DeleteMember
        {
            get
            {
                if (_DeleteMember == null)
                {
                    _DeleteMember = new RelayCommand(DeleteMembeFunction, a => SelectedMember!=null);
                }
                return _DeleteMember;
            }
        }
     
        private ICommand _UploadMemberPhotoCommand;
        public ICommand UploadMemberPhotoCommand
        {
            get
            {
                if (_UploadMemberPhotoCommand == null)
                {
                    _UploadMemberPhotoCommand = new RelayCommand(UploadMemberPhotoFunction, a => true);
                }
                return _UploadMemberPhotoCommand;
            }
        }
      
       
        #endregion
        #region Functions
        private void NewMemberFunction(object obj)
        {
            SelectedMember = new Member();
            if (Members.Count < 1)
            {
                SelectedMember.LibraryCardNumber = 1;
            }
            else
            {
                SelectedMember.LibraryCardNumber = Members.Max(a => a.LibraryCardNumber) + 1;
            }
            Members.Add(SelectedMember);
        }
        private void SaveMemberFunction(object obj)
        {
            try
            {
                if (SelectedMember == null)
                {
                    throw new Exception("Press new button to add new member");
                }
                if (SelectedMember.FirstName == null || SelectedMember.Roll == null)
                {
                    throw new Exception("There are some error on the form.\nPlease completely fill the form.");
                }
                if (SelectedMember.FirstName.Length < 1)
                {
                    throw new Exception("First Name is Required");
                }
                if (SelectedMember.Roll.Length < 1)
                {
                    throw new Exception("Roll Number is required");
                }
                if (SelectedMember != null)
                {
                    TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
                    if (SelectedMember.FirstName != null)
                    {
                        SelectedMember.FirstName = textInfo.ToTitleCase(SelectedMember.FirstName);
                    }
                    if (SelectedMember.MiddleName != null)
                    {
                        SelectedMember.MiddleName = textInfo.ToTitleCase(SelectedMember.MiddleName);
                    }
                    if (SelectedMember.LastName != null)
                    {
                        SelectedMember.LastName = textInfo.ToTitleCase(SelectedMember.LastName);
                    }
                    if (SelectedMember.MemberId < 1)
                    {
                        context.Members.Add(SelectedMember);
                    }
                    ValidationContext vcontext = new ValidationContext(SelectedMember);
                    List results = new List();
                    bool isValid = Validator.TryValidateObject(SelectedMember, vcontext, results);
                    if (context.SaveChanges() > 0)
                    {
                        MessageBox.Show("Saved");
                        Members = new ObservableCollection(context.Members.Take(20).OrderBy(a=>a.FirstName));
                    }
                    else
                    {
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    //sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    //eve.Entry.Entity.GetType().Name,
                    //eve.Entry.State));
                    sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().BaseType.Name,
                    eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName,
                        ve.ErrorMessage));
                    }
                }
                MessageBox.Show(sb.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void DeleteMembeFunction(object obj)
        {
            if (SelectedMember != null)
            {
                context.Members.Remove(SelectedMember);
                if (context.SaveChanges() > 0)
                {
                    Members.Remove(SelectedMember);
                }
            }
        }