Deprecated: Creation of dynamic property Kirki_Toolkit::$modules is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/kirki.php on line 67

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Textarea::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Toggle::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Multicheck::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Number::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Text::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Multicheck::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Warning: Cannot modify header information - headers already sent by (output started at /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php:291) in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-includes/feed-rss2.php on line 8
Parameters in Raw SQL Queries with FromSqlRaw in Entity Framework Core Archives - OctopusCodes https://octopuscodes.com/tag/parameters-in-raw-sql-queries-with-fromsqlraw-in-entity-framework-core/ Tue, 09 May 2023 04:02:45 +0000 en-US hourly 1 https://wordpress.org/?v=6.9 https://octopuscodes.com/wp-content/uploads/2022/12/cropped-logo-80x80-1-32x32.png Parameters in Raw SQL Queries with FromSqlRaw in Entity Framework Core Archives - OctopusCodes https://octopuscodes.com/tag/parameters-in-raw-sql-queries-with-fromsqlraw-in-entity-framework-core/ 32 32 Parameters in Raw SQL Queries with FromSqlRaw Method in Entity Framework Core https://octopuscodes.com/net-core-tutorial/entity-framework-core-tutorial/parameters-in-raw-sql-queries-with-fromsqlraw-method-in-entity-framework-core/ Tue, 09 May 2023 03:31:53 +0000 https://octopuscodes.com/?p=2268 You can pass parameters to a raw SQL query executed using the FromSqlRaw method. This helps to prevent SQL injection attacks and makes the code more readable. using EntityFrameworkCore_ConsoleApp.Models; using …

The post Parameters in Raw SQL Queries with FromSqlRaw Method in Entity Framework Core appeared first on OctopusCodes.

]]>
You can pass parameters to a raw SQL query executed using the FromSqlRaw method. This helps to prevent SQL injection attacks and makes the code more readable.

using EntityFrameworkCore_ConsoleApp.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var status = true;
                var products = databaseContext.Products.FromSqlRaw("select * from product where status = {0}", status).ToList();
                products.ForEach(product =>
                {
                    Console.WriteLine("Id: " + product.Id);
                    Console.WriteLine("Name: " + product.Name);
                    Console.WriteLine("Price: " + product.Price);
                    Console.WriteLine("Quantity: " + product.Quantity);
                    Console.WriteLine("Description: " + product.Description);
                    Console.WriteLine("Status: " + product.Status);
                    Console.WriteLine("Photo: " + product.Photo);
                    Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
                    Console.WriteLine("Category Id: " + product.CategoryId);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1
---------------------
Id: 12
Name: Laptop 1
Price: 15
Quantity: 16
Description: Description 3
Status: True
Photo: photo4.gif
Created: 20/10/2021
Category Id: 2
---------------------
Id: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------
Id: 16
Name: Computer 3
Price: 19
Quantity: 18
Description: Description 7
Status: True
Photo: photo8.gif
Created: 20/05/2023
Category Id: 3
---------------------

Use AND condition in Where:

using EntityFrameworkCore_ConsoleApp.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var min = 5;
                var max = 20;
                var products = databaseContext.Products.FromSqlRaw("select * from product where price >= {0} and price <= {1}", min, max).ToList();
                products.ForEach(product =>
                {
                    Console.WriteLine("Id: " + product.Id);
                    Console.WriteLine("Name: " + product.Name);
                    Console.WriteLine("Price: " + product.Price);
                    Console.WriteLine("Quantity: " + product.Quantity);
                    Console.WriteLine("Description: " + product.Description);
                    Console.WriteLine("Status: " + product.Status);
                    Console.WriteLine("Photo: " + product.Photo);
                    Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
                    Console.WriteLine("Category Id: " + product.CategoryId);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1
---------------------
Id: 10
Name: Tivi 2
Price: 5
Quantity: 22
Description: Description 2
Status: False
Photo: photo2.gif
Created: 20/04/2022
Category Id: 1
---------------------
Id: 11
Name: Tivi 3
Price: 20
Quantity: 4
Description: Description 2
Status: False
Photo: photo3.gif
Created: 20/11/2022
Category Id: 1
---------------------
Id: 12
Name: Laptop 1
Price: 15
Quantity: 16
Description: Description 3
Status: True
Photo: photo4.gif
Created: 20/10/2021
Category Id: 2
---------------------
Id: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------
Id: 16
Name: Computer 3
Price: 19
Quantity: 18
Description: Description 7
Status: True
Photo: photo8.gif
Created: 20/05/2023
Category Id: 3
---------------------

Use OR condition in Where:

using EntityFrameworkCore_ConsoleApp.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var price1 = 5;
                var price2 = 15;
                var price3 = 20;
                var products = databaseContext.Products.FromSqlRaw("select * from product where price = {0} or price = {1} or price = {2}", price1, price2, price3).ToList();
                products.ForEach(product =>
                {
                    Console.WriteLine("Id: " + product.Id);
                    Console.WriteLine("Name: " + product.Name);
                    Console.WriteLine("Price: " + product.Price);
                    Console.WriteLine("Quantity: " + product.Quantity);
                    Console.WriteLine("Description: " + product.Description);
                    Console.WriteLine("Status: " + product.Status);
                    Console.WriteLine("Photo: " + product.Photo);
                    Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
                    Console.WriteLine("Category Id: " + product.CategoryId);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Id: 10
Name: Tivi 2
Price: 5
Quantity: 22
Description: Description 2
Status: False
Photo: photo2.gif
Created: 20/04/2022
Category Id: 1
---------------------
Id: 11
Name: Tivi 3
Price: 20
Quantity: 4
Description: Description 2
Status: False
Photo: photo3.gif
Created: 20/11/2022
Category Id: 1
---------------------
Id: 12
Name: Laptop 1
Price: 15
Quantity: 16
Description: Description 3
Status: True
Photo: photo4.gif
Created: 20/10/2021
Category Id: 2
---------------------

You can also pass parameters with the SqlParameter method as below:

using EntityFrameworkCore_ConsoleApp.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var param1 = new SqlParameter("@min", 5);
                var param2 = new SqlParameter("@max", 20);
                var products = databaseContext.Products.FromSqlRaw("select * from product where price >= @min and price <= @max", param1, param2).ToList();
                products.ForEach(product =>
                {
                    Console.WriteLine("Id: " + product.Id);
                    Console.WriteLine("Name: " + product.Name);
                    Console.WriteLine("Price: " + product.Price);
                    Console.WriteLine("Quantity: " + product.Quantity);
                    Console.WriteLine("Description: " + product.Description);
                    Console.WriteLine("Status: " + product.Status);
                    Console.WriteLine("Photo: " + product.Photo);
                    Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
                    Console.WriteLine("Category Id: " + product.CategoryId);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1
---------------------
Id: 10
Name: Tivi 2
Price: 5
Quantity: 22
Description: Description 2
Status: False
Photo: photo2.gif
Created: 20/04/2022
Category Id: 1
---------------------
Id: 11
Name: Tivi 3
Price: 20
Quantity: 4
Description: Description 2
Status: False
Photo: photo3.gif
Created: 20/11/2022
Category Id: 1
---------------------
Id: 12
Name: Laptop 1
Price: 15
Quantity: 16
Description: Description 3
Status: True
Photo: photo4.gif
Created: 20/10/2021
Category Id: 2
---------------------
Id: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------
Id: 16
Name: Computer 3
Price: 19
Quantity: 18
Description: Description 7
Status: True
Photo: photo8.gif
Created: 20/05/2023
Category Id: 3
---------------------

The post Parameters in Raw SQL Queries with FromSqlRaw Method in Entity Framework Core appeared first on OctopusCodes.

]]>