Friday, March 23, 2018

ASP.Net Core 2 with Postgresql on Linux Part 3: Create the ASP.Net MVC Core 2.x app and Install Entity Framework Core

In Part I, I discussed the rationale for porting a ASP.Net Core 2 app to my Linux system replacing SQL Server with Postgresql and place it on Heroku. As I thought about it, my first logical step was to port the data from SQL Server to Postgresql. While I think I am proficient at the command prompt, I had to admit that having a GUI based tool would be a help. Having used pgAdmin on a Rails 3.x project, that is where I leaned. In that article I show the steps to installing pgAdmin, a GUI Postgresql client on Ubuntu 16.04. In Part II, we transferred the SQL Server data to Postgres.

In this post, I create the ASP.Net MVC Core 2.x app, from which we will proceed, and get Entity Framework Core installed.

First, I created a new ASP.Net MVC .Net Core C# project in Visual Studio Code (see https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app-xplat/start-mvc for more).

 $mkdir myApp  
 $cd myApp  
 $dotnet new mvc  

I already had the .NET Core 2.0.0 SDK, Visual Studio Code, and the VS Code C# extension on my Ubuntu 16.04 Linux system. See https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app-xplat/start-mvc for more information on these steps.


Then, I issued a dotnet run to build and execute the source code:


Per the URL provided in Visual Studio Code screen above, I browsed to http:/localhost:5000:


OK, so the app will build and load.

Next, I updated the project file in Visual Studio Code (myApp.csproj) to include the following settings to install Entity Framework Core:

 <Project Sdk="Microsoft.NET.Sdk.Web">  
  <PropertyGroup>  
   <TargetFramework>netcoreapp2.0</TargetFramework>  
  </PropertyGroup>  
  <ItemGroup>  
   <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />  
   <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />  
  </ItemGroup>  
  <ItemGroup> 
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />  
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />  
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />  
  </ItemGroup>  
 </Project>  

Then, I ran the dotnet restore command to install the above EF package and tools:

 $dotnet restore  


Now we have a working ASP.Net MVC Core 2.0 app with Entity Framework Core installed.

Next, in Part IV, I look at the configuration differences, in preparation of porting my existing app to use Postgresql.


No comments: