Tuesday, January 14, 2025

Package Manager Console error The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception

Recently I came across the error The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception, when I am trying to open the Package Manager Console.

As per comment form Yishai Galatzer  this is caused by a stack overflow bug in a PowerShell DLL 

Workaround

In your Visual Studio folder, make a backup copy of file devenv.exe.config.

Then, in the original devenv.exe.config file, insert the following after the opening assemblyBinding element(run as admin) and save the file


<!-- WORKAROUND START ->
<dependentAssembly>
        <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
        <publisherPolicy apply="no" />
      </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Commands.Utility" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.ConsoleHost" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Commands.Management" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Security" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Commands.Diagnostics" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
<!-- WORKAROUND END -->
To Make the changes effect you'll need to restart VS.

Cheers,
Samitha


Tuesday, December 17, 2024

Code analysis with CodeQL

 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

Sunday, November 24, 2024

Increase EF Core Efficiency: with Bulk Updates

 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

:

Sunday, November 10, 2024

EF Core AsSplitQuery

 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

Monday, October 14, 2024

Assign an empty string if the value is null in Linq

 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