SaveChanges() public méthode

This has been overridden to handle: a) Updating of modified items (see p194 in DbContext book)
public SaveChanges ( ) : int
Résultat int
        public static void ResetDatabaseToTestData(SampleWebAppDb context, string filepathOfXmlFile)
        {
            context.Posts.RemoveRange(context.Posts);
            context.Tags.RemoveRange(context.Tags);
            context.Blogs.RemoveRange(context.Blogs);
            context.PostTagGrades.RemoveRange(context.PostTagGrades);
            context.PostLinks.RemoveRange(context.PostLinks);
            context.SaveChanges();

            var loader = new LoadDbDataFromXml(filepathOfXmlFile);

            context.Blogs.AddRange(loader.Bloggers);                //note: The order things appear in the database are not obvious
            //have to add these by hand
            context.PostTagGrades.AddRange(loader.PostTagGrades);
            context.SaveChanges();
        }
        public static void ResetDatabaseToTestData(SampleWebAppDb context, string filepathOfXmlFile)
        {
            context.Posts.RemoveRange( context.Posts);
            context.Tags.RemoveRange( context.Tags);
            context.Blogs.RemoveRange( context.Blogs);
            context.PostTagGrades.RemoveRange(context.PostTagGrades);
            context.PostLinks.RemoveRange(context.PostLinks);
            context.SaveChanges();

            var loader = new LoadDbDataFromXml(filepathOfXmlFile);

            context.Blogs.AddRange(loader.Bloggers);                //note: The order things appear in the database are not obvious
            //have to add these by hand
            context.PostTagGrades.AddRange(loader.PostTagGrades);
            context.SaveChanges();
        }
 public void SetUp()
 {
     using (var db = new SampleWebAppDb())
     {
         DataLayerInitialise.InitialiseThis();
         var filepath = TestFileHelpers.GetTestFileFilePath("DbContentSimple.xml");
         DataLayerInitialise.ResetDatabaseToTestData(db, filepath);
         db.SaveChanges();
     }
 }
        public void Check01DeleteFailBecauseOfForeignKeyBad()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var post = db.Posts.First();
                db.PostLinks.Add(new PostLink {PostPart = post});
                db.SaveChanges();
            }
            
            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Posts.Remove(db.Posts.First());
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("This operation failed because another data entry uses this entry.");
            }
        }
        public void Check02UniqueKeyErrorBad()
        {
            //NOTE: To test this I needed to comment out the ValidateEntity method in SampleWebAppDb

            var tagGuid = Guid.NewGuid().ToString("N");
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid});
                db.SaveChanges();
            }

            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid });
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
        public void Check05CauseBothErrorsBad()
        {
            //NOTE: To test this I needed to comment out the ValidateEntity method in SampleWebAppDb

            var tagGuid = Guid.NewGuid().ToString("N");
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var post = db.Posts.First();
                db.PostLinks.Add(new PostLink { PostPart = post });
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid });
                db.SaveChanges();
            }

            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Posts.Remove(db.Posts.First());
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid });
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);         //for these two cases we only get one error
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }