Search This Blog

2018/01/06

Notes on Integrating Postgres Entity Framework with DOT NET Core 2.0

First create a Core 2.0 app say dotnetmvcapp using

dot net new dotnetmvcapp

now we will follow online tutorial

https://www.youtube.com/watch?v=md20lQut9EE

I am just listing my files & its final content & Few important points.

dotnetmvcapp.csproj


<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSql" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>

</Project>


run restore packages

dotnet restore


Startup.cs
add below using directives first

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFrameworkNpgsql()
.AddDbContext<dotnetmvcappContext>(opt
=> opt.UseNpgsql(Configuration.GetConnectionString("MyWebAppConnection")));
}
Here You Need to add Below using get read of error message at “ UseNpgsql” &
“dotnetmvcappContext”.

using dotnetmvcapp.Models;
using Microsoft.EntityFrameworkCore;




Create Model:

a) Create Models folder

b) Inside Models folder add

dotnetmvcappContext.cs

Users.cs


dotnetmvcappContext.cs

using Microsoft.EntityFrameworkCore;

namespace dotnetmvcapp.Models
{
public class dotnetmvcappContext : DbContext
{
public dotnetmvcappContext(DbContextOptions<dotnetmvcappContext> options) : base(options)
{

}
public DbSet<User> Users {get;set;}
}
}


Users.cs
namespace dotnetmvcapp.Models
{
public class User
{
public int Id{get;set;}
public string Name{get;set;}

public string Email{get;set;}
}
}

appsettings.json

{
"ConnectionStrings":{
"MyWebAppConnection":"User ID= xdba;Password=sangram;Server=localhost;Port=5432;Database=xplay;Integrated Security=true;Pooling = true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}

Run Migration:
dotnet ef migrations add InitialMigration

It initializes our 'dotnetmvcappContext'

this Migration can be undone using
ef migrations remove

Apply Migration to Db:

dotnet ef database update

this will create a Users Table inside postgres which conforms to


CREATE TABLE public."Users"
(
"Id" integer NOT NULL DEFAULT nextval('"Users_Id_seq"'::regclass),
"Email" text COLLATE pg_catalog."default",
"Name" text COLLATE pg_catalog."default",
CONSTRAINT "PK_Users" PRIMARY KEY ("Id")
)

you can check postgre sql and confirm the User Table has been created.


Ubuntu -Resolving gitlab Permission denied (publickey) on push

    1) Once you created gitlab account ,create a repository say "abc" on gitlab, with "code_dir" as folder to push to gitlab
    cd code_dir
    git init
    git remote add origin git@gitlab.com:your.name/abc.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master

      at time of final push i got greeted with "gitlab Permission denied (publickey)" error,after to resolve it we need to create public-private key that identfies our machine & add it to gitab
      inside settings ,ssh key section.

    2) on terminal run
     ssh-keygen
     output:
        Generating public/private rsa key pair.
        Enter file in which to save the key (/home/sangram/.ssh/id_rsa):
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in /home/username/.ssh/id_rsa.
        Your public key has been saved in /home/username/.ssh/id_rsa.pub.
        The key fingerprint is:
        SHA256:xwoAYiFpCTSM9QjvNfPfkh/NQAKhktRQ2zh6+tYo1u4 sangram@sangram-HP-Laptop-15-bs0xx
        The key's randomart image is:
        +---[RSA 2048]----+
        |XBB+. o.         |
        |=O.=.= .         |
        |. = @ . . .      |
        | . + *   +       |
        |  o . o S +      |
        |   o   o = +     |
        |  .. o  = o o    |
        |  o.+ .  o .     |
        | . =E     .      |
        +----[SHA256]-----+

    2) Cat /home/username/.ssh/id_rsa.pub
           copy all output to clipboard

    3) login to gitlab & visit
        settings-->SSH Keys
    in textbox for key on left paste key text & hit save.
    4) Now attempt push
    git push -u origin master