.NET6 and DateTime problem. Cannot write DateTime with Kind=UTC to PostgreSQL type ‘timestamp without time zone’

A. Solved by adding AppContext.SetSwitch(“Npgsql.EnableLegacyTimestampBehavior”, true); to the Startup Configure method. B. Or in case you have no Startup class at all and all your initialization is inside Program.cs with a host builder then your file ending might look like: … //adding services etc var host = builder.Build(); AppContext.SetSwitch(“Npgsql.EnableLegacyTimestampBehavior”, true); … //your other scoped code … Read more

error CS8773: “Feature ‘global using directive’ is not available in C# 9.0” after downgrade from net6.0 to net5.0

Finally I found that the reason is an extra property ImplicitUsings in the project file that is not supported by .net 5.0. <PropertyGroup> <TargetFramework>net5.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> I needed to remove ImplicitUsings from the file.

.NET 6.0 C# “new console template” – how to read CLI arguments?

You can access the command line arguments from anywhere in your code using the Environment class. In particular, you can use Environment.GetCommandLineArgs: string name = Environment.GetCommandLineArgs()[1]; Console.WriteLine($”Hello, {name}!”); Note that the first element in the array contains the path of the executable and the arguments passed to the program start with the second element, i.e. … Read more

Equivalent to UserSettings / ApplicationSettings in WPF for newer .NET versions

You can add the same old good settings file e.g. via the right click on the Properties -> Add -> New Item and search for the “Settings”. The file can be edited in the settings designer and used as in the .net framework projects before (ConfigurationManager, Settings.Default.Upgrade(), Settings.Default.Save, etc. works). Add also the app.config file … Read more

Problem Updating to .Net 6 – Encrypting String

The reason is this breaking change: DeflateStream, GZipStream, and CryptoStream diverged from typical Stream.Read and Stream.ReadAsync behavior in two ways: They didn’t complete the read operation until either the buffer passed to the read operation was completely filled or the end of the stream was reached. And the new behaviour is: Starting in .NET 6, … Read more

How can I read the appsettings.json in a .NET 6 console application?

Add these two nuget packages in your application Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Json Then you can use ConfigurationBuilder to use appsettings.json file var configuration = new ConfigurationBuilder() .AddJsonFile($”appsettings.json”); var config = configuration.Build(); var connectionString = config.GetConnectionString(“ConnectionString”); Getting Values from AppSettings in Console Application Adding AppSettings in .net core app

DateTime.Now equivalent for TimeOnly and DateOnly?

You can use .FromDateTime() method, To get current date only: var dateNow = DateOnly.FromDateTime(DateTime.Now); To get current time only: var timeNow = TimeOnly.FromDateTime(DateTime.Now); For more details, you can go through Github issue. There are several good comments which explains, why .Now property is not introduced to DateOnly and TimeOnly Why .Today, .Now and UtcNow properties … Read more

Comparing two objects with == operator

I am not sure why it changes when you change it from 10 to 1 I believe this is an implementation detail and you should not rely on it (will try to find something in the specs) but some positive single digit numbers are cached in int.ToString implementation for .NET Core. Here is excerpt from … Read more

Error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0

I found the solution here https://jaliyaudagedara.blogspot.com/2021/07/azure-devops-building-projects.html It works if I specify the .NET Core SDK version & set preview version to true – task: UseDotNet@2 displayName: ‘Use .NET Core sdk’ inputs: packageType: ‘sdk’ version: ‘6.0.x’ includePreviewVersions: true So my final pipelines looks something like this # .NET Core Function App to Windows on Azure # … Read more

Adding ADO.Net Entity Framework gives “The project’s target framework does not contain Entity Framework runtime assemblies”

I know that almost all the other answers recommends to change the target framework, but some users (including me) needs to use .NET 6.0 instead of .NET Framework, so that solution its not valid for us. I was able to create the models by using Paul Sinnema’s link and using SQL authentication instead of Windows … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)