public async void GetAddress() { using (var context = new AdventureWorks2014Context(_dbContextOptions)) { var addressApi = new AddressesController(context); for (int i = 0; i < 10; ++i) { Address address = new Address(); address.AddressLine1 = "Address line 1"; address.AddressLine2 = "Address line 2"; address.City = "Zagreb"; address.PostalCode = $"1234{ i }"; addressApi.PostAddress(address).Wait(); } } using (var context = new AdventureWorks2014Context(_dbContextOptions)) { var addressApi = new AddressesController(context); var result = await addressApi.GetAddress(5); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); Address address = okResult.Value as Address; Assert.NotNull(address); Assert.Equal("12344", address.PostalCode); } }
/// <summary> /// Get back all the data about the object that you may want regardless of if you actually need or use it. /// </summary> /// <param name="lastName">The last name of the person we are looking for.</param> /// <param name="firstName">The first name of the person we are looking for.</param> private static void LargeObjectWay(string lastName, string firstName) { Person person; using (var context = new AdventureWorks2014Context()) { // Write out the query & parameters so we can view everything if the debugger is attached if (System.Diagnostics.Debugger.IsAttached) { context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); } // Include all the possible user information we could ever need person = context.People .Include(p => p.Employee) .Include(p => p.BusinessEntity) .Include(p => p.BusinessEntityContacts) .Include(p => p.EmailAddresses) .Include(p => p.Password) .Include(p => p.Customers) .Include(p => p.PersonCreditCards) .Include(p => p.PersonPhones) .Single(p => p.LastName == lastName && p.FirstName == firstName); } Console.WriteLine($"Person: {person.LastName}, {person.FirstName}"); Console.WriteLine("Phone Numbers:"); foreach (var phone in person.PersonPhones) { Console.WriteLine($"....{phone.PhoneNumber}"); } }
public async void GetPerson() { using (var context = new AdventureWorks2014Context(_dbContextOptions)) { var peopleAPI = new PeopleController(context); var tmpperson = new Person(); tmpperson.PersonType = $"Tip"; tmpperson.FirstName = "Ime"; tmpperson.LastName = "Prezime"; peopleAPI.PostPerson(tmpperson).Wait(); } using (var context = new AdventureWorks2014Context(_dbContextOptions)) { var peopleAPI = new PeopleController(context); var result = await peopleAPI.GetPerson(1); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); var person = okResult.Value as Person; Assert.NotNull(person); Assert.Equal("Prezime", person.LastName); } }
public async Task <List <ProductDto> > GetList(string[] colorList, int count) { using (var dbContext = new AdventureWorks2014Context()) { var items = await dbContext.Product .Where(e => colorList.Contains(e.Color)) .Include(e => e.ProductProductPhoto) .ThenInclude(e => e.ProductPhoto) .Include(e => e.ProductSubcategory) .Take(count) .ToListAsync(); await dbContext.SaveChangesAsync(); var output = items.Select(e => new ProductDto { Id = e.ProductId, Name = e.Name, Category = e.ProductSubcategory?.Name, Color = e.Color, Weight = e.Weight, ThumbNail = e.ProductProductPhoto == null ? null : "data:image/png;base64," + Convert.ToBase64String(e.ProductProductPhoto.First().ProductPhoto.ThumbNailPhoto) }).ToList(); return(output); } }
public PeopleController(AdventureWorks2014Context context) { _context = context; _unitOfWork = new UnitOfWork(_context); _dbEmp = _unitOfWork.GenericRepository <Employee>(); _dbPer = _unitOfWork.GenericRepository <Person>(); _dbPhn = _unitOfWork.GenericRepository <PersonPhone>(); _dbEml = _unitOfWork.GenericRepository <EmailAddress>(); _dbBis = _unitOfWork.GenericRepository <BusinessEntity>(); }
public async Task <List <string> > GetColors() { using (var dbContext = new AdventureWorks2014Context()) { var items = await dbContext.Product .Select(e => e.Color) .Distinct() .ToListAsync(); return(items); } }
// GET: Products public ActionResult Category(int id, int page = 1) { using (AdventureWorks2014Context db = new AdventureWorks2014Context()) { ProductSubcategory psc = db.ProductSubcategories.Find(id); IEnumerable <Product> products = psc.Products.OrderBy(p => p.Name).Skip((page - 1) * PageCount).Take(PageCount); ViewBag.Title = psc.Name; ViewBag.ID = id; ViewBag.Page = page; ViewBag.PageMax = Math.Ceiling(psc.Products.Count() / (double)PageCount); return(View(products)); } }
public async void PostPerson() { // Koristenje InMemory baze podataka (context) using (var context = new AdventureWorks2014Context(_dbContextOptions)) { var peopleAPI = new PeopleController(context); for (int i = 0; i < 10; ++i) { var tmpperson = new Person(); tmpperson.PersonType = $"Tip { i + 1 }"; tmpperson.FirstName = "Ime" + i; tmpperson.LastName = "Prezime" + i; var result = await peopleAPI.PostPerson(tmpperson); var badRequest = result as BadRequestObjectResult; Assert.Null(badRequest); // Ako API ne vraca BadRequest, to znaci da je poziv uspjesan } } }
public async void PostAddress() { using (var context = new AdventureWorks2014Context(_dbContextOptions)) { var addressApi = new AddressesController(context); for (int i = 0; i < 10; ++i) { Address address = new Address(); address.AddressLine1 = "Address line 1"; address.AddressLine2 = "Address line 2"; address.City = "Zagreb"; address.PostalCode = $"1234{ i }"; var result = await addressApi.PostAddress(address); var badRequest = result as BadRequestObjectResult; Assert.Null(badRequest); } } }
/// <summary> /// Create multiple separate queries for the data needed but they will be smaller & simplier. /// </summary> /// <param name="lastName">The last name of the person we are looking for.</param> /// <param name="firstName">The first name of the person we are looking for.</param> private static void SeparateQueryWay(string lastName, string firstName) { using (var context = new AdventureWorks2014Context()) { // Write out the query & parameters so we can view everything if the debugger is attached if (System.Diagnostics.Debugger.IsAttached) { context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); } // Note that we aren't including any tables in this query var person = context.People .Single(p => p.LastName == lastName && p.FirstName == firstName); // As a result of not including the PersonPhones table, we need to run this part of the logic within the context so that it will not fail since it will make a repeat trip to the database. Console.WriteLine($"Person: {person.LastName}, {person.FirstName}"); Console.WriteLine("Phone Numbers:"); foreach (var phone in person.PersonPhones) { Console.WriteLine($"....{phone.PhoneNumber}"); } } }
/// <summary> /// Create a single query and only include the data that you need for the object. /// </summary> /// <param name="lastName">The last name of the person we are looking for.</param> /// <param name="firstName">The first name of the person we are looking for.</param> private static void SmallQueryWay(string lastName, string firstName) { Person person; using (var context = new AdventureWorks2014Context()) { // Write out the query & parameters so we can view everything if the debugger is attached if (System.Diagnostics.Debugger.IsAttached) { context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); } person = context.People .Include(p => p.PersonPhones) .Single(p => p.LastName == lastName && p.FirstName == firstName); } Console.WriteLine($"Person: {person.LastName}, {person.FirstName}"); Console.WriteLine("Phone Numbers:"); foreach (var phone in person.PersonPhones) { Console.WriteLine($"....{phone.PhoneNumber}"); } }
public BikesRepository(AdventureWorks2014Context context) { _context = context; }
public SalesPersonsController(AdventureWorks2014Context context) { _context = context; }
public EmployeesController(AdventureWorks2014Context context) { _context = context; }
public LookupService(AdventureWorks2014Context context) { _context = context; }
public StoresController(AdventureWorks2014Context context) { _context = context; }
public BikesService(AdventureWorks2014Context context) { _context = context; }
public PeopleController(AdventureWorks2014Context context) { _context = context; }
public PersonService() { _dbContext = new AdventureWorks2014Context(); }
public CustomersController(AdventureWorks2014Context context) { _context = context; }
public BusinessEntitiesController(AdventureWorks2014Context context) { _context = context; }
public TokenController(IConfiguration configuration, AdventureWorks2014Context context) { Configuration = configuration; _context = context; }
public UnitOfWork(AdventureWorks2014Context context) { _context = context; }
public void SetDbContext(AdventureWorks2014Context adventureWorks2014Context) { _dbContext = adventureWorks2014Context; }
/// <summary> /// Initializes a new instance of the <see cref="ProductTransactions"/> class. /// </summary> /// <param name="context">The context.</param> public ProductTransactions(AdventureWorks2014Context context) { _context = context; }
public LookupRepository(AdventureWorks2014Context context) { _context = context; }
public DataProvider(AdventureWorks2014Context context) { this.context = context; }
public ProductsController(AdventureWorks2014Context context) { _context = context; }
public AdventureWorksController(AdventureWorks2014Context dbContext) { _dbContext = dbContext; }
public TokenV1_1Controller(AdventureWorks2014Context context, IConfiguration configuration) { _context = context; Configuration = configuration; }