Beispiel #1
0
        private static void AddUser() // method for defined add new User to the database
        {
            Form1 form = new Form1();

            form.SendingData("introduce User's Name\n"); // call method SendingData for send current string
            var    userName = form.ReceivingData();      // call method Receiving for got data from client part
            string userN    = userName.ToString();       // convert get data to the type string

            form.SendingData("introduce User's Age");    // call method SendingData for send current string
            var userAge = form.ReceivingData();          // call method ReceivingData for get all data from client part
            int userA   = Int32.Parse(userAge);          // convert get data in the int type

            using (UserContext db = new UserContext())   // defined variable about class UserContext
            {
                User newUser = new User {
                    Name = userN, Age = userA
                };                                     // defined instance with got data

                db.Users.Add(newUser);                 // choice current object and add to the database
                db.SaveChanges();                      // save new data in the database
                form.SendingData("command performed"); // call method SendingData for send current string

                var users = db.Users;                  // got all users from the database

                form.UsersEvent += form.ShowUsers;     // defined current method 'ShowUsers' for event - 'UsersEvent'
                if (form.UsersEvent != null)           // if event have method for performing
                {
                    form.UsersEvent(users);            // call current event
                }
            }
        }