Suppress "Use 'throw' expression" suggestion

C# 7 has introduced new features: pattern matching, our variables, tuples, and many more. It's also introduced throw expression. This allows to use the throw keyword in an expression. For instance: _foo = foo ?? throw new ArgumentNullException(nameof(foo)); parts.Length > 0 ? parts[0] : throw new InvalidOperationException(); Adding new syntaxes is great, but I clearly don't like this one to check the… [read more]

Entity Framework Core: Specifying data type length and precision

Note: I use the preview version of Entity Framework Core 2.0 (2.0.0-preview2-final). Things may change after the final version is released ORMs like Entity Framework Core provide great abstractions of the database. However, it should not prevent you from looking at the generated code. In this post, I want to draw attemption to the schema generation. Indeed, in a relational database, using the right… [read more]

Entity Framework Core: View the model as text

Note: I use the preview version of Entity Framework Core 2.0 (2.0.0-preview2-final). Things may change after the final version is released In the previous posts, I edited the model to change the names of the generated objects or add properties. Instead of generating the migration script to view what is generated, you can use the ToDebugString extension method to view the content of the model. using… [read more]

Validating user with cookie authentication in ASP.NET Core 2

In a previous post, I wrote about the cookie authentication in ASP.NET Core 2. The cookie authentication does 2 things: Write a cookie with encrypted data when the user logs in Read the cookie, decrypt it, and set the request identity (Request.User.Identity) When it read the cookie and set the identity, it doesn't check the user actually exists. For instance, John logs in on browser A, then, he deletes… [read more]

Use brotli compression with ASP.NET Core

Brotli is a compression algorithm. In general, it provides better compression results than gzip. For instance, the size of the default page of an ASP.NET Core project using gzip is 1797 bytes, and using Brotli, it's only 1333 bytes. So it's a 25% reduction! You can use Brotli with most of the major browsers (source): So, it's time to support Brotli in addition to gzip! I've already written about the gzip… [read more]

Entity Framework Core: Soft Delete using Query Filters

Note: I use the preview version of Entity Framework Core 2.0 (2.0.0-preview2-final). Things may change after the final version is released I continue my exploration of Entity Framework Core 2.0. Today, I'll explore how to use soft-delete (or logical delete) easily. I mean implementing soft-delete in a transparency way, e.g. as if you were actually deleting rows. To implement soft delete, you need to add a… [read more]

Entity Framework Core: Generate tracking columns

Note: I use the preview version of Entity Framework Core 2.0 (2.0.0-preview2-final). Things may change after the final version is released When you design a database, you sometimes need to add columns to track when a record changed and who made the change. To clarify, you add the following columns: CreatedAt CreatedBy LastUpdatedAt LastUpdatedBy You can easily use default values and triggers to handle the… [read more]