Latest Posts
Tuesday, August 3, 2021
In the last post we created our ACME Bank database in MySQL, the next step is to get the values from the Values table in the Asp.Net Core WebApi controller.
Let's create some test data so that we could retrieve the values from the database. The Values table could contain anything. So I am going to store famous philosophers throughout history. These philosophers are so famous that they only have one name:
You can run the SQL insert statement below to seed the data in MySQL:
INSERT INTO acmebank.Values ( Name ) VALUES ( 'Socrate' ), ( 'Plato' ), ( 'Spock' ), ( 'Thanos' );
So your Values table should look like this when you executed the insert query
Thursday, July 29, 2021
In this post we are going to create our first Entity Framework migration and creating an actual database in MySQL.
In order to do that we need to add a few NuGet packages to our Asp.Net Core project including the Entity Framework Core package. But, before we do that we want to find out what what version of .NET Core we are running. Obviously I have it memorized, but for the rest of you, you can type the command dotnet --version to figure out what the version is :) It's always a good idea to try and match the package version with the .NET Core runtime you have. It's one of the more annoying thing with Angular and Asp.Net Core, it's changing constantly. So as you can see my version 3.1.xxx so I should try to install packages that are made for 3.1.x.
The first package we are going to install is the Microsoft.EntityFrameworkCore. So open the ACMEBank.API project with Visual Studio Code. Press Ctrl+Shift+P and type NuGet and select
Tuesday, June 1, 2021
In the next month or so I will start a series of blog posts that will take you from scratch on how to deploy an Angular application that makes API calls from an Asp.Net Core web api backend using Entity Framework Core as it's ORM and MySQL as the database. Most tutorials would end there, but I will take it further and deploy the application on AWS so that it lives on the world wide web. Since I don't want to start from scratch I will be modernizing the ACME bank application that I've started with AngularJS. If you are working with AngularJS you know that it's time to update and upgrade because it's at its' last legs and Angular is the future. Some say the journey is more rewarding than the destination. I hope, you will join me on this journey. Thank you, here is a diagram of what's to come
Tuesday, August 27, 2019
Monday, August 26, 2019
Here are the steps to create a product repository for the products in the database:
1. Create a folder in the NorthwindCafe.Web solution call "Data"
2. Create an interface call IProductRepository in the "Data" folder, this will be the interface that we implement our repository from.
The code of the IProductRepository should look like this
using NorthwindCafe.Web.Models; using System.Collections.Generic; namespace NorthwindCafe.Web.Data { public interface IProductRepository { IEnumerable<Product> GetProducts(); } }
Friday, August 23, 2019
Here are the steps to seed the NorthwindCafe database:
1. Create a file call DBInitializer in the NorthwindCafe.Web "Models" folder, in the file type in the following code
using System.Linq; namespace NorthwindCafe.Web.Models { public class DbInitializer { public static void Initialize(NorthwindContext context) { context.Database.EnsureCreated(); if(context.Categories.Any()) { return; } var categories = new Category[] { new Category {Name = "Coffee", Description="Coffee", Products = new Product[] { new Product { Name = "Dark Roast", Description = "Dark Roast", Price = 2.0M } } }, new Category {Name = "Tea", Description="Tea", Products = new Product[] { new Product { Name = "Chai", Description = "Chai", Price = 1.5M } } }, new Category {Name = "Pastry", Description="Pastry", Products = new Product[] { new Product { Name = "Cupcake", Description = "Cupcake", Price = 1.25M } } }, new Category {Name = "Food", Description = "Food", Products = new Product[] { new Product { Name = "Hamburger", Description = "Hamburger", Price = 5.0M } } } }; foreach (var c in categories) { context.Categories.Add(c); } context.SaveChanges(); } } }
Thursday, August 22, 2019
Here are the steps to create your NorthwindCafe database:
1. Open the Startup.cs file, then type the following lines in the ConfigureServices method
var connectionString = Configuration["Data:NorthwindContextConnection"]; services.AddDbContext<NorthwindContext>(options => options.UseSqlServer(connectionString));
The line above gets the connection string from the appSettings.json file that we've created earlier. Then use the AddDbContext method in the services instance. Dependency injection will take care of the plumbing for you. Using lamba expression we tell the Entity Framework to use the Sql Sever provider for Entity Framework core.
Make sure you have the following namespaces in your Startup class
using NorthwindCafe.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
Wednesday, August 21, 2019
Follow the steps below to create the NorthwindContext:
1. Create a class in Models folder call NorthwindContext
Tuesday, August 20, 2019
Monday, April 3, 2017
Here are the steps to create the Category repository class in the NorthwindCafe application:
1. Create the ICategoryRepository interface for dependency injection, create a file call ICategoryRepository.cs in the "Models" folder with the following code
using System.Collections.Generic; namespace NorthwindCafe.Web.Models { public interface ICategoryRepository { IEnumerableGetAllCategories(); Category GetCategory(int Id); } }
Wednesday, March 22, 2017
Here are the steps to seed the NorthwindCafe database:
1. Create a file call DBInitializer in the NorthwindCafe.Web folder, in the file type in the following code
using System.Linq; namespace NorthwindCafe.Web.Models { public class DbInitializer { public static void Initialize(NorthwindContext context) { context.Database.EnsureCreated(); if(context.Categories.Any()) { return; } var categories = new Category[] { new Category {Name = "Coffee", Description="Coffee", Products = new Product[] { new Product { Name = "Dark Roast", Description = "Dark Roast", Price = 2.0M } } }, new Category {Name = "Tea", Description="Tea", Products = new Product[] { new Product { Name = "Chai", Description = "Chai", Price = 1.5M } } }, new Category {Name = "Pastry", Description="Pastry", Products = new Product[] { new Product { Name = "Cupcake", Description = "Cupcake", Price = 1.25M } } }, new Category {Name = "Food", Description = "Food", Products = new Product[] { new Product { Name = "Hamburger", Description = "Hamburger", Price = 5.0M } } } }; foreach (var c in categories) { context.Categories.Add(c); } context.SaveChanges(); } } }
Monday, August 8, 2016
Here are the steps to create your NorthwindCafe database:
1. Open the Startup.cs file, then type the following lines in the ConfigureServices method
var connectionString = Configuration["Data:NorthwindContextConnection"]; services.AddDbContext<NorthwindContext>(options => options.UseSqlServer(connectionString));
The line above gets the connection string from the appSettings.json file that we've created earlier. Then use the AddDbContext method in the services instance. Dependency injection will take care of the plumbing for you. Using lamba expression we tell the Entity Framework to use the Sql Sever provider for Entity Framework core.
Make sure you have the following namespaces in your Startup class
using NorthwindCafe.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
Friday, August 5, 2016
Here are the steps:
1. Open the project.json file
Thursday, August 4, 2016
Follow the steps below to create the NorthwindContext:
1. Create a class in Models folder call NorthwindContext