Example #1
0
        static void Main(string[] args)
        {
            User user = new User
            {
                UserName = "******",
                FullName = "Qwerty son"
            };

            AddUser("Admin", user);
        }
Example #2
0
        static void AddUser(string groupName, User user)
        {
            TelerikAcademyEntities context = new TelerikAcademyEntities();
            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    var userGroup = context.Groups.Where(x => x.Name == groupName).FirstOrDefault();
                    Group group;
                    if (userGroup == null)
                    {
                        group = new Group
                        {
                            Name = groupName
                        };

                        context.Groups.Add(group);
                    }
                    else
                    {
                        group = userGroup;
                    }

                    user.GroupID = group.GroupID;

                    context.Users.Add(user);
                    context.SaveChanges();
                    transaction.Complete();
                }
                catch (DbEntityValidationException)
                {
                    Console.WriteLine("Cannot add user which does not have the required columns specified");
                }
                catch (DbUpdateException ex)
                {
                    Console.WriteLine(ex.InnerException.InnerException.Message);
                }
            }
        }