Recently, while building an app service to host a .NET Core API, I had to implement the logic for using both Read-Only and Read-Write Database Contexts for Entity Framework Core. In this particular case, it was the same database – just different contexts, because depending on the location of the app service the app was deployed in, read and write operations might actually go to different database instances, That’s really easy, right? Well… Yes and no. Essentially, it’s easy to spin up, but comes with…Continue reading How to implement multiple Connection Strings for one DbContext in EF Core?
Sometimes you’ll need the default identity for your ASP.NET Core application, but want to disable the registration of new users. Sounds pretty basic, right? However, there’s actually no easy switch you can just flip to enable or disable said functionality. I went through a lot of different articles and ideas by other people. Most of them involved fiddling with IdentityController, creating new scaffolded pages, adding new Actions with redirection code, and who knows what else… But who wants to fiddle with Identity? It’s glitchy and…Continue reading How to disable “Register” action in ASP.NET Core?
Every now and then comes the need to write your console output to a log file. There’s a simple way to do this in .NET Framework, and quite a few online articles detailing a borderline one-liner on how to do achieve it: Adding a log file listener(s) by calling Debug.Listeners – something like this: TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(tr1); TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt")); Debug.Listeners.Add(tr2); Source However, this doesn’t work in .NET Core anymore. Problem: Everything changes in .NET Core Using the code…Continue reading Adding Debug listeners to your console application fails in .NET Core
This is another, kind of a classic and simple solution to a fairly simple problem: How to fix it, when your DbContextOptionsBuilder fails to take in your connection string and complains about UseSqlServer missing or whatever? Okay – let’s take a step back. How did we end up here? So, let’s set the scene. You’re building your DbContext (called ApplicationDbContext in the example below) in a .NET Core application of some sorts. You’ll do this like shown below: using Microsoft.EntityFrameworkCore; using System; namespace YourNamespace…Continue reading Solving: ‘DbContextOptionsBuilder’ does not contain a definition for ‘UseSqlServer’
Another case, where there’s plenty of documentation, but it just doesn’t seem to be comprehensive enough or just doesn’t apply for any of the cases that I have – for whatever reason. Maybe my cases are wrong, or I’m just bad at googling, but I’ve struggled to find a good page documenting this. Perfect case for another blog post about the topic, then – perhaps someone else will find it useful! This post answers the question: How to get Claims for a user in .NET…Continue reading Iterating Group memberships using Claims in .NET Core
When you’re in your .NET Core project, it’s always easy: you just register your DbContext in ConfigureServices, and then inject it into whichever Page, View or Controller you might need it in. However, when you have another project or solution you’re working on and you’d still like to use the same DbContext and your entity classes in it, you have to find another way to do it! What should we do, then? Description Let’s first take a look at the use cases and tech stack.…Continue reading How to instantiate your DbContext outside your Data project?
Just a quick piece of documentation, as this piece of code has changed in different versions of .NET Core 3.0 so far a few times. Need to have things like this written down somewhere! UriHelper / NavigationManager with .NET Core 3.0 Both of these helper libraries do essentially the same thing: navigate/browse/redirect the user to a different address, component, page or view in or by your web app. What you normally use this library for is exactly the same in all versions I’ve worked with,…Continue reading How to use UriHelper or NavigationManager in .NET Core 3.0 & Blazor?
Welp – just ran into this one, and as I expect others to encounter the same issue, better document in right away! So in Blazor, I was crafting a simple component that’d display some data in a nice way. Being lazy and crafty, I thought I’d copy-paste some similar code from an earlier POC I had built, since I knew it worked. The component was overriding a method called “OnInitAsync”, and it just nicely fetched data after initialized. But alas, it didn’t. Not anymore. I…Continue reading ‘__generated__Index.OnInitAsync()’: no suitable method found to override
Building a proof-of-concept on .NET Core 3.0 preview 7 I ran into issues when developing web APIs. This release is production-ready (according to Microsoft -see below), but every now and then you run into really unintuitive and sometimes surprising issues. Go LiveNET Core 3.0 Preview 7 is supported by Microsoft and can be used in production. We strongly recommend that you test your app running on Preview 7 before deploying Preview 7 into production. If you find an issue with .NET Core 3.0, please file…Continue reading How to resolve build failing with .NET Core 3 and Microsoft.AspNetCore.Mvc.Versioning