Skip to main content

Posts

Debug your .NET 8 code more efficiently

.NET 8 introduces a lot of debugging improvements. If you take a look for example at the HttpContext , you see that you get a much better debug summary than in .NET 7: .NET 7: .NET 8: But that is not a feature I want to bring under your attention. After recently updating my Visual Studio version, I noticed the following announcement among the list of new Visual Studio features: That is great news! This means that you can debug your .NET 8 applications without a big performance impact on the rest of your code. The only thing we need to do is to disable the Just My Code option in Visual Studio: If we now try to debug a referenced release binary, only the relevant parts are decompiled without impacting the other code: More information Debugging Enhancements in .NET 8 - .NET Blog (microsoft.com)
Recent posts

EF Core - Error CS1503 Argument 2: cannot convert from 'string' to 'System.FormattableString'

I was pair programming with a team member when she got the following compiler error: Error CS1503 Argument 2: cannot convert from 'string' to 'System.FormattableString' The error appeared after trying to compile the following code: The reason becomes apparent when we look at the signature of the SqlQuery method: As you can see the method expects a FormattableString not a string . Why is this? By using a FormattableString EF Core can protect us from SQL injection attacks. When we use this query with parameters(through string interpolation), the supplied parameters values are wrapped in a DbParameter . To get rid of the compiler error, we can do 2 things: 1. We explicitly create  a FormattableString from a regular string: 2. We use string interpolation and add a ‘$’ sign in front of the query string: More information SQL Queries - EF Core | Microsoft Learn RelationalDatabaseFacadeExtensions.SqlQuery<TResult> Method (Microsoft.EntityF

Git–Dubious ownership

Today I had a strange Git error message I never got before. When I tried to execute any action on a local Git repo, it fails with the following error: git status fatal: detected dubious ownership in repository at 'C:/projects/examplerepo' 'C:/projects/examplerepo' is owned by: 'S-1-5-32-544' but the current user is: 'S-1-5-21-1645522239-329068152-682003330-18686' To add an exception for this directory, call: git config --global --add safe.directory C:/projects/examplerepo Image generated by AI What does this error mean and how can we fix it? Let’s find out! This error means that the current user is not the owner of the git repository folder. Before git executes an operation it will check this and if that is not the case it will return the error above. The reason why this check exists is because of security reasons. Git tries to avoid that another user can place files in our git repo folder. You can check the owner of a directory by executing

EF Core - Query splitting

In EF Core when fetching multiple entities in one request it can result in a cartesian product as the same data is loaded multiple times which negatively impacts performance. An example is when I try to load a list of Customers with their ordered products: The resulting query causes a cartesian product as the customer data is duplicated for every ordered product in the result set. EF Core will generate a warning in this case as it detects that the query will load multiple collections. I talked before about the AsSplitQuery method to solve this. It allows Entity Framework to split each related entity into a query on each table: However you can also enable query splitting globally in EF Core and use it as the default. Therefore update your DbContext configuration: More information EF Core–AsSplitQuery() (bartwullems.blogspot.com) Single vs. Split Queries - EF Core | Microsoft Learn

Loading aggregates with EF Core

In Domain-Driven Design (DDD), an aggregate is a cluster of domain objects that are treated as a single unit for the purpose of data changes. The aggregate has a root and a boundary: Aggregate Root : This is a single, specific entity that acts as the primary point of interaction. It guarantees the consistency of changes being made within the aggregate by controlling access to its components. The aggregate root enforces all business rules and invariants within the aggregate boundary. Boundary : The boundary defines what is inside the aggregate and what is not. It includes the aggregate root and other entities or value objects that are controlled by the root. Changes to entities or value objects within the boundary must go through the aggregate root to ensure consistency. An example of an aggregate is an Order (which is the Aggregate root) together with OrderItems (entities inside the Aggregate). The primary function of an aggregate is to ensure data consist

Entity Framework Core– Avoid losing precision

When mapping a decimal type to a database through an ORM like EF Core, it is important to consider the precision. You don't want to lose data or end up with incorrect values because the maximum number of digits differs between the application and database. If you don’t explicitly configure the store type, EF Core will give you a warning to avoid losing precision. Imagine that we have the following Product class with a corresponding configuration: If we try to use this Product class in our application, we get the following warning: warn: SqlServerEventId.DecimalTypeDefaultWarning[30000] (Microsoft.EntityFrameworkCore.Model.Validation)       No store type was specified for the decimal property 'UnitPrice' on entity type 'Product'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values in 'OnModelCreating' using 'Has

CS0012: The type 'System.Object' is defined in an assembly that is not referenced.

A fter referencing a .NET Standard 2.0 project in a .NET 4.8 ASP.NET MVC project, the project failed ant runtime with the following error message: CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Whoops! Let me explain how I fixed the problem. The solution – Part I I first tried to add the NETStandard.Library nuget package to the .NET 4.8 project but that didn’t made the error disappear.(Although I come back to this in Part II below). So I removed the nuget package again and instead I did the following: I manually edited the csproj file and added the following reference: I also updated the reference and set Copy Local=true After doing that the error disappeared and the application ran successfully on my local machine. Victory… … or not? After committing the updated project, a colleague conta