Example #1
0
        public void A_context_type_can_go_through_initialization_and_be_used_for_CRUD_from_multiple_threads()
        {
            // This test, unusually, modifies the database without being inside a transaction.
            // This is intentional since there are lots of threads and lots of connections working at the same
            // time here. The test is written in such a way that even if cleanup fails it should not impact
            // the test running again.
            ExecuteInParallel(
                () =>
            {
                int id;
                var name = Thread.CurrentThread.ManagedThreadId.ToString();

                using (var context = new MultiInitContextForCrud())
                {
                    var product = context.Products.Add(
                        new Product
                    {
                        Name = name
                    });
                    context.SaveChanges();
                    id = product.Id;
                    Assert.NotEqual(0, id);
                }

                using (var context = new MultiInitContextForCrud())
                {
                    var product = context.Products.Find(id);
                    Assert.Equal(name, product.Name);
                    product.Name += "_Updated!";
                    context.SaveChanges();
                }

                using (var context = new MultiInitContextForCrud())
                {
                    var product = context.Products.Where(p => p.Id == id).Single();
                    Assert.Equal(name + "_Updated!", product.Name);
                    context.Entry(product).State = EntityState.Deleted;
                    context.SaveChanges();
                }

                using (var context = new MultiInitContextForCrud())
                {
                    Assert.Null(context.Products.Find(id));
                }
            });
        }
        public void A_context_type_can_go_through_initialization_and_be_used_for_CRUD_from_multiple_threads()
        {
            // This test, unusually, modifies the database without being inside a transaction.
            // This is intentional since there are lots of threads and lots of connections working at the same
            // time here. The test is written in such a way that even if cleanup fails it should not impact
            // the test running again.
            ExecuteInParallel(
                () =>
                    {
                        int id;
                        var name = Thread.CurrentThread.ManagedThreadId.ToString();

                        using (var context = new MultiInitContextForCrud())
                        {
                            var product = context.Products.Add(
                                new Product
                                    {
                                        Name = name
                                    });
                            context.SaveChanges();
                            id = product.Id;
                            Assert.NotEqual(0, id);
                        }

                        using (var context = new MultiInitContextForCrud())
                        {
                            var product = context.Products.Find(id);
                            Assert.Equal(name, product.Name);
                            product.Name += "_Updated!";
                            context.SaveChanges();
                        }

                        using (var context = new MultiInitContextForCrud())
                        {
                            var product = context.Products.Where(p => p.Id == id).Single();
                            Assert.Equal(name + "_Updated!", product.Name);
                            context.Entry(product).State = EntityState.Deleted;
                            context.SaveChanges();
                        }

                        using (var context = new MultiInitContextForCrud())
                        {
                            Assert.Null(context.Products.Find(id));
                        }
                    });
        }