public static IWebHostBuilder InitSqLiteDb(this IWebHostBuilder hostBuilder, string db) { _optionsBuilder.UseSqlite(db); using (var context = new CarApiContext(_optionsBuilder.Options)) { context.Database.EnsureCreated(); context.EnsureSeedData(); } return(hostBuilder); }
public void Test1() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var options = new DbContextOptionsBuilder <CarApiContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new CarApiContext(options)) { context.Database.EnsureCreated(); } using (var context = new CarApiContext(options)) { var companyId = Guid.NewGuid(); var company = new Company { Id = companyId }; context.Companies.Add(company); var car = new Car() { Id = Guid.NewGuid(), CompanyId = company.Id, VIN = "xxx", RegNr = "ABC123" }; context.Cars.Add(car); context.SaveChanges(); } using (var context = new CarApiContext(options)) { var bc = new CarController(context); var result = bc.GetCars(); Assert.NotNull(result); Assert.Equal(result.Count(), 1); } } finally { connection.Close(); } }
public CarUnitOfWork(CarApiContext context) { _context = context; Cars = new CarRepository(_context); Companies = new CompanyRepository(_context); }
public CarRepository(CarApiContext context) : base(context) { }
public CarController(CarApiContext context) { _unitOfWork = new CarUnitOfWork(context); }
public CompanyRepository(CarApiContext context) : base(context) { }