public static User CreateNewUser(EfModelContainer db) { User user = new User(); // it named "User" cause f##king model refuses to work properly try { Console.WriteLine("Enter name of employee"); user.FirstName = Console.ReadLine(); Console.WriteLine("Enter last name of employee"); user.LastName = Console.ReadLine(); Console.WriteLine("Enter Email"); user.Email = Console.ReadLine(); Console.WriteLine("Enter department name of employee"); string employeeDepartment = Console.ReadLine(); //if (db.Departments.) //{ // } //var createdEmployee = db.Departments. //here mb needed to add checking for not null // Where(c => c.Name == employeeDepartment). // FirstOrDefault(); // user.Department = createdEmployee; user.Department = new Department() { Name = employeeDepartment, }; } catch (Exception e) { Console.WriteLine(e); Console.WriteLine("Something went wrong when create an employee"); } return(user); }
public static void AddNewEmploye(EfModelContainer db) { db.Users.Add(CreateNewUser(db)); }
static void Main(string[] args) { var db = new EfModelContainer(); Console.WriteLine("Enter admin department name"); Department department1 = new Department { Name = Console.ReadLine() }; var chiefShepherd = new Admin() { FirstName = "Boss", LastName = "Super", Email = "*****@*****.**", Level = 100500, Department = department1, //and Birthday }; db.Departments.Add(department1); db.SaveChanges(); CreateNewEmploee: Console.WriteLine("Would you like to create new Employe?"); Console.WriteLine("Enter Y or N"); string ch = Console.ReadLine(); if ((ch == "y") | (ch == "Y")) { AddNewEmploye(db); } else if (!((ch == "n") | (ch == "N")) && !((ch == "y") | (ch == "Y"))) //not Yes or No { Console.WriteLine("You entered an invalid character"); goto CreateNewEmploee; //CHANGE TO IF-ELSE } try { db.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult validationError in ex.EntityValidationErrors) { Console.Write("Object: " + validationError.Entry.Entity.ToString()); Console.Write(" "); foreach (DbValidationError err in validationError.ValidationErrors) { Console.WriteLine(err.ErrorMessage + ""); } } } catch (Exception ex) { Console.Write("Object: " + ex.ToString()); } Console.WriteLine(new string('-', 50)); Console.WriteLine("chiefShepherd is: " + chiefShepherd.FirstName); foreach (var department in db.Departments) { Console.WriteLine("The following employees work in the " + department.Name + " department:"); foreach (var user in department.Users) { Console.WriteLine("\t" + user.FirstName + " " + user.LastName + " " + user.Email); } } //db.Departments.RemoveRange(db.Departments); //db.Users.RemoveRange(db.Users); try { db.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult validationError in ex.EntityValidationErrors) { Console.Write("Object: " + validationError.Entry.Entity.ToString()); Console.Write(" "); foreach (DbValidationError err in validationError.ValidationErrors) { Console.Write(err.ErrorMessage + " "); Console.ReadKey(); } } } Console.ReadKey(); }