static void Main(string[] args) { using var context = new ContosoPetsContext(); /* * var squeakyBone = new Product * { * Name = "Squeaky Dog Bone", * Price = 4.99M * }; * context.Products.Add(squeakyBone); * * var tennisBalls = new Product * { * Name = "Tennis Ball 3-Pack", * Price = 9.99M * }; * context.Add(tennisBalls); * * context.SaveChanges(); */ var products = context.Products .Where(p => p.Price >= 5.00M) .OrderBy(p => p.Name); foreach (var product in products) { Console.WriteLine($"Id:\t{product.Id}"); Console.WriteLine($"Name:\t{product.Name}"); Console.WriteLine($"Price:\t{product.Price:C2}"); Console.WriteLine(new string('-', 20)); } }
private static void UpdateProduct(ContosoPetsContext context, Product product) { if (product != null) { product.Price = 7.99m; } context.SaveChanges(); }
private static void DeleteProduct(ContosoPetsContext context, Product product) { if (product != null) { context.Remove(product); } context.SaveChanges(); }
static void Main(string[] args) { using ContosoPetsContext context = new ContosoPetsContext(); var squeakyBone = context.Products .Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); if (squeakyBone is Product) { //Edit entity records in the database //squeakyBone.Price = 7.99M; //Delete entity records from the database context.Remove(squeakyBone); } context.SaveChanges(); //Display entity records from the database 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)); } //Add New products into database //Product squeakyBone = new Product() //{ // Name = "Squeaky Dog Bone", // Price = 4.99M //}; //context.Products.Add(squeakyBone); //Product tennisBalls = new Product() //{ // Name = "Tennis Ball 3-Pack", // Price = 9.99M //}; //context.Add(tennisBalls); //context.SaveChanges(); }
private static void RetrieveProducts(ContosoPetsContext context) { 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)); } Console.WriteLine(); }
private static void DeleteRecord() { using ContosoPetsContext context = new ContosoPetsContext(); var squeakyBone = context.Products .Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); if (squeakyBone is Product) { context.Remove(squeakyBone); } context.SaveChanges(); }
private static void EditRecord() { using ContosoPetsContext context = new ContosoPetsContext(); var squeakyBone = context.Products .Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); if (squeakyBone is Product) { squeakyBone.Price = 7.99m; } context.SaveChanges(); }
static void ContextReadInfo() { using var context = new ContosoPetsContext(); var products = context.Products .Where(p => p.Price >= 5.00m) .OrderBy(p => p.Name); foreach (var product in products) { Console.WriteLine("Id: " + product.Id); Console.WriteLine("Name: " + product.Name); Console.WriteLine("Price: " + product.Price); Console.WriteLine(new string('-', 20)); } }
static void Main() { using ContosoPetsContext context = new ContosoPetsContext(); SeedDb(context); RetrieveProducts(context); var product = GetProduct(context); UpdateProduct(context, product); RetrieveProducts(context); DeleteProduct(context, product); RetrieveProducts(context); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ContosoPetsContext context) { context.Database.Migrate(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseMvc(); SeedData.Initialize(context); }
static void ContextAddInfo() { using var context = new ContosoPetsContext(); var squeakyBone = new Product() { Name = "Squeaky Bone Dog", Price = 4.99M }; var tennisBall = new Product() { Name = "Tennis ball 3-Pack", Price = 9.99M }; context.Add(squeakyBone); context.Add(tennisBall); context.SaveChanges(); }
private static void SetupDemoProducts() { using ContosoPetsContext context = new ContosoPetsContext(); Product squeakyBone = new Product() { Name = "Squeaky Dog Bone", Price = 4.99m }; context.Products.Add(squeakyBone); Product tennisBalls = new Product() { Name = "Tennis Ball 3-Pack", Price = 9.99m }; context.Add(tennisBalls); context.SaveChanges(); }
private static void DisplayProducts() { using ContosoPetsContext context = new ContosoPetsContext(); // Fluent syntax var products = context.Products .Where(p => p.Price >= 5.00m) .OrderBy(p => p.Name); // LINQ Syntax //var products = from product in context.Products // where product.Price > 5.00m // orderby product.Name // select product; 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)); } }
private static void SeedDb(ContosoPetsContext context) { context.Database.ExecuteSqlRaw("DELETE FROM dbo.Products"); context.Database.ExecuteSqlRaw("DBCC CHECKIDENT('dbo.Products', RESEED, 0)"); Product squeakyBone = new Product { Name = "Squeaky Dog Bone", Price = 4.99M }; context.Products.Add(squeakyBone); Product tennisBalls = new Product { Name = "Tennis Ball 3-Pack", Price = 9.99M }; context.Add(tennisBalls); context.SaveChanges(); }
private ContosoPetsContext GetDatabaseContext() { var options = new DbContextOptionsBuilder <ContosoPetsContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var databaseContext = new ContosoPetsContext(options); databaseContext.Database.EnsureCreated(); if (databaseContext.Products.Count() <= 0) { for (int i = 1; i <= 5; i++) { databaseContext.Products.Add( new Product( i, $"Product{i}", 0.99M + i )); databaseContext.SaveChanges(); } } return(databaseContext); }
public MyLocationAPIsController(ContosoPetsContext context) { _context = context; }
public OrderService(ContosoPetsContext context) { _context = context; }
public EditModel(ContosoPetsContext context) { _context = context; }
public OrdersController(ContosoPetsContext context) { _context = context; }
static void Main(string[] args) { //This using property makes sure the ContosoPetsContext is desposed of properly when im using it. using ContosoPetsContext context = new ContosoPetsContext(); /**************************************************************************************************** * //ADDS DATA * * //Creates the squeaky bone object. * Product squeakyBone = new Product() * { * Name = "Squeaky Dog Bone", * Price = 7.99M * }; * * //adds object to the table. * context.Products.Add(squeakyBone); * * * * Product tennisBall = new Product() * { * Name = "Tenis Ball 3-Pack", * Price = 9.99M * }; * * //Note added directly to the Context as entity knows where to add it. * context.Add(tennisBall); * * //Saves the products to the table on run time. * context.SaveChanges(); */ /**************************************************************************************************** * //UPDATES DATABASE * * var squeakyBone = context.Products * .Where(p => p.Name == "Squaky Dog Bone") * .FirstOrDefault(); * * if (squeakyBone is Product) * { * squeakyBone.Price = 7.99m; * squeakyBone.Name = "Squeaky Dog Bone"; * } * context.SaveChanges(); * * * //Deletes from DATABASE * * var squeakyBone = context.Products * .Where(p => p.Name == "Squeaky Dog Bone") * .FirstOrDefault(); * * if (squeakyBone is Product) * { * context.Remove(squeakyBone); * } * context.SaveChanges(); */ /****************************************************************************************************/ //READS FROM DATABASE /*Fulid Api Syntax * var products = context.Products * .Where(p => p.Price >= 5.00m) * .OrderBy(p => p.Name); */ var products = from product in context.Products where product.Price > 5.00m orderby product.Name select product; 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)); } /****************************************************************************************************/ }
private static Product GetProduct(ContosoPetsContext context) { return(context.Products .FirstOrDefault(p => p.Name == "Squeaky Dog Bone")); }
public IndexModel(ContosoPetsContext context) { _context = context; }
static void Main(string[] args) { // We create a new instance of ContosoPetsContext with using statement so that we ensure ContosoPetsContext objetc is disposed off properly after we're done using it. using ContosoPetsContext context = new ContosoPetsContext(); /* * // Editing Data * var squeakyBone = context.Products * .Where(p => p.Name == "Squeaky Dog Bone") * .FirstOrDefault(); // incase we don't have the record we get null * * if (squeakyBone is Product) // To check to see if squeakyBone is a Product * { * squeakyBone.Price = 7.99m; // if it is a product we set the price for 7.99m * } * context.SaveChanges(); */ // Deleting a data from the database var squeakyBone = context.Products .Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); // incase we don't have the record we get null if (squeakyBone is Product) // To check to see if squeakyBone is a Product { context.Remove(squeakyBone); // if it is a product we set the price for 7.99m } context.SaveChanges(); // LINQ method does the same thing as the Fluent API in the below example var products = from product in context.Products where product.Price > 5.00m orderby product.Name select product; /* * // Fluent APIs use extension methods that chain methods together and lamda (p=> p.) expressions to specify the query * var products = context.Products * .Where(p => p.Price >= 5.00m) * .OrderBy(p => p.Name); */ // Reading Data to console from database 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)); } /* * // Adding data to database * * Product squeakyBone = new Product() * { * Name = "Squeaky Dog Bone", * Price = 4.99M * }; * context.Products.Add(squeakyBone); * * Product tennisBalls = new Product() * { * Name = "Tennis Ball 3-Pack", * Price = 9.99M * }; * context.Add(tennisBalls); // Here we don't have to use the context.Products.Add since entity framework knows that is a product value * * context.SaveChanges(); * // now after using it we can erase and then run the code up there to call the data to our console. */ }
public CreateModel(ContosoPetsContext context) { _context = context; }
static void Main(string[] args) { using ContosoPetsContext context = new ContosoPetsContext(); var squeakyBone = context.Products .Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); if (squeakyBone is Product) { // step 3 - update the price // squeakyBone.Price = 7.99M; context.Remove(squeakyBone); } context.SaveChanges(); // step 1 - Product creation /* * Product squeakyBone = new Product() * { * Name = "Squeacky Dog Bone", * Price = 4.99M * }; * context.Products.Add(squeakyBone); * * Product tennisBalls = new Product() * { * Name = "Tennis Ball 3-Pack", * Price = 9.99M * }; * context.Add(tennisBalls); * * context.SaveChanges(); */ // end step 1 // step 2 - read and output to console. var products = context.Products .Where(p => p.Price >= 5.00M) .OrderBy(p => p.Name); /* * // alternative using LINQ. Use the one above OR below, not both * * var products = from product in context.Products * where product.Price > 5.00M * orderby product.Name * select product; * */ 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)); } // end step 2 }
public ProductsController(ContosoPetsContext context) { _context = context; }
public DeleteModel(ContosoPetsContext context) { _context = context; }
public DetailsModel(ContosoPetsContext context) { _context = context; }