Posts

Showing posts with the label SQL Server

Database First Approach in Entity Framework Core: A Step-by-Step Guide

Image
Introduction: In this blog, I'll walk you through the process of setting up an Entity Framework Core project with a focus on Microsoft SQL Server as the database provider. Setting Up the Database: To begin, we created two tables, namely "Students" and "Teachers," in the "SampleDB" database. This forms the foundation for our data model. Creating the MVC Project: Next, we initiated a new MVC project using Visual Studio. Installing Necessary Packages: In the Package Manager Console, execute the following commands to install the essential packages for Entity Framework Core: Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design Install-Package Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.SqlServer: Installs the required package to enable Entity Framework Core with Microsoft SQL Server as th...

Entity Framework Core (EF) with SQL Server LocalDB

Image
This blog is going to illustrate how to implement Entity Framework (EF) Core with SQL Server LocalDB. The Entity Framework Core enables access to data from the database. This allows developers to avoid having to write most of the data access code. And the SQL Server LocalDB works similarly to the SQL SERVER with few features. You can get the LocalDB when you install visual studio or visual studio express. You can find the .mdf and _log.ldf files in the C:/Users/{user} directory. Let see how to create a SQL Server LocalDB and implement Entity Framework Core Step 1: The first step is to add a connection. In the menu -> click Tools and select Connect to Database It will display the Add Connection window. Enter (localdb)\mssqllocaldb in the server name textbox. Then in the Select or enter a database, select master and click ok Now in the server explorer, you can find the master DB containing Tables, Views, Stored...

SQL Server LEAD() Function

Image
This blog is going to illustrate the functionality of SQL LEAD() and how to use it. The SQL LEAD () function gives the next row of data for the offset value. This functionality was introduced in SQL Server 2012. Let’s see how to use it. The following is the SampleTable. SELECT SampleID, [Year], Profit FROM SampleTable The following query illustrates how to use the LEAD() function. SELECT SampleID, Year, Profit, LEAD(Profit, 1) OVER (ORDER BY SampleID ASC) AS NextYearProfit FROM SampleTable The following image is the output of the query above. Here you will find the column NextYearProfit. This shows the next profit. Default Value In the previous example, you can see that the last NextYearProfit column value is null. You can set the default value instead of zero if you want. SELECT SampleID, [Year], Profit, LEAD(Profit, 1, 7000000) OVER (ORDER BY SampleID ASC) AS NextYearProfit FROM SampleTable In the image below you can see that the las...

SQL Server LAG() Function

Image
This blog is going to illustrate the functionality of SQL LAG() and how to use it. The SQL LAG() function gives the previous row data of the offset value. This functionality has been introduced on the SQL Server 2012. The following is the sample table. There are three columns in table SampleID, Year, and Profit. Let’s see how to get the previous row value.  SELECT * FROM SampleTable The following is an example of the SQL query LAG() function. The query explains how to get the previous year’s profit. SELECT SampleID, Year, Profit,  LAG(Profit, 1) OVER (ORDER BY SampleID ASC) AS PreviousProfit  FROM SampleTable The following table shows the output of the query above. You can see the new column called PreviousProfit here. The first row of the PreviousProfit column is null because there is no previous value. Default value When using the LAG() function, the first result will be null. But it can be assigned a default value. In the following query, the default...