コード例 #1
0
        public async Task <ActionResult> Generate(int count)
        {
            if (!ModelState.IsValid)
            {
                return(JsonBadRequest(new { success = false, message = "Invalid samples" }));
            }

            if (count <= 0)
            {
                return(JsonBadRequest(new { success = false, message = "count must be greater than 0" }));
            }

            int existingCount;

            using (var context = new SampleDataDbContext())
                existingCount = await context.Set <SampleData>().CountAsync();

            var samples = Enumerable.Range(existingCount, count)
                          .Select(i => new CreateSampleDataViewModel {
                Name = $"Generated sample {i}"
            });

            var client      = new HttpClient();
            var bulkUrl     = Url.Action("Bulk", "Database", null, Request.Url.Scheme);
            var json        = JsonConvert.SerializeObject(samples);
            var contentType = "application/json";
            var content     = new StringContent(json, Encoding.UTF8, contentType);

            var response = await client.PostAsync(bulkUrl, content).ConfigureAwait(false);

            var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            return(Stream(responseStream, contentType, (int)response.StatusCode));
        }
コード例 #2
0
        public async Task <ActionResult> Bulk(IEnumerable <CreateSampleDataViewModel> model)
        {
            if (!ModelState.IsValid)
            {
                return(JsonBadRequest(new { success = false, message = "Invalid samples" }));
            }

            var sampleData = model.Select(m => new SampleData {
                Name = m.Name
            });
            int changes;

            using (var context = new SampleDataDbContext())
                using (var transaction = context.Database.BeginTransaction())
                {
                    context.Configuration.AutoDetectChangesEnabled = false;
                    context.Configuration.ValidateOnSaveEnabled    = false;
                    context.Set <SampleData>().AddRange(sampleData);
                    changes = await context.SaveChangesAsync();

                    transaction.Commit();
                }

            return(Json(new { success = true, changes }));
        }
コード例 #3
0
        public HttpStatusCodeResult SimpleDbTest()
        {
            const string sampleDataName = "simple_DB_test_sample_data_name";

            using (var dbCtx = new SampleDataDbContext())
            {
                dbCtx.Set <SampleData>().Add(new SampleData {
                    Name = sampleDataName
                });
                dbCtx.SaveChanges();
            }

            using (var dbCtx = new SampleDataDbContext())
            {
                if (dbCtx.Set <SampleData>().Count() != 1)
                {
                    throw new InvalidOperationException($"dbCtx.Set<SampleData>().Count(): {dbCtx.Set<SampleData>().Count()}");
                }
                if (dbCtx.Set <SampleData>().First().Name != sampleDataName)
                {
                    throw new InvalidOperationException($"dbCtx.Set<SampleData>().First().Name: {dbCtx.Set<SampleData>().First().Name}");
                }
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
コード例 #4
0
        /// <summary>
        /// List the sample data in the database
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            List <SampleData> samples;

            using (var context = new SampleDataDbContext())
                samples = context.Set <SampleData>().ToList();

            return(View(samples));
        }
コード例 #5
0
        public async Task <ActionResult> Create(CreateSampleDataViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            int changes;

            using (var context = new SampleDataDbContext())
            {
                var sampleData = new SampleData {
                    Name = model.Name
                };
                context.Set <SampleData>().Add(sampleData);
                changes = await context.SaveChangesAsync();
            }

            AddAlert(new SuccessAlert("Sample added", $"{changes} sample was saved to the database"));
            return(RedirectToAction("Index"));
        }
コード例 #6
0
ファイル: HomeController.cs プロジェクト: one-legged/test
        public async Task <ActionResult> ConcurrentDbTest()
        {
            // Spans should overlap (actually alternatively strictly "contain" the corresponding span on the other concurrent branch)
            // [---------------- INSERT A.0 ------------------------] ...    [-- INSERT A.N.before --] [-- INSERT A.N.after --]
            //   [-- INSERT B.0.before --] [-- INSERT B.0.after --]    ... [---------------- INSERT B.N -----------------------]
            const int numberOfConcurrentIterations = ConcurrentDbTestNumberOfIterations;

            // Create table before concurrent inserts
            using (var dbCtx = new SampleDataDbContext())
            {
                if (dbCtx.Set <SampleData>().Count() != 0)
                {
                    throw new InvalidOperationException($"dbCtx.Set<SampleData>().Count(): {dbCtx.Set<SampleData>().Count()}");
                }
            }

            var syncBarrier = new Barrier(2);

            DbInterception.Add(new ConcurrentDbTestDbCommandInterceptor(syncBarrier));

            var currentTx = Agent.Tracer.CurrentTransaction;
            await Task.WhenAll(Task.Run(() => ConcurrentSpan("A", currentTx)), Task.Run(() => ConcurrentSpan("B", currentTx)));

            using (var dbCtx = new SampleDataDbContext())
            {
                var sampleDataList = dbCtx.Set <SampleData>().ToList();
                if (sampleDataList.Count != 3 * numberOfConcurrentIterations)
                {
                    throw new InvalidOperationException($"sampleDataList.Count: {sampleDataList.Count}");
                }

                for (var i = 0; i < numberOfConcurrentIterations; ++i)
                {
                    var(containingPrefix, containedPrefix) = i % 2 == 0 ? ("A", "B") : ("B", "A");
                    var expectedNames = new List <string>
                    {
                        $"{containedPrefix}.{i}.before", $"{containingPrefix}.{i}", $"{containedPrefix}.{i}.after"
                    };
                    var actualNames = new List <string> {
                        sampleDataList[3 * i].Name, sampleDataList[3 * i + 1].Name, sampleDataList[3 * i + 2].Name
                    };
                    if (!actualNames.SequenceEqual(expectedNames))
                    {
                        throw new InvalidOperationException(
                                  $"actualNames: {string.Join(", ", actualNames)}, expectedNames: {string.Join(", ", expectedNames)}");
                    }
                }
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));

            void ConcurrentSpan(string branchId, ITransaction tx)
            {
                tx.CaptureSpan(branchId, ConcurrentDbTestSpanType, () =>
                {
                    for (var i = 0; i < numberOfConcurrentIterations; ++i)
                    {
                        var isIndexEven     = i % 2 == 0;
                        var isContainedSpan = branchId == "B" ? isIndexEven : !isIndexEven;

                        var numberOfInserts = isContainedSpan ? 2 : 1;
                        for (var j = 0; j < numberOfInserts; ++j)
                        {
                            if (isContainedSpan)
                            {
                                syncBarrier.SignalAndWait();
                            }
                            using (var dbCtx = new SampleDataDbContext(/* attachedState: */ isContainedSpan))
                            {
                                var suffix = isContainedSpan ? j == 0 ? ".before" : ".after" : "";
                                dbCtx.Set <SampleData>().Add(new SampleData {
                                    Name = $"{branchId}.{i}{suffix}"
                                });
                                dbCtx.SaveChanges();
                            }
                            if (isContainedSpan)
                            {
                                syncBarrier.SignalAndWait();
                            }
                        }
                    }
                });
            }
        }