private static void Seed(ProjectsDbContext db) { if (db.Users.Any()) { return; } var user = new User() { Email = "*****@*****.**", Name = "Oscar" }; db.Users.Add(user); var team = new Team() { Name = "Mega team", Members = new[] { user } }; db.Teams.Add(team); var project = new Project() { Description = "test project", Team = team }; db.Projects.Add(project); db.SaveChanges(); }
private static void PopulateDb() { using (var ctx = new ProjectsDbContext()) { Project p1 = ctx.Projects.Add(new Project()).Entity; Project p2 = ctx.Projects.Add(new Project()).Entity; ctx.Subprojects.Add(new Subproject() { Area = 100, Project = p1 }); ctx.Subprojects.Add(new Subproject() { Area = 200, Project = p1 }); ctx.Subprojects.Add(new Subproject() { Area = 350, Project = p2 }); ctx.Subprojects.Add(new Subproject() { Area = 450, Project = p2 }); ctx.Subprojects.Add(new Subproject() { Area = 10000, Project = p2 }); ctx.SaveChanges(); } }
public void Save() { lock (contextLock) //lock для сохранения данных в бд { context.SaveChanges(); } }
private void PopulateDb(ProjectsDbContext ctx) { User user1 = ctx.Users.Add(new User { Name = "user1" }).Entity; User user2 = ctx.Users.Add(new User { Name = "user2" }).Entity; User user3 = ctx.Users.Add(new User { Name = "user3" }).Entity; User user4 = ctx.Users.Add(new User { Name = "user4" }).Entity; ctx.SaveChanges(); Project p1 = ctx.Projects.Add(new Project { CreatedBy = user1, ModifiedBy = user3 }).Entity; Project p2 = ctx.Projects.Add(new Project { CreatedBy = user2, ModifiedBy = user4 }).Entity; ctx.Subprojects.Add(new Subproject { Area = 100, Project = p1 }); ctx.Subprojects.Add(new Subproject { Area = 200, Project = p1 }); ctx.Subprojects.Add(new Subproject { Area = 350, Project = p2 }); ctx.Subprojects.Add(new Subproject { Area = 450, Project = p2 }); ctx.Subprojects.Add(new Subproject { Area = 10000, Project = p2 }); ctx.SaveChanges(); }
private static void ClearDb() { using (var ctx = new ProjectsDbContext()) { foreach (var subproject in ctx.Subprojects) { ctx.Subprojects.Remove(subproject); } foreach (var project in ctx.Projects) { ctx.Projects.Remove(project); } ctx.SaveChanges(); } }
private void SeedData(ProjectsDbContext context) { if (context.ProjectItems.Any()) { return; // DB has been seeded } var p1 = new ProjectItem { ParentId = null, Name = "1. Project 1", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 30), Type = ItemType.Project }; context.ProjectItems.Add(p1); context.SaveChanges(); var p11 = new ProjectItem { ParentId = p1.Id, Name = "1.1. SubProject 1", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Project }; context.ProjectItems.Add(p11); context.SaveChanges(); var p111 = new ProjectItem { ParentId = p11.Id, Name = "1.1.1. Task 1", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Task }; var p112 = new ProjectItem { ParentId = p11.Id, Name = "1.1.2. Task 2", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Task }; var p113 = new ProjectItem { ParentId = p11.Id, Name = "1.1.3. Task 3", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Task }; context.ProjectItems.Add(p111); context.ProjectItems.Add(p112); context.ProjectItems.Add(p113); context.SaveChanges(); var p1131 = new ProjectItem { ParentId = p113.Id, Name = "1.1.3.1 Task 3.1", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Task }; context.ProjectItems.Add(p1131); context.SaveChanges(); var p12 = new ProjectItem { ParentId = p1.Id, Name = "1.2. SubProject 2", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Project }; var p13 = new ProjectItem { ParentId = p1.Id, Name = "1.3. SubProject 3", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Project }; context.ProjectItems.Add(p12); context.ProjectItems.Add(p13); context.SaveChanges(); var p131 = new ProjectItem { ParentId = p13.Id, Name = "1.3.1. Task 1", StartDate = new DateTime(2020, 06, 01), EndDate = new DateTime(2020, 06, 10), Type = ItemType.Task }; context.ProjectItems.Add(p131); context.SaveChanges(); }