private void SaveHourCost(RoleHourCost cost, ScrumFactoryEntities context) { RoleHourCost oldCost = context.RoleHourCosts.SingleOrDefault(c => c.RoleUId == cost.RoleUId); if (oldCost == null) { context.RoleHourCosts.AddObject(cost); return; } context.AttachTo("RoleHourCosts", oldCost); context.ApplyCurrentValues <RoleHourCost>("RoleHourCosts", cost); }
public void CreateHourCosts(Project project, Project similarProject) { using (var context = new ScrumFactoryEntities(this.connectionString)) { // get similar costs RoleHourCost[] similarCosts = null; if (similarProject != null) { similarCosts = GetHourCosts(similarProject.ProjectUId); } // fore each role at this project foreach (Role r in project.Roles) { // checks if there is a role with the same shortname at the similar project, and if so, uses its costs Role similarRole = null; string roleName = r.RoleShortName.ToLower(); if (similarProject != null) { similarRole = similarProject.Roles.FirstOrDefault(sr => sr.RoleShortName.ToLower() == roleName); } RoleHourCost similarHourCost = null; if (similarRole != null && similarCosts != null) { similarHourCost = similarCosts.FirstOrDefault(c => c.RoleUId == similarRole.RoleUId); } if (similarHourCost == null) { similarHourCost = new RoleHourCost() { Price = 0, Cost = 0 } } ; // only if role is new RoleHourCost oldCost = context.RoleHourCosts.SingleOrDefault(h => h.RoleUId == r.RoleUId); if (oldCost == null) { RoleHourCost cost = new RoleHourCost() { RoleUId = r.RoleUId, ProjectUId = project.ProjectUId, Cost = similarHourCost.Cost, Price = similarHourCost.Price }; context.RoleHourCosts.AddObject(cost); } } context.SaveChanges(); } }