예제 #1
0
        public async void MssqlTestMethod()
        {
            //Add some monsters before querying
            _context.Add(new BackupLog {
                ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"
            });
            _context.Add(new BackupLog {
                ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"
            });
            _context.Add(new BackupLog {
                ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"
            });
            _context.Add(new BackupLog {
                ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"
            });
            _context.Add(new BackupLog {
                ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"
            });
            _context.SaveChanges();
            //Execute the query
            var res = _context.Logs.FromSql("SELECT ID, Description, ClientIP, ConfigurationID, LogError, Status, DateTime, RowVersion FROM Logs").ToList();

            //Verify the results
            Assert.Equal(5, res.Count());
        }
            public EfInMemoryContext()
            {
                options = new DbContextOptionsBuilder <BackupsContext>()
                          .UseInMemoryDatabase()
                          .Options;

                // Create the schema in the database
                using (var context = new BackupsContext(options))
                {
                    context.Database.EnsureCreated();
                }

                // Insert seed data into the database using one instance of the context
                using (var context = new BackupsContext(options))
                {
                    int id = 1;
                    context.Add(new BackupLog {
                        ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++
                    });
                    context.Add(new BackupLog {
                        ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++
                    });
                    context.Add(new BackupLog {
                        ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++
                    });
                    context.Add(new BackupLog {
                        ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++
                    });
                    context.Add(new BackupLog {
                        ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++
                    });
                    context.SaveChanges();
                }
                _context = new BackupsContext(options);
            }
        public async Task <IActionResult> Create([FromBody] BackupConfigurationDTO backupDTO)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var backup = mapper.Map <BackupConfigurationDTO, BackupConfiguration>(backupDTO);
                    _context.Add(backup);
                    var res = await _context.SaveChangesAsync();

                    if (res > 0)
                    {
                        var mapped = mapper.Map <BackupConfiguration, BackupConfigurationDTO>(backup);
                        return(new ObjectResult(mapped));
                    }
                }
                catch (Exception ex)
                {
                    this.telemetry.TrackException(ex);
                    return(BadRequest(
                               new { message = ex.Message }
                               ));
                }
            }
            else
            {
                string msg = string.Join("|", ModelState.Values.SelectMany(e => e.Errors).Select(s => s.ErrorMessage));
                return(BadRequest(msg));
            }
            return(BadRequest());
        }