Esempio n. 1
0
        public void Deleting_object_when_relationships_have_not_been_all_enumerated_should_not_cause_collection_modified_exception_71937()
        {
            try
            {
                ProviderAgnosticConfiguration.SuspendExecutionStrategy = true;
                using (var context = new GranniesContext())
                {
                    using (context.Database.BeginTransaction())
                    {
                        var g = context.Grannys.Add(context.Grannys.Create());

                        var c = context.Children.Add(context.Children.Create());
                        c.Name = "Child";

                        var h = context.Houses.Add(context.Houses.Create());

                        g.Children.Add(c);
                        g.House = h;

                        context.SaveChanges();

                        context.Children.Remove(c); // This would previously throw

                        Assert.Equal(EntityState.Deleted, context.Entry(c).State);
                        Assert.Equal(EntityState.Unchanged, context.Entry(g).State);
                        Assert.Equal(EntityState.Unchanged, context.Entry(h).State);

                        Assert.Null(c.House);
                        Assert.Null(c.Granny);

                        Assert.Equal(0, g.Children.Count);
                        Assert.Same(h, g.House);

                        Assert.Equal(0, h.Children.Count);
                        Assert.Same(g, h.Granny);

                        context.SaveChanges();

                        Assert.Equal(EntityState.Detached, context.Entry(c).State);
                        Assert.Equal(EntityState.Unchanged, context.Entry(g).State);
                        Assert.Equal(EntityState.Unchanged, context.Entry(h).State);

                        Assert.Null(c.House);
                        Assert.Null(c.Granny);

                        Assert.Equal(0, g.Children.Count);
                        Assert.Same(h, g.House);

                        Assert.Equal(0, h.Children.Count);
                        Assert.Same(g, h.Granny);
                    }
                }
            }
            finally
            {
                ProviderAgnosticConfiguration.SuspendExecutionStrategy = false;
            }
        }
Esempio n. 2
0
        public void Deleting_object_when_relationships_have_not_been_all_enumerated_should_not_cause_collection_modified_exception_71937()
        {
            using (var context = new GranniesContext())
            {
                var g = context.Grannys.Add(context.Grannys.Create());

                var c = context.Children.Add(context.Children.Create());
                c.Name = "Child";

                var h = context.Houses.Add(context.Houses.Create());

                g.Children.Add(c);
                g.House = h;

                context.SaveChanges();

                context.Children.Remove(c); // This would previously throw

                Assert.Equal(EntityState.Deleted, context.Entry(c).State);
                Assert.Equal(EntityState.Unchanged, context.Entry(g).State);
                Assert.Equal(EntityState.Unchanged, context.Entry(h).State);

                Assert.Null(c.House);
                Assert.Null(c.Granny);

                Assert.Equal(0, g.Children.Count);
                Assert.Same(h, g.House);

                Assert.Equal(0, h.Children.Count);
                Assert.Same(g, h.Granny);

                context.SaveChanges();

                Assert.Equal(EntityState.Detached, context.Entry(c).State);
                Assert.Equal(EntityState.Unchanged, context.Entry(g).State);
                Assert.Equal(EntityState.Unchanged, context.Entry(h).State);

                Assert.Null(c.House);
                Assert.Null(c.Granny);

                Assert.Equal(0, g.Children.Count);
                Assert.Same(h, g.House);

                Assert.Equal(0, h.Children.Count);
                Assert.Same(g, h.Granny);
            }
        }