示例#1
0
        public void ShouldThrowOnDuplicateAddObject(TestIdentityObject obj, ChangeType changeTypeOne, ChangeType changeTypeTwo)
        {
            // Arrange
            var sut = new Ltr();

            // Act
            sut.AddObject(obj, changeTypeOne);

            // Arrange
            Assert.Throws <ArgumentException>(() => sut.AddObject(obj, changeTypeTwo));
        }
示例#2
0
        public void ShouldAddObject(TestIdentityObject obj, ChangeType changeType)
        {
            // Arrange
            var sut = new Ltr();

            // Act
            sut.AddObject(obj, changeType);

            // Arrange
            var ltrObj = Assert.Single(sut.Objects);

            Assert.Equal(obj.IntProperty.ToString(), ltrObj[nameof(obj.IntProperty)].Value);
            Assert.Equal(obj.StringProperty, ltrObj[nameof(obj.StringProperty)].Value);
            Assert.Equal(changeType, ltrObj.ChangeType);
        }
        private void SetupDeletedObjects(Ltr ltr)
        {
            var tms = DateTimeOffset.UtcNow;

            foreach (var entry in this.ChangeTracker.Entries()
                     .Where(e => e.State == EntityState.Deleted)
                     .Select(e => e.Entity)
                     .OfType <BaseEntity>())
            {
                if (entry.ShouldSerialize())
                {
                    ltr.AddObject(entry, ChangeType.Delete);
                }
            }
        }
        private void SetupChangedObjects(Ltr ltr)
        {
            var tms = DateTimeOffset.UtcNow;

            foreach (var entry in this.ChangeTracker.Entries()
                     .Where(e => e.State == EntityState.Modified)
                     .Select(e => e.Entity)
                     .OfType <BaseEntity>())
            {
                entry.SetProperty(e => e.WhenChanged, tms);
                if (entry.ShouldSerialize())
                {
                    ltr.AddObject(entry, ChangeType.Update);
                }
            }
        }
        public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
        {
            var ltr = new Ltr();

            this.ChangeTracker.DetectChanges();

            this.SetupNewObjects(ltr);
            this.SetupChangedObjects(ltr);
            this.SetupDeletedObjects(ltr);

            await this.Repository <Ltr>().AddAsync(ltr, cancellationToken);

            this.ChangeTracker.DetectChanges();

            this.ChangeTracker.AutoDetectChangesEnabled = false;
            var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);

            this.ChangeTracker.AutoDetectChangesEnabled = true;

            return(result);
        }