// GET: Pet public ActionResult Index() { PetStoreContext context = new PetStoreContext(); List <Pet> pets = context.Pets.ToList(); return(View(pets)); }
public void AddPet(Pet pet) { using (var db = new PetStoreContext()) { db.Pets.Add(pet); db.SaveChanges(); } }
//IPetRepository _petRepository; //public TestController(IPetRepository petRepository) //{ // _petRepository = petRepository; //} // GET: Test public ActionResult Index() { var context = new PetStoreContext(); var model = context.Pet.ToList(); return(View(model)); }
static void AddProductsToDatabase() { using PetStoreContext context = new PetStoreContext(); { Product squeakyBone = new Product() { Name = "Squeaky Dog Bone", Price = 4.99M }; //We have the option of explicitly adding the object to its associated table. This is not necessary (see below) context.Products.Add(squeakyBone); Product tennisBalls = new Product() { Name = "Tennis Ball 3-Pack", Price = 9.99M }; //Here, we add the object only to the context. Entity will recognize the object and assign it to the correct table. context.Add(tennisBalls); //Commit the changes to the context. context.SaveChanges(); } }
static void ReadInformationFromDatabase() { //Query the database using Fluent API; this means using extensions to chain methods together. Here, we //also use lambda expressions to consolidate the verbiage. using PetStoreContext context = new PetStoreContext(); { //The lines below can also be written as a LINQ query: //var products = from product in context.Products // where product.Price > 5.00m // orderby product.Name // select product; var products = context.Products .Where(p => p.Price >= 5.00m) .OrderBy(p => p.Name); foreach (Product p in products) { Console.WriteLine($"Id: {p.Id}"); Console.WriteLine($"Name: {p.Name}"); Console.WriteLine($"Price: {p.Price}"); Console.WriteLine(new string('-', 20)); } } }
public IActionResult Put(int id) { using (var db = new PetStoreContext()) { db.Pets.Add(new Pet(2, "Amos", "Cat")); db.SaveChanges(); } return(Ok()); }
protected override void Up(MigrationBuilder migrationBuilder) { using (var db = new PetStoreContext()) { db.Pets.Add(new Pet(1, "Zoey", "Cat")); db.Pets.Add(new Pet(2, "Norman", "Dog")); db.SaveChanges(); } }
public PetsController(PetStoreContext storeContext) { _storeContext = storeContext; if (_storeContext.Pets.Count() == 0) { _storeContext.Pets.Add(new Pet { Name = "Cat", Family = "Cat" }); _storeContext.SaveChanges(); } }
public OwnersController(PetStoreContext context) { _storeContext = context; if (_storeContext.Owners.Count() == 0) { _storeContext.Owners.Add(new Owner { Name = "Dileep" }); _storeContext.SaveChanges(); } }
public bool DeletePet(int petId) { using (var db = new PetStoreContext()) { var toRemove = db.Pets.FirstOrDefault(p => p.Id == petId); if (toRemove != null) { db.Pets.Remove(toRemove); db.SaveChanges(); return(true); } return(false); } }
public UnitOfWork(PetStoreContext context) { _context = context; UserAddress = new UserAddressRepository(_context); Products = new ProductRepository(_context); Orders = new OrderRepository(_context); OrderItems = new OrderItemRepository(_context); ShoppingCarts = new ShoppingCartRepository(_context); ShoppingCartItems = new ShoppingCartItemRepository(_context); Pets = new PetRepository(_context); PetTypes = new PetTypeRepository(_context); }
static void EditExistingRecord() { //To edit a record, we first need to get a reference to it; this requires querying the table. using PetStoreContext context = new PetStoreContext(); { var squeakyBone = context.Products.Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); //check that a non null object was returned. if (squeakyBone is Product) { //Modify the object's data. squeakyBone.Price = 7.99m; } //Commit the changes. context.SaveChanges(); } }
static void DeleteExistingRecord() { //To edit a record, we first need to get a reference to it; this requires querying the table. using PetStoreContext context = new PetStoreContext(); { var squeakyBone = context.Products.Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); //check that a non null object was returned. if (squeakyBone is Product) { //Remove the object context.Remove(squeakyBone); Console.WriteLine("removed object."); } //Commit the changes. context.SaveChanges(); } }
public ActionResult Create(Pet pet) { if (!ModelState.IsValid) { return(View(pet)); } try { PetStoreContext context = new PetStoreContext(); context.Pets.Add(pet); context.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError("Error", ex.Message); return(View(pet)); } TempData["Message"] = "Created " + pet.Name; return(RedirectToAction("Index")); }
public PetService(PetStoreContext petContext) { this._petContext = petContext; }
public BaseRepository(PetStoreContext context, IAsyncPolicy retryPolicy) { _context = context ?? throw new System.ArgumentNullException(nameof(context)); _retryPolicy = retryPolicy ?? throw new System.ArgumentNullException(nameof(retryPolicy)); }
public ProductRepository(PetStoreContext context) : base(context) { }
public BreedService(PetStoreContext context) { this._context = context; }
public PetsController(PetStoreContext contexto, IPetRepository petRepository) { this.contexto = contexto; this.petRepository = petRepository; }
public ProviderItemRepository(PetStoreContext context) : base(context) { }
public PetRepository(PetStoreContext contexto) { this.contexto = contexto; }
public IEnumerable <Pet> GetAllPets() { using (var db = new PetStoreContext()) { return(db.Pets.ToList()); } }
public AnimalService(PetStoreContext context) { _context = context; }
public Pet GetPet(int petId) { using (var db = new PetStoreContext()) { return(db.Pets.FirstOrDefault(p => p.Id == petId)); } }
public PetsController(PetStoreContext contexto) { this.contexto = contexto; }
public OwnersController(PetStoreContext context) { _context = context; }
public PetRepository(PetStoreContext context) { _context = context; }
public PetsController(PetStoreContext context) { _context = context; }
public ShoppingCartRepository(PetStoreContext context) : base(context) { }
public OrderRepository(PetStoreContext context) : base(context) { }