adsense

Sunday, March 30, 2025

Entity Framework Selectmany

 In Entity Framework Core, the SelectMany operation flattens collections, which can lead to unexpected results if not carefully applied. For example, in a query that filters students and their active courses:

var activeCourses = await _employeeRepository

    .GetAllEmployees()

    .Where(employee=> employee.IsActive)

    .SelectMany(employee=> employee.PayeDetails)

    .Where(payDetail=> payDetail.IsTermnated)

    .ToListAsync();

The Where clause before SelectMany filters the employees, but after SelectMany, the query works with a flattened collection of pay details. This means the second Where clause filters the pay details, not the employees. As a result, you might get pay details from terminated employees because the filtering is done on the flattened collection of pay details, not the original employee collection. 

In some cases Selectmany can act like a CrossJoin as well.


Cheers

Samitha

Saturday, March 1, 2025

Ef .Net Core DbSet.ExecuteUpdate

 EF Core 7.0 introduces the ExecuteUpdate method, which allows for updating entities in the database based on a query result. However, it requires explicit specification of the changes to be made, as EF Core does not automatically detect them. Tracked entities will not remain in sync, and additional commands may need to be issued in a specific order to avoid violating database constraints (e.g., updating dependents before deleting a principal).

The ExecuteUpdate method complements, rather than replaces, the existing SaveChanges mechanism. It functions similarly to the ExecuteDelete method, but with the key difference that an update requires specifying which properties to update and how to do so, typically using calls to SetProperty.


Cheers.

Samitha

Sunday, February 16, 2025

Return no operation form a task implementation

 When you want to return a no operation result from a task implementation from an endpoint you can use following to return no operation performed.



 public Task LongRunningAsync()

        {

            // do some work

            var x = 1;     

         // Can't return 'null' 

            return Task.FromResult<object<(null);

        }


Cheers,

Samitha

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