Exemple #1
0
        private void LinqAddOrModifyUser(LDB.UserInfor UserInf)
        {
            isEFRun = false;
            AddUser addusFrm = new AddUser();

            addusFrm.SetLinqModiInfor(UserInf);
            if (UserInf == null)
            {
                if (addusFrm.ShowDialog() == DialogResult.OK)
                {
                    var usInfor = addusFrm.GetLinqUserInfor();

                    usInfor.Addresses = new EntitySet <LDB.Address>();
                    usInfor.Addresses.Add(new LDB.Address {
                        addr = "xxx", Email = "*****@*****.**", phone = "000000"
                    });
                    usInfor.Addresses.Add(new LDB.Address {
                        addr = "xxx", Email = "*****@*****.**", phone = "000000"
                    });
                    usInfor.Addresses.Add(new LDB.Address {
                        addr = "xxx", Email = "*****@*****.**", phone = "000000"
                    });

                    if (usInfor != null)
                    {
                        using (LDB.LDBContextDataContext db = GetLinqContext())
                        {
                            db.UserInfors.InsertOnSubmit(usInfor);
                            db.SubmitChanges();
                        }
                        RefreshDataByLinq();
                    }
                    else
                    {
                        AddAndShowMessages("Nothing is Add");
                    }
                }
            }
            else
            {
                //addusFrm.SetLinqModiInfor(UserInf);
                switch (addusFrm.ShowDialog())
                {
                case DialogResult.OK:
                    var uinf = addusFrm.GetLinqUserInfor();
                    using (var ldb = GetLinqContext())
                    {
                        var uif = ldb.UserInfors.Where(x => x.id == UserInf.id).FirstOrDefault();
                        if (uif != null)
                        {
                            uif.FirstName = uinf.FirstName;
                            uif.LastName  = uinf.LastName;
                            uif.Password  = uinf.Password;
                            uif.UserName  = uinf.UserName;
                            foreach (var addr in uif.Addresses)
                            {
                                addr.Email = "*****@*****.**";
                            }
                            try
                            {
                                ldb.SubmitChanges();
                            }
                            catch (ChangeConflictException e)
                            {
                                AddAndShowMessages(e.Message);
                            }
                            catch
                            { }
                        }
                    }
                    RefreshDataByLinq();
                    break;

                case DialogResult.No:
                    using (var db = GetLinqContext())
                    {
                        db.ExecuteCommand("delete from UserInfors where id={0}"
                                          , UserInf.id.ToString()
                                          );
                    }
                    RefreshDataByLinq();
                    break;

                default:
                    break;
                }
            }
        }