public Todo Add(SandboxContext db, Todo todo) { db.Todos.Add(todo); db.SaveChanges(); return(todo); }
private static void SeedTopics(this SandboxContext context) { var topics = new List <Topic>() { new Topic() { Body = "This is the body of a topic to test the thing. Plz tho.", Title = "Test the thing", Downdoots = 0, Updoots = 0 }, new Topic() { Body = "This is the body of a second topic to test the thing. Plz tho.", Title = "Test the thing 2", Downdoots = 0, Updoots = 0 }, new Topic() { Body = "This is the body of a third topic to test the thing. Plz tho.", Title = "Test the thing 3", Downdoots = 0, Updoots = 0 }, }; var existing = context.Topics.ToList(); var newTopics = new List <Topic>(); newTopics.AddRange(topics.Where(t => existing.All(e => e.Body != t.Body))); context.Topics.AddRange(newTopics); context.SaveChanges(); }
public Todo Update(SandboxContext db, Todo todo) { db.Todos.Attach(todo); db.Entry(todo).State = EntityState.Modified; db.SaveChanges(); return(todo); }
public Links UpdateLink(SandboxContext db, Links link) { db.Links.Attach(link); db.Entry(link).State = EntityState.Modified; db.SaveChanges(); return(link); }
public bool Delete(SandboxContext db, int todoId) { Todo todo = db.Todos.Where(x => x.Id == todoId).FirstOrDefault(); db.Todos.Remove(todo); db.SaveChanges(); return(true); }
public bool Delete(SandboxContext db, int linkId) { Links link = db.Links.Where(l => l.Id == linkId).FirstOrDefault(); db.Links.Remove(link); db.SaveChanges(); return(true); }
public Links AddLink(SandboxContext db, Links link) { link.CreateTimestamp = DateTime.Now; link.Visible = true; db.Links.Add(link); db.SaveChanges(); return(link); }
static void Main(string[] args) { var inMemoryContextOptions = new DbContextOptionsBuilder <SandboxContext>() .UseInMemoryDatabase(databaseName: "Test") .Options; using (var context = new SandboxContext(inMemoryContextOptions)) { var beach = new BeachEntity(); beach.Id = Guid.NewGuid(); for (int i = 0; i < 5; i++) { var grain = new SandGrainEntity { CreatedDate = DateTime.UtcNow, Id = Guid.NewGuid() }; beach.Grains.Add(grain); } context.Database.EnsureCreated(); context.Beaches.Add(beach); int savedCount = context.SaveChanges(); System.Console.WriteLine(); System.Console.WriteLine($"Created {savedCount} new entries."); System.Console.WriteLine(); WriteBeach(beach); BeachEntity clonedBeach = CloneBeach(context, beach); context.Beaches.Add(clonedBeach); savedCount = context.SaveChanges(); System.Console.WriteLine(); System.Console.WriteLine($"Created {savedCount} new entries."); System.Console.WriteLine(); WriteBeach(clonedBeach); } }
public static void Initialize(SandboxContext context) { if (context.Users.Any()) { return; } User user = new User { Name = "Ivan", Email = "*****@*****.**", Password = "******" }; context.Users.Add(user); context.SaveChanges(); }