static void Main(string[] args) { using (var softUniEntities = new SoftUniEntities()) { using (var dbContextTransaction = softUniEntities.Database.BeginTransaction()) { try { var employees = new List<Employee> { new Employee { FirstName = "Gosho", Address = new Address { AddressText = "str. Georgi rakovski 21", Town = new Town { Name = "Kaspichan2" } }, LastName = "Goshev", MiddleName = "Goshev", DepartmentID = softUniEntities .Departments .FirstOrDefault(d => d.Name == "Engineering") .DepartmentID, JobTitle = "Momche za vsichko", HireDate = DateTime.Now } }; var project = new Project { // Uncomment this to add project to database. // Name = "Test Project", Description = "My best project", Employees = employees, StartDate = DateTime.Now }; softUniEntities.Projects.Add(project); softUniEntities.SaveChanges(); dbContextTransaction.Commit(); Console.WriteLine("Project has been added to database."); } catch (Exception e) { dbContextTransaction.Rollback(); Console.WriteLine("Project is not added to database. See Exception: \n " + e.Message); } } } }
public static void RemoveTown(int townId) { var softUniEntities = new SoftUniEntities(); var town = softUniEntities.Towns.Find(townId); var removedTownName = town.Name; softUniEntities.Towns.Remove(town); softUniEntities.SaveChanges(); Console.WriteLine(removedTownName + " is removed!"); }
public static void ChangeTownName(int townId, string newTownName) { var sofUniEntities = new SoftUniEntities(); var town = sofUniEntities.Towns.Find(townId); var oldTownName = town.Name; town.Name = newTownName; sofUniEntities.SaveChanges(); Console.WriteLine(oldTownName + " now is: " + town.Name); }
static void Main(string[] args) { var softUniEntities = new SoftUniEntities(); var concurrencySoftUniEntities = new SoftUniEntities(); var guy = softUniEntities.Employees.FirstOrDefault(e => e.FirstName == "Roberto"); var guy2 = concurrencySoftUniEntities.Employees.FirstOrDefault(e => e.FirstName == "Roberto"); guy.FirstName = "Gosho"; guy2.FirstName = "Pesho"; softUniEntities.SaveChanges(); concurrencySoftUniEntities.SaveChanges(); }
public static int InsertTown(string name) { var softuniEntities = new SoftUniEntities(); var town = new Town { Name = name }; softuniEntities.Towns.Add(town); softuniEntities.SaveChanges(); Console.WriteLine("Town " + name + " inserted!"); return town.TownID; }