Posts

Showing posts from 2019

Demystifying User Secrets in .Net Core

Image
User Secrets are a convenient way to store local configuration values in your .Net Core applications. When you create a new .Net Core application, a usersecrets.json file will be automatically created for you to store your local config values. You can manage the values inside your solution by opening up the json file through the web project context menu. Inside this file, you will see an empty object by default. You can copy over your entire appsettings.json or specific sections and modify them as you see fit for local use. When you run the solution, the config values in usersecrets.json will merge with appsettings.json and overwrite any values that match. If you want to see where the file is located, right-click the open tab in VS and click "Open Containing Folder". This will take you to your local AppData > Roaming directory. If you back out of this directory, you will see folders for all of your apps and you will notice that MS uses g

CQRS db Contexts with .NET Core

We're developing a new app using .NET Core 2.2 and it's time for some redesign. When it came to EF db Contexts we wanted to separate our Reads from our Writes (a la CQRS) more explicitly. We needed to work with the same entities in both BUT we still wanted to optimize performance with Reads and customize Writes separately so below is a quick and easy implementation. Base Context Setup a Base Context class that you will inherit from; you can either inherit from DbContext or in this case IdentityDbContext since we're also leveraging Identity Core. public abstract class BaseContext <TContext> : IdentityDbContext<User> where TContext : DbContext { protected BaseContext (DbContextOptions<TContext> options) : base (options) { } protected override void OnModelCreating (ModelBuilder modelBuilder) { base .OnModelCreating(modelBuilder); } // TODO add DbSet<> for each entity/table (as needed) public DbSet<Audit> Audit { get ; s