CodeQL is a set of software tools that work together to perform a specific task or series of tasks. This includes various tools for tasks such as compiling, linking , debugging, code analysis and Testing.
Read more on CodeQL here.
Cheers,
Samitha
Blogs Tech stuff and many more...
CodeQL is a set of software tools that work together to perform a specific task or series of tasks. This includes various tools for tasks such as compiling, linking , debugging, code analysis and Testing.
Read more on CodeQL here.
Cheers,
Samitha
ExecuteUpdate was introduced in EF Core 6.0 to improve data manipulation capabilities.
To demonstrate the use of ExecuteUpdate, let's consider examples below.
using (var context = new DbContext())
var items= context.Items.ToList();
foreach (var item in items)
{
item .Name = "XXX";
item .LastUpdateDateTime = DateTime.UtcNow;
}
context.SaveChanges();
The first example uses the traditional approach of iterating over a list of items and updating each one.
context.Items.ExecuteUpdate(i => i.SetProperty(p => p.Name, "XXX")
.SetProperty(g => p.LastUpdateDateTime, DateTime.UtcNow))
The second example uses ExecuteUpdate.
When consider a bulk update, EFCore will generate the following query for each line. As a result for 1000 lines, 1000 SQL queries will be executed.
--Sigle query for bulk updates
UPDATE "Items" AS "i"
SET "LastUpdateDateTime" = rtrim(rtrim(strftime('%Y-%m-%d %H:%M:%f', 'now'), '0'), '.'),
"Name" = 'XXX'
The ExecuteUpdate method translates the update operation into a single SQ query executing without loading entities into memory. This results in significant performance gains, particularly noticeable in large-scale operations.
You can read more about ExecuteUpdate here.
Regards,
Samitha
:
AsSplitQuery will perform separate queries instead of complex joins when returning data from multiple tables.
When using a single query result with data from multiple tables, there is a probability of “cartesian explosion” of duplicate data across many columns and rows.
e.g
var result =dbContext.Student.Where(s=> s.Id == id)
.Include(x => x.Books)
.ThenInclude(x => x.Overdues)
.AsSplitQuery();
Cheers,
Samitha
If you ever wanted to assign an empty string to a value returned from a Linq query, use ?? (null-coalescing operator) to return Empty string in case of null
var qry= from row in context.Table
select new { FieldValue = (field.Value ?? string.Empty) };
Cheers,
Samitha
In C# 8.0, the Switch statements has become more powerful with patterns. All the cases have turned into expressions and become more readable.
Following code illustrates basic usage of new switch.
var action= 1;
var result = operation switch
{
1 => "Action 1",
2 => "Action 2",
3 => "Action 3",
4 => "Action 4",
_ => "No Action performed"
};
Console.WriteLine(result );
More information can be found here.
Chaeers,
Samitha
EF .Net Core retrieves entities from the database with racking enabled, meaning any changes made to the entity are observed and can be automatically saved back to the database.
This automatic monitory feature is not optimal when it comes to read only operations (SELECT statements). To get more information on Tracking Vs Non Tracking queries read following Microsoft article.
As an optimization for read-only queries we can AsNoTracking as shown below. Adding this will stop E EF Core tracking the results, returning in improved performance as EF Core doesn't manage the state of the entity.
var books= dbContext.Books
.AsNoTracking()
.ToList();
Cheers,
Samitha
When working with Enumerable in .Net, there is always a possibility of returning an empty Enumurable if no match is found. Consider the following code.
private IEnumarable<Book> GetBook(string name)
{
if (string.IsNullOrEmpty(name))
return Enumerable.Empty<Book>();
return _dbContext.Book.Where(b => b.Name.ToLower().Contains(name));
}
In the above piece of code Enumerable.Empty method is used to check the null name and return an empty book rather than returning null which can cause runtime exception if the name is not supplied. This is considered as defensive code practice.
Cheers,
Samitha