![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_1f841115-1126-4f9b-b269-58086cf81d98.png)
You can create database driven .Net Core applications using JavaScriptServices, and PrimeNg.
Live Example
You can see the application running live on Microsoft Azure at: http://helloworlddata.azurewebsites.net
YouTube Video
![image image](1176262/Windows-Live-Writer-Hello-World-Angular.Net-Core-and-PrimeNg_144D7-image_827fa5de-0d25-47ce-9bbb-ede5f8d6a67d.png)
You can see the YouTube video that covers all the content of this Blog here: https://www.youtube.com/watch?v=i6ke21P-fgA
Prerequisites
If you do not already have them, install the following prerequisites:
Create JavaScriptServices project
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_950cdb33-6674-489a-87c4-911321bc256d.png)
Create a folder on your Microsoft Windows computer (this tutorial was created using Windows 10).
Note: Do not have any special characters in the folder name. For example, and exclamation mark (!) will break the JavaScript code.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_b03d4a67-f1a1-41d4-81ee-b7f423d82b37.png)
You can type CMD and press Enter to switch to the command line (and still be in that directory).
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_13ab9f2a-5107-49d1-a917-efd4417933cb.png)
If you have not already installed JavaScriptServices, install them by entering (and pressing Enter):
dotnet --install Microsoft.AspNetCore.SpaTemplates::*
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_396df77a-cf19-45d1-b9c4-3ea09d1f976e.png)
The screen will display indicating the templates now available.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_baa58b8f-25fe-4633-b6c2-db6ffcbe4427.png)
Create the ASP.NET Core JavaScriptServices application by entering (and pressing Enter):
dotnet new angular
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_b1a36342-ebd2-4a9c-b691-3c8775067ab1.png)
The application will be created.
Double-click on the *.csproj file to open it in Visual Studio 2017.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_5f799a96-695e-48aa-a7c5-66274a341288.png)
It will start restoring .Net dependencies and the node_modules (you can view the ../node_modules directory to see the items populated).
(Note: This can take 3-10 minutes or more)
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_a91ee5aa-5ebd-48c8-9530-aa8a5f5e623b.png)
Visual Studio will indicate when everything is complete.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_4d6a9353-93d1-4e4b-b3bb-3cec3fd2167c.png)
Press Ctrl+F5 to Start Without Debugging.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_895f2213-ac5c-43a0-b429-1adf1b716cf8.png)
The application will Build.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_3da7d86a-eb09-4ae0-bf5b-9e743689dba6.png)
The application will show.
Close the web browser for now.
Add PrimeNG
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_eb0efaaf-ac3b-4882-850a-a848202b7618.png)
We will now install the free open source PrimeNG Angular 2 components.
This will demonstrate how to integrate most Angular 2+ libraries with JavaScriptServices.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_e93cf113-68d4-4fbb-9cb2-fc1ad500a5da.png)
Open the package.json file and add:
"font-awesome": "^4.7.0",
"primeng": "^2.0.0"
Save the file.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_c5abe7b2-45ff-4462-8ebe-749a1b71c758.png)
Open the webpack.config.vendor.js file and add:
'font-awesome/css/font-awesome.css',
'primeng/primeng',
'primeng/resources/themes/omega/theme.css',
'primeng/resources/primeng.min.css',
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_b3abb75d-2b06-46d0-aaa5-a27c6757eb80.png)
Also, in rules/test, add:
|gif
Save the file.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_17b8bb90-0a5b-4e50-99b9-e3c5cb39b4ef.png)
At this time, PrimeNg does not support pre-rendering so in ..\Views\Home\Index.cshtml, change:
<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>
to:
<app>Loading...</app>
Save the file.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_ef34afe3-ab23-44da-b421-0bc7177f157f.png)
We altered the webpack.config.vendor.js file (to add PrimeNg and Font Awesome) but it is not updated by the normal build process. We have to run its configuration manually whenever we alter it.
In a command prompt, at the project root, run:
webpack --config webpack.config.vendor.js
(Note: If you don’t already have the webpack tool installed (for example you get an error when you run the code above), you’ll need to run: "npm install -g webpack" first)
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_032c27ad-88a1-43a0-b749-a563189df823.png)
Create a folder called prime in the components folder and add the following code to ..\ClientApp\app\components\prime\prime.component.html:
<H1>Counter</H1>
<p>This is a simple example of an Angular 2 component.</p>
<p>Current count: <strong>{{ currentCount }}</strong></p>
<p-growl [value]="msgs"></p-growl>
<button pButton
type="button"
(click)="incrementCounter()"
label="Increment"
icon="fa-check"
class="ui-button-info">
</button>
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_c6ee662d-03aa-4ae9-8f6a-bdf5f229d4eb.png)
Create the file and add the following code to ..\ClientApp\app\components\prime\prime.component.ts:
import { Component } from '@angular/core';
import {
ButtonModule,
GrowlModule,
Message
} from 'primeng/primeng';
@Component({
selector: 'counter',
templateUrl: './prime.component.html'
})
export class PrimeComponent {
public currentCount = 0;
public msgs: Message[] = [];
public incrementCounter() {
this.currentCount++;
this.msgs.push(
{
severity: 'info',
summary: 'Info Message',
detail: this.currentCount.toString()
});
}
}
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_8a62e7f4-dce1-459c-bbac-50c5954c0d3e.png)
Alter the file at: ..\ClientApp\app\app.module.ts to add:
import { FormsModule } from '@angular/forms';
import { PrimeComponent } from './components/prime/prime.component';
import { ButtonModule, GrowlModule } from 'primeng/primeng';
@NgModule({
declarations: [
...
PrimeComponent
],
imports: [
RouterModule.forRoot([
...
{ path: 'prime', component: PrimeComponent },
...
]),
FormsModule,
ButtonModule,
GrowlModule
]
})
...
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_8d37831d-634e-4b4d-8f77-1ed9a149dbee.png)
Add the following code to ..\ClientApp\app\components\navmenu\navmenu.component.html:
<li [routerLinkActive]="['link-active']">
<a [routerLink]="['/prime']">
<span class='glyphicon glyphicon-th-list'></span> Prime
</a>
</li>
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_611c1f86-4c58-481d-9ec7-d2359b1e9b07.png)
Press Ctrl+F5 to Start Without Debugging.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_8cacb8f9-5ca1-4ac2-8724-caa02c11f0e4.png)
When you click on the Prime link in the menu you will see a page that is using PrimeNg components.
Add A Database
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_6e2a1a94-0cbc-4cbc-9a9b-2c82399d3dab.png)
Right-click on the project node, select Add then New Scaffolded Item…
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image7.png)
Select Full Dependencies then click Add.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_21be183f-d9c8-45db-bd20-d1c43190331b.png)
The Scaffolding will run.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_373acc6f-c87a-4601-a646-0ec5e18202b7.png)
Close the ScaffoldingReadMe.txt file that opens.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_d7be5382-f7de-4eff-8cb6-00f3c08cf2b5.png)
Right-click on the project node and select Edit HelloWorldData.csproj.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_89781c3a-f026-42e0-9089-1e8057c95f85.png)
Add:
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
Save and close the file.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_983c4cac-da60-4ca0-840d-1bc278d8e2ea.png)
From the menu in Visual Studio, select Tools then Connect to Database…
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image19.png)
- Ensure that Microsoft SQL Server Database File (SqlClient) is selected for Data source (use the Change button if not)
- Enter HelloData.mdf for the database name and indicate that it is in a folder called Data that is under your project root (the file does not exist, it will be created by Visual Studio)
- Click OK
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image22.png)
Click Yes to create the database.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_8190bef3-7f4b-45a1-b928-68c8b0d33223.png)
In the Server Explorer window in Visual Studio (you can get to it from the Visual Studio menu using View then Server Explorer), the database will show.
Expand it, right-click on the Tables node and select Add New Table.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_265a7f62-e0b9-41da-9bbb-96665fee7c5e.png)
Enter the following script and click the Update button:
CREATE TABLE [dbo].[WeatherForecast] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[DateFormatted] NVARCHAR (50) NOT NULL,
[TemperatureC] INT NOT NULL,
[TemperatureF] INT NOT NULL,
[Summary] NVARCHAR(50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image31.png)
Click Update Database.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_463c61a1-c0b2-46cb-a8bf-00059482e638.png)
The Data Tools Operations window will indicate when the update is complete.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_74a182cf-c99a-4213-8138-fa8a9b22baa8.png)
In the Server Explorer window, right-click on the database and select Refresh.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_3a54c100-6597-42c9-8bba-4a808c5148a5.png)
The WeatherForecast table will show.
Right-click on the table and select Show Table Data.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_8ebf1042-bec6-4fc2-b99a-1045cadfd45d.png)
Enter sample data into the grid.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_c5fbb08e-e960-4de7-b10d-cf4b4e68065f.png)
Always Close the database connection when done working with it to prevent locking.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_b77164c4-c5e6-4f39-93ac-622d3395430c.png)
In the Solution Explorer, right-click on the project node and select Manage NuGet Packages.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image59.png)
Search for and install: Microsoft.EntityFrameworkCore.Tools.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_bf9a06ce-b893-41f9-be20-f4b73c3d884e.png)
Open the NuGet Package Manager Console.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_997e9a92-b3ad-4984-af90-169d9452d194.png)
Enter:
Scaffold-DbContext "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\TEMP\HelloWorldData\Data\HelloData.mdf;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
and press Enter.
(update the connection string above to point to the location of the .mdf file in the project)
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_eef52d7b-f17f-469c-bc7d-15001d1d1b33.png)
The scaffolded files will appear in the Models directory.
Note: If you get DBContext cannot be found errors (red squiggly lines in the Visual Studio text editor, simply close Visual Studio and re-open it.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_232489b0-c631-4a53-b59a-bdfbcbc86d87.png)
Rename the DataContext file and the class to HelloDataContext.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_9bf898d9-1221-4702-be78-d991b06a3017.png)
Next, we follow the directions at this link: ASP.NET Core - Existing Database.
We remove the OnConfiguring method.
We add the following constructor to the class:
public HelloDataContext(DbContextOptions<HelloDataContext> options) : base(options) { }
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_198be84c-0108-43cc-b27e-6c17f4c0b5d4.png)
We add the following using statements to Startup.cs:
using HelloWorldData.Models;
using Microsoft.EntityFrameworkCore;
![image image](1176262/Windows-Live-Writer-Hello-World-Angular.Net-Core-and-PrimeNg_144D7-image_91cec9f6-6e53-44d1-9c63-88cdc7ebb3b6.png)
We add the following code to the ConfigureServices section:
services.AddDbContext<HelloDataContext>(options => options.UseSqlServer(
Configuration.GetConnectionString("HelloWorldDataDatabase")));
![image image](1176262/Windows-Live-Writer-Hello-World-Angular.Net-Core-and-PrimeNg_144D7-image_6728ba4a-716e-4bae-9372-8fd27562d08d.png)
Add the following setting to the appsettings.json file:
"ConnectionStrings": {
"HelloWorldDataDatabase": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=
C:\\TEMP\\HelloWorldData\\Data\\HelloData.mdf;Integrated Security=True;"
},
(Note: The HelloWorldDataDatabase value needs to be on a single line – see the source code for the exact seting)
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_2c58c8cd-fcc0-4695-a61e-85bcc3796482.png)
Right-click on the Controllers folder and select Add then New Scaffolded Item…
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_4e5fbcfb-ac74-4603-a6c2-08ac9e7c82d7.png)
Select API, then API Controller with actions, using Entity Framework, then click Add.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image99.png)
Select the options above and click Add.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image116.png)
An API controller that will retrieve data from the database will be created.
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_7c5b9e76-6acb-4e22-858b-41b6e4dfc4d1.png)
We will now point the Angular page to the new API controller.
Open the ..\ClientApp\app\components\fetchdata\fetchdata.component.ts file and alter the .get address to:
'/api/WeatherForecasts'
![image image](1176262/Windows-Live-Writer-Hello-World-With-.Net-Core-_5894-image_5a799f41-0a93-4160-9571-2ca09023aab2.png)
When we run the application, and click on the Fetch data link, the data will now be retrieved from the database.
Links LightSwitchHelpWebsite
Hello World! in Angular 2 using Visual Studio 2015 and ASP.NET 4
Implement ASP.NET 4 MVC Application Security in Angular 2 Using OData 4
Tutorial: Creating An Angular 2 CRUD Application Using MVC 5 and OData 4
Tutorial: An End-To-End Angular 2 Application Using MVC 5 and OData 4
An Angular 2+ Web Application Configuration Wizard
An Angular 2 Tree With CRUD Functionality
Links Other
Building Single Page Applications on ASP.NET Core with JavaScriptServices/a>
Visual Studio 2017
Node.js
EntityFramework Reverse POCO Generator
ASP.NET Core - New Database
ASP.NET Core - Existing Database
Publishing and Running ASP.NET Core Applications with IIS
Download
The source file is too big to attach to the article, so the project is available at http://lightswitchhelpwebsite.com/Downloads.aspx
You must have Visual Studio 2017 (or higher) installed to run the code.
You will need to alter the appsettings.json file to point to the exact location of the .mdf file on your computer depending on where you unzip the project to.