public void DeleteAll_DateIsInRange_DeletesAllMatchesAndNothingElse() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); db.BlogPosts.Add(BlogPost.Create("T1", DateTime.Today.AddDays(-2))); db.BlogPosts.Add(BlogPost.Create("T2", DateTime.Today.AddDays(0))); db.BlogPosts.Add(BlogPost.Create("T3", DateTime.Today.AddDays(2))); db.SaveChanges(); } using (var db = Context.Sql()) { var lower = DateTime.Today.AddDays(-1); var upper = DateTime.Today.AddDays(1); var count = EFBatchOperation.For(db, db.BlogPosts).Where(b => b.Created <upper && b.Created> lower).Delete(); Assert.AreEqual(1, count); } using (var db = Context.Sql()) { var posts = db.BlogPosts.ToList(); Assert.AreEqual(2, posts.Count); Assert.AreEqual(0, posts.Count(p => p.Title == "T2")); } }
public void InsertAll_InsertsItems() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); var list = new List <BlogPost>() { BlogPost.Create("T1"), BlogPost.Create("T2"), BlogPost.Create("T3") }; EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); } using (var db = Context.Sql()) { Assert.AreEqual(3, db.BlogPosts.Count()); Assert.AreEqual("*****@*****.**", db.BlogPosts.First().Author.Email); } }
public void DeleteAll_DateIsSmallerThan_DeletesAllMatchesAndNothingElse() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); db.BlogPosts.Add(BlogPost.Create("T1", DateTime.Today.AddDays(-2))); db.BlogPosts.Add(BlogPost.Create("T2", DateTime.Today.AddDays(-1))); db.BlogPosts.Add(BlogPost.Create("T3", DateTime.Today.AddDays(1))); db.SaveChanges(); } using (var db = Context.Sql()) { var limit = DateTime.Today; var count = EFBatchOperation.For(db, db.BlogPosts).Where(b => b.Created < limit).Delete(); Assert.AreEqual(2, count); } using (var db = Context.Sql()) { var posts = db.BlogPosts.ToList(); Assert.AreEqual(1, posts.Count); Assert.AreEqual("T3", posts.First().Title); } }
public void DeleteAll_PropertyEquals_DeletesAllMatchesAndNothingElse() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); db.BlogPosts.Add(BlogPost.Create("T1")); db.BlogPosts.Add(BlogPost.Create("T2")); db.BlogPosts.Add(BlogPost.Create("T2")); db.BlogPosts.Add(BlogPost.Create("T3")); db.SaveChanges(); } using (var db = Context.Sql()) { var count = EFBatchOperation.For(db, db.BlogPosts).Where(b => b.Title == "T2").Delete(); Assert.AreEqual(2, count); } using (var db = Context.Sql()) { var posts = db.BlogPosts.ToList(); Assert.AreEqual(2, posts.Count); Assert.AreEqual(0, posts.Count(p => p.Title == "T2")); } }
public void InsertAll_WrongColumnOrder_InsertsItems() { using (var db = new ReorderedContext()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); } using (var db = Context.Sql()) { var list = new List <BlogPost>() { BlogPost.Create("T1"), BlogPost.Create("T2"), BlogPost.Create("T3") }; EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); } using (var db = Context.Sql()) { Assert.AreEqual(3, db.BlogPosts.Count()); } }
public void InsertAll_NoProvider_UsesDefaultInsert() { string fallbackText = null; Configuration.DisableDefaultFallback = false; Configuration.Log = str => fallbackText = str; using (var db = Context.SqlCe()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); } var list = new List <BlogPost>() { BlogPost.Create("T1"), BlogPost.Create("T2"), BlogPost.Create("T3") }; using (var db = Context.SqlCe()) { EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); } using (var db = Context.SqlCe()) { Assert.AreEqual(3, db.BlogPosts.Count()); } Assert.IsNotNull(fallbackText); }
public void Merge_With_Specific_Update_Column() { Setup(); using (var db = Context.Sql()) { var posts = db.BlogPosts.ToList(); foreach (var post in posts) { post.Title = post.Title.Replace("1", "4").Replace("2", "8").Replace("3", "12"); post.Reads = 99; } var insert = BlogPost.Create("TNew"); insert.Reads = 99; posts.Add(insert); EFBatchOperation.For(db, db.BlogPosts).MergeAll(posts, null, c => c.ColumnsToUpdate(p => p.Reads)); } using (var db = Context.Sql()) { var posts = db.BlogPosts.OrderBy(b => b.ID).ToList(); posts.ForEach(p => Assert.AreEqual(99, p.Reads)); Assert.AreEqual("T1", posts[0].Title); Assert.AreEqual("T2", posts[1].Title); Assert.AreEqual("T3", posts[2].Title); Assert.AreEqual("TNew", posts[3].Title); } }
public void Merge_With_Condition() { Setup(); using (var db = Context.Sql()) { var posts = db.BlogPosts.ToList(); foreach (var post in posts) { post.Title = post.Title.Replace("1", "4").Replace("2", "8").Replace("3", "12"); } var insert = BlogPost.Create("TNew"); posts.Add(insert); EFBatchOperation.For(db, db.BlogPosts).MergeAll(posts, c => c.ColumnsToIdentity(p => p.ID)); } using (var db = Context.Sql()) { var posts = db.BlogPosts.OrderBy(b => b.ID).ToList(); Assert.AreEqual("T4", posts[0].Title); Assert.AreEqual("T8", posts[1].Title); Assert.AreEqual("T12", posts[2].Title); Assert.AreEqual("TNew", posts[3].Title); } }
public void InsertAll_WithExplicitConnection_InsertsItems() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); var list = new List <BlogPost> { BlogPost.Create("T1"), BlogPost.Create("T2"), BlogPost.Create("T3") }; EFBatchOperation.For(db, db.BlogPosts).InsertAll(list, db.Database.Connection); } using (var db = Context.Sql()) { Assert.AreEqual(3, db.BlogPosts.Count()); } }
private static void SetupBasePosts() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); var p = BlogPost.Create("T1"); p.Reads = 2; db.BlogPosts.Add(p); db.BlogPosts.Add(BlogPost.Create("T2")); db.SaveChanges(); } }
private static void Setup() { using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.ForceDelete(); } db.Database.Create(); var list = new List <BlogPost> { BlogPost.Create("T1"), BlogPost.Create("T2"), BlogPost.Create("T3") }; EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); } }
public void DeleteAll_NoProvider_UsesDefaultDelete() { string fallbackText = null; Configuration.DisableDefaultFallback = false; Configuration.Log = str => fallbackText = str; using (var db = Context.SqlCe()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); db.BlogPosts.Add(BlogPost.Create("T1", DateTime.Today.AddDays(-2))); db.BlogPosts.Add(BlogPost.Create("T2.0", DateTime.Today.AddDays(0))); db.BlogPosts.Add(BlogPost.Create("T2.1", DateTime.Today.AddDays(0))); db.BlogPosts.Add(BlogPost.Create("T3", DateTime.Today.AddDays(2))); db.SaveChanges(); } using (var db = Context.SqlCe()) { var lower = DateTime.Today.AddDays(-1); var upper = DateTime.Today.AddDays(1); var count = EFBatchOperation.For(db, db.BlogPosts).Where(b => b.Created <upper && b.Created> lower && b.Title == "T2.0").Delete(); Assert.AreEqual(1, count); } using (var db = Context.SqlCe()) { Assert.AreEqual(3, db.BlogPosts.Count()); } Assert.IsNotNull(fallbackText); }
public void InsertAll_WithForeignKey() { int postId; using (var db = Context.Sql()) { if (db.Database.Exists()) { db.Database.Delete(); } db.Database.Create(); var bp = BlogPost.Create("B1"); db.BlogPosts.Add(bp); db.SaveChanges(); postId = bp.Id; var comments = new List <Comment> { new Comment { Text = "C1", PostId = bp.Id }, new Comment { Text = "C2", PostId = bp.Id } }; EFBatchOperation.For(db, db.Comments).InsertAll(comments); } using (var db = Context.Sql()) { Assert.AreEqual(2, db.Comments.Count()); Assert.AreEqual(2, db.Comments.Count(c => c.PostId == postId)); } }
private static void CreateSmallTestSet(Context db) { db.Contacts.Add(new Contact { FirstName = "FN1", LastName = "LN1", Title = "Director", Id = Guid.NewGuid(), BirthDate = DateTime.Today, PhoneNumbers = new List <PhoneNumber> { new PhoneNumber { Id = Guid.NewGuid(), Number = "10134" }, new PhoneNumber { Id = Guid.NewGuid(), Number = "15678" } } }); db.Contacts.Add(new Contact { FirstName = "FN2", LastName = "LN2", Title = "Associate", Id = Guid.NewGuid(), BirthDate = DateTime.Today, PhoneNumbers = new List <PhoneNumber> { new PhoneNumber { Id = Guid.NewGuid(), Number = "20134" }, new PhoneNumber { Id = Guid.NewGuid(), Number = "25678" } }, Emails = new List <Email> { new Email { Id = Guid.NewGuid(), Address = "*****@*****.**" }, new Email { Id = Guid.NewGuid(), Address = "*****@*****.**" } } }); db.Contacts.Add(new Contact { FirstName = "FN3", LastName = "LN3", Title = "Vice President", Id = Guid.NewGuid(), BirthDate = DateTime.Today, Emails = new List <Email> { new Email { Id = Guid.NewGuid(), Address = "*****@*****.**" }, new Email { Id = Guid.NewGuid(), Address = "*****@*****.**" } } }); var blogPost1 = BlogPost.Create("BP1"); blogPost1.Comments = new List <Comment> { new Comment { Text = "C1" } }; db.BlogPosts.Add(blogPost1); var blogPost2 = BlogPost.Create("BP2"); blogPost2.Comments = new List <Comment> { new Comment { Text = "C2" }, new Comment { Text = "C3" } }; db.BlogPosts.Add(blogPost2); var blogPost3 = BlogPost.Create("BP3"); blogPost3.Comments = new List <Comment> { new Comment { Text = "C4" }, new Comment { Text = "C5" }, new Comment { Text = "C6" } }; db.BlogPosts.Add(blogPost3); db.SaveChanges(); }