Wednesday 29 September 2021

How to push changes to github after password authentication is deprecated

I tried to check-in some code to my github repository after more than 6 months today. I found that normal username and password for authentication does not work anymore. Instead I had to use Personal access token (PAT).

There's a simple step to generate PAT. Keep the code secure in a secure place as it will be required to push the change to the git. 

Create a token in github:

1) login to github site. 

2) on the top right corner, Click user icon.

3) on the dropdown menu,  select Settings.

4) click on Developer settings 

5) Select Personal Access token. 

6) Click Generate new token. And follow the instruction. 

7) select required permissions. 

Use token to push code:

1) Open console and locate the source folder. Run the code below: 

git push https://<GITHUB_ACCESS_TOKEN>@github.com/<GITHUB_USERNAME>/<REPOSITORY_NAME>.git
Example: 
git push https://ghp_N4dLdh4feZyO2F4XIt8NufA3056jSU2qlkoi@github.com/sanjeeb/myproject2.git

Saturday 11 September 2021

simple use of Record in c#9 and newer versions

 c# 9 introduces a new keyword Record which makes an object immutable. Properties in the record can be initialised at the time of creation or constructor call only. Record can be written like how we write a class. It has same syntax as class.

For example:

public record Friend

        {

            public Friend()

            {

            }

            public Friend(string name, string surname)

            {

                this.Name = name;

                this.Surname = surname;

            }

            public string Name { get; init; }

            public string Surname { get; init; }

        }

use of Friend record:

var newFriend = new Friend("Tom", "Pandey");

//--Note: we called here using constructor.

var newFriend1 = new Friend{Name="Tom",Surname= "Pandey"};

//Note: this calls the constructor without parameter. constructor without parameter is not required to to call to initiate the record. 

This record is equivalent to below code which don't have a constructor:

public record Friend

        {

            public string Name { get; init; }

            public string Surname { get; init; }

        }


use of Friend Record:

var newFriend = new Friend("Tom", "Pandey");

Note: we called here using constructor, but we don't have constructor defined. so It will throw an error. Instead we create object friend like this:

var newFriend1 = new Friend{Name="Tom",Surname= "Pandey"};

In both the example of Record, we can not modify the property Name or Surname after the object is created. 

The Friend record with constructor can be written as below code in one line:

public record Friend(String Name, String Surname);

use of Friend Record: 

Friend friend = new Friend("John","Pandey" );

Note: since this is constructor type record, we must create record object using constructor.for example below code will throw error:

var newFriend1 = new Friend{Name="Tom",Surname= "Pandey"};

On compilation, this is converted to class with immutable properties.

init only property in c#9 or later versions

 c# 9 introduces new Init-Only property that allow to make immutable properties in a class. This means the property with "init" keyword in place of "set" keyword allows the property to be initialized at the construction step of an object. it doesn't allow you to set the value later, once the object is initialised. 

For example

 public class Friend

        {

            public Friend(string name, string surname)

            {

                this.Name = name;

                this.Surname = surname;

            }

            public string Name { get; init; }


            public string Surname { get; init; }

        }


Use of Friend

   public void SomeMethod()

        {

            Friend friend = new Friend("John", "Sharma");

            var newFriend = new Friend("Tom", "Pandey");

        }

Use of init property after initialization throws an error. for eg.

 public void SomeMethod()

        {

            Friend friend = new Friend("John", "Sharma");

            var newFriend = new Friend("Tom", "Pandey");

            friend.Surname = "xxx"; //<--- Error here.

        }

Generate SQL script from entity framework using powershell/visual studio

to run the below command , start PowerShell in visual studio. Select the database project. In PowerShell:   Script-Migration this command wi...