Thursday 1 December 2022

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 will generate script from blank database to the latest migration.


Script-Migration migrationname1

this command will generate script from migration named migrationname1 to the latest migration.


Script-Migration migrationname1 migrationnamex

this command will generate script from migration named migrationname1 to migrationnamex



Monday 28 November 2022

Adding custom prerequisite software to Visual studio installer project

 One of my application require asp.net runtime to be installed in the client system to run the application. Visual studio installer project doesn't have option to add Microsoft ASP.NET Core 6.0.11 Shared Framework (x64) to the project pre-requisite screen in visual studio.



The list of Prerequisite is loaded from configuration available in 

C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\BootstrapperPackages

Or on the location where visual studio is installed. :D



Solution:

note: Microsoft ASP.NET Core 6.0.11 Shared Framework (x64) needs to be installed in the system so that we can get some info from control panel.


  1) clone a folder and rename it to "aspnet6coreruntime_x64"

2) remove all the files/folders other than these:

    



3)  modify package.xml to : 

<?xml version="1.0" encoding="utf-8" ?>

<Package xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper" Name="DisplayName" Culture="Culture">


  <!-- Defines a localizable string table for error messages-->

  <Strings>

    <String Name="DisplayName">Microsoft ASP.NET Core 6.0.11 Shared Framework (x64)</String>

    <String Name="Culture">en</String>

    <String Name="AdminRequired">You do not have the permissions required to install the ASP.NET Core 6.0 Runtime (v6.0.11). Please contact your administrator.</String>

    <String Name="InvalidOS">Installation of the ASP.NET Core 6.0 Runtime (v6.0.11) (x64) is supported only on x64 machines.</String>

    <String Name="GeneralFailure">A failure occurred attempting to install the ASP.NET Core 6.0 Runtime (v6.0.11).</String>

    <String Name="InvalidPlatformWinNT">Installation of the Microsoft ASP.NET Core 6.0 Runtime (v6.0.11) is not supported on this operating system.</String>

  </Strings>

</Package>


4) modify product.xml to :

<?xml version="1.0" encoding="utf-8" ?> 


<Product xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper" ProductCode="Microsoft ASP.NET Core 6.0.11 Shared Framework (x64)">


  <!-- Defines list of files to be copied on build -->

  <PackageFiles CopyAllPackageFiles="false">

    <PackageFile Name="aspnetcore-runtime-6.0.11-win-x64.exe"

                 HomeSite="https://download.visualstudio.microsoft.com/download/pr/e874914f-d43d-4b61-8479-f6a5536e44b1/7043adfe896aa9f980ce23e884aae37d/aspnetcore-runtime-6.0.11-win-x64.exe"

                 PublicKey="0" />

    <PackageFile Name="NetCoreCheck.exe" />

  </PackageFiles>


  <!-- Run the NetCoreCheck tool that will determine if the necessary framework is installed -->

  <InstallChecks>

    <ExternalCheck Property="NetCoreCheck" PackageFile="NetCoreCheck.exe" Arguments="Microsoft ASP.NET Core 6.0.11 Shared Framework (x64)"/>

  </InstallChecks>


  <!-- Defines how to invoke the setup for the .Net Runtime 6.0 -->

  <Commands Reboot="Defer">

    <Command PackageFile="aspnetcore-runtime-6.0.11-win-x64.exe" Arguments=' /q '>


      <!-- These checks determine whether the package is to be installed -->

      <InstallConditions>

        <!-- Block install on any platform other than x64 (Arm64 will usually work too) -->

        <FailIf Property="ProcessorArchitecture" Compare="ValueEqualTo" Value="Intel" String="InvalidOS" BeforeInstallChecks="true"/>

        <FailIf Property="ProcessorArchitecture" Compare="ValueEqualTo" Value="Arm" String="InvalidOS" BeforeInstallChecks="true"/>

        <!-- Block install on less than Windows 7 RTM -->

        <FailIf Property="VersionNT" Compare="VersionLessThan" Value="6.1.0" String="InvalidPlatformWinNT"/>

        <!-- NetCoreCheck returning 0 means the runtime is already installed -->

        <BypassIf Property="NetCoreCheck" Compare="ValueEqualTo" Value="0"/>

        <!-- Block install if user does not have admin privileges -->

        <FailIf Property="AdminUser" Compare="ValueEqualTo" Value="false" String="AdminRequired"/>

      </InstallConditions>


      <ExitCodes>

        <ExitCode Value="0" Result="Success"/>

        <ExitCode Value="3010" Result="SuccessReboot"/>

        <DefaultExitCode Result="Fail" FormatMessageFromSystem="true" String="GeneralFailure" />

      </ExitCodes>


    </Command>

  </Commands>

</Product>


4.1) packageFile is the name of the file downloaded from microsoft site: https://download.visualstudio.microsoft.com/download/pr/e874914f-d43d-4b61-8479-f6a5536e44b1/7043adfe896aa9f980ce23e884aae37d/aspnetcore-runtime-6.0.11-win-x64.exe

4.2) HomeSite is the url of downloadable location. 
4.3) Arguments is the name of the installed library which can be found in control panel. 



5) Now restart visual studio and open the project solution. 
6) 

Thursday 17 February 2022

Raspberry pi: how to find raspberry pi model details

In the raspberry pi console type: 

 pinout

output:

,--------------------------------. | oooooooooooooooooooo J8 +==== | 1ooooooooooooooooooo | USB | +==== | Pi Model 3B V1.2 | | +----+ +==== | |D| |SoC | | USB | |S| | | +==== | |I| +----+ | | |C| +====== | |S| | Net | pwr |HDMI| |I||A| +====== `-| |--------| |----|V|-------' Revision : a02082 SoC : BCM2837 RAM : 1024Mb Storage : MicroSD USB ports : 4 (excluding power) Ethernet ports : 1 Wi-fi : True Bluetooth : True Camera ports (CSI) : 1 Display ports (DSI): 1 J8: 3V3 (1) (2) 5V GPIO2 (3) (4) 5V GPIO3 (5) (6) GND GPIO4 (7) (8) GPIO14 GND (9) (10) GPIO15 GPIO17 (11) (12) GPIO18 GPIO27 (13) (14) GND GPIO22 (15) (16) GPIO23 3V3 (17) (18) GPIO24 GPIO10 (19) (20) GND GPIO9 (21) (22) GPIO25 GPIO11 (23) (24) GPIO8 GND (25) (26) GPIO7 GPIO0 (27) (28) GPIO1 GPIO5 (29) (30) GND GPIO6 (31) (32) GPIO12 GPIO13 (33) (34) GND GPIO19 (35) (36) GPIO16 GPIO26 (37) (38) GPIO20 GND (39) (40) GPIO21


This commands returns few important information like Model and revision of the raspberry pi, Memory, SoC, Wifi/Bluetooth, and Pin ports detail.

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.

        }

Friday 14 May 2021

How to check roles for sql users

 

The following query returns the members of the database roles.

SELECT DP1.name AS DatabaseRoleName,   

   isnull (DP2.name, 'No members'AS DatabaseUserName   
 FROM sys.database_role_members AS DRM  
 RIGHT OUTER JOIN sys.database_principals AS DP1  
   ON DRM.role_principal_id = DP1.principal_id  
 LEFT OUTER JOIN sys.database_principals AS DP2  
   ON DRM.member_principal_id = DP2.principal_id  
WHERE DP1.type = 'R'
ORDER BY DP1.name;  

Thursday 22 April 2021

Search by keyword in Stored procedure, Views, Functions And Tables

 CREATE PROCEDURE [util].[SearchAll]       

(@strFind AS VARCHAR(MAX))
AS
BEGIN
    SET NOCOUNT ON; 
    --TO FIND STRING IN ALL PROCEDURES        
    BEGIN
        SELECT OBJECT_NAME(OBJECT_ID) SP_Name
              ,OBJECT_DEFINITION(OBJECT_ID) SP_Definition
        FROM   sys.procedures
        WHERE  OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'
    END 

    --TO FIND STRING IN ALL VIEWS        
    BEGIN
        SELECT OBJECT_NAME(OBJECT_ID) View_Name
              ,OBJECT_DEFINITION(OBJECT_ID) View_Definition
        FROM   sys.views
        WHERE  OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'
    END 

    --TO FIND STRING IN ALL FUNCTION        
    BEGIN
        SELECT ROUTINE_NAME           Function_Name
              ,ROUTINE_DEFINITION     Function_definition
        FROM   INFORMATION_SCHEMA.ROUTINES
        WHERE  ROUTINE_DEFINITION LIKE '%'+@strFind+'%'
               AND ROUTINE_TYPE = 'FUNCTION'
        ORDER BY
               ROUTINE_NAME
    END

    --TO FIND STRING IN ALL TABLES OF DATABASE.    
    BEGIN
        SELECT t.name      AS Table_Name
              ,c.name      AS COLUMN_NAME
        FROM   sys.tables  AS t
               INNER JOIN sys.columns c
                    ON  t.OBJECT_ID = c.OBJECT_ID
        WHERE  c.name LIKE '%'+@strFind+'%'
        ORDER BY
               Table_Name
    END
END

exec [util].[SearchAll]  'username'

Tuesday 20 April 2021

Find table and column name in MSSQL

It is time consuming job to browse each and every tables and columns to find required tables  and it's even difficult to search columns name in any database. There's a easy way to do the search.

For example, We want to search all the tables which contains user in the table names in sanjeebDB database:

select * from sanjeebDB.INFORMATION_SCHEMA.TABLES 

where TABLE_NAME like '%user%' 

     ORDER by TABLE_NAME  

Next example, we want to search all the tables which contains 'username' a column name:

select * from sanjeebDB.INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME  like '%username%'


If we need these functions repetitively then we can create a stored procedure and invoke the stored procedure passing keyword as a parameter:

 Create PROCEDURE [UTIL].[FIND_TABLE]
(
    @pTableName VARCHAR(max)
)
AS
BEGIN
     select * from sanjeebDB.INFORMATION_SCHEMA.TABLES 
     where TABLE_NAME like '%' + @pTableName + '%' 
     ORDER by TABLE_NAME  
END



create PROCEDURE [UTIL].[FIND_TABLE_BY_COLUMN]
(
    @pColumnName VARCHAR(max)
)
AS
BEGIN
     select * from sanjeebDB.INFORMATION_SCHEMA.COLUMNS where             COLUMN_NAME  like '%' + @pColumnName + '%'
END

To use them, run the below sql query:

exec util.FIND_TABLE 'user'

exec util.FIND_TABLE_BY_COLUMN 'username'


Good Luck!




Thursday 25 February 2021

How to give SQL User access to execute Stored procedures and read/write access to database

Assumptions: stored procedures and tables are created under schema dbo. in case different schema, change the below code accordingly. 


 --Create Login with a strong password

--Run the below code in Master Database

CREATE LOGIN [sanjeebuser] WITH PASSWORD = 'VeryStrongPa55w0rD1!'

--run the below code in your Selected Database, (optional: run in master database as well)

CREATE USER [sanjeebuser]

 FOR LOGIN [sanjeebuser]

 WITH DEFAULT_SCHEMA =  dbo

GO


--Run the below code in your selected Database. please do not run the code in Master database.

CREATE ROLE [dbstoregeprocedureaccess] AUTHORIZATION [dbo]

GRANT EXECUTE,ALTER,DELETE,UPDATE,INSERT ON SCHEMA::dbo TO dbstoregeprocedureaccess;

GRANT CREATE PROCEDURE TO dbstoregeprocedureaccess;



EXEC sp_addrolemember N'db_datareader', N'sanjeebuser'

EXEC sp_addrolemember N'db_datawriter', N'sanjeebuser'

EXEC sp_addrolemember N'dbstoregeprocedureaccess', N'sanjeebuser'


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...