2020
Entity Framework Core - preview model
In this article, I show you VisualStudio extension EFCorePowerTools written by Erik Ejlskov Jensen which lets you see how Entity Framework Core interpreting your model. This extension is very helpful to avoid problems when EF Core interprets the model in a different way than you anticipated. EF Core has some conventions that you should follow to receive the desired database schema. With this extension, you don’t have to apply the migration to the database to see if you didn’t make any mistakes in your model. You can preview the model in the graph or you can generate SQL scripts to see what will be generated in your database.
WPF - create custom button with image (ImageButton)
In this blog post, I will show you how you can create a custom control ImageButton. In this control, we will use Grid control to locate our image and the content of the button will be always in the center of it. You don’t have to specify the button content - in this case it will be an image button only. ImageButton inherits from Button so you can access all button’s properties. Additionally, you can set the following properties:
- ImageSource
- ImageLocation (Left, Top, Right, Bottom, Center)
- ImageWidth
- ImageHeight
ASP.NET Core Tag Helpers - Cache Busting
Have you ever had a problem with stale JavaScript or CSS files in browsers using by your customers? How many times did you explain to your customers that they should clear the cache? And how many times did you hear the following words “How do I do that?”? If you don’t want to have these problems in the future please read this blog post.
How to enable Browser Link in ASP.NET Core 3.1
This guide will step you through the process of enabling Browser Link in ASP.NET Core project.
2019
Secure static files in ASP.NET Core
The Static File Middleware doesn’t provide authorization, all files served by this middleware are publicly accessible. Static files are stored in the following directories:
- Web root directory -
{content root}/wwwroot
- it’s the default directory - you can also serve files outside the Web root directory by specifying it in the
UseStaticFiles
method as below:
ASP.NET Core - how to bind enum to HTML select tag
In this post, you will see how to bind enum to HTML select tag. Let’s say that you have class Person
and one of its properties is enum (in our case it’s enum Gender
).
SQL query for records that contain a percent character
Let’s say that you have a table (shown below) and you want to select rows where column Name
contains a percentage sign. So in this case query should return rows with ids: 3, 5 and 6.
NuGet - error NU1108: cycle detected
Lately, when I wanted to create a sample program with an IdentityServer4 I bumped into a problem with NuGet.
DevExpress GridControl - change default text by using Localizer class
If you want to translate default text e.g. “Click here to add a new row” in the GridControl
you can use a Localizer class. Each DevExpress component has a specific Localizer class but not all string can be translated via that class.
.NET Core 3 preview - not working in VS2019 main release
If you have installed VS2019 main release (version 16.0.*) you cannot create a new project with target platform .NET Core 3 by default. To make it possible you have two options:
Entity Framework 6 - optimistic concurrency
Concurrency conflicts occur when more than one user wants to edit the same entity stored in the database at the same time. Let’s say that we have two users Mark and John currently using our application and they want to edit the same entity stored in DB which Id is equal to 2 (please refer to image below).
C# 6 feature - using static directive
In C# 6 was introduced using static
directive. By using this language construct you can reference to static class members without specifying a type name.
WPF Validation - Using INotifyDataErrorInfo
In the .NET 4.5 was introduced new interface INotifyDataErrorInfo
which enables data entity classes to implement custom validation rules and expose validation results asynchronously. This interface has three members:
HasErrors
property indicates whether there are any validation errorsGetErrors
method returns anIEnumerable
that contains validation errors for the specified property (when the propertyName parameter isn’t equal to null or empty string) or for the entire entity (when the propertyName parameter is equal to null or empty string)ErrorsChanged
event must occur when the validation errors have changed for a property or for the entity
WPF Validation - Using IDataErrorInfo
In this blog post, I will show you how you can validate data using IDataErrorInfo
. This interface has only two members which need to be implemented:
public string Error { get; }
- gets an error message indicating what is wrong with this objectpublic string this[string columnName] { get; }
- get the error message for the property with the given name.
Entity Framework 6 - speed up reading the data
If you want to read data from the database without need of editing it later you can improve your query by using the AsNoTracking
method. This “read-only query” is quicker to execute because EF will no need to set up change tracking information about every single loaded entity.
WPF Validation - Display errors to the user
In this post, I will show how you can present user input validation errors to the user. By default, WPF shows a red border around the TextBox
when the entered value is invalid. But in this case, our user has no idea what is wrong with entered data. We need to inform the user by providing an error message on the view.
WPF Validation - Using ValidatesOnExceptions
In WPF we have few options to validate user input data. In this article, I will show you validation based on the exception. This validation is applied in very simple solutions. The only thing you should do is throw an exception on property level and set ValidatesOnExceptions
to true in the view.
Entity Framework 6 Code First - add constrains
EF6 has two ways to where you can add constraints to your model. One of them is Fluent API and the other one is Data Annotations.
Entity Framework 6 Code First - singular table names
By default, EF 6 Code First creates a table in your database with the convention that the table name to be a pluralized version of the entity type name.
Json.NET - ignoring properties during serialization
When you’re using Json.NET to serialize an object by default all public fields and properties will be serialized. In this case, if you want to ignore some of them you can use JsonIgnore
attribute. In the following example, you can see how you can ignore SSN
property during the serialization process.
2018
Improve built-in ASP.NET Core DI container with Scrutor
ASP.NET Core has built-in dependency injection (DI) container. It’s easy and simple for the smaller project where you register all your dependencies (e.g. services, repositories, e.t.c.) by hand. Microsoft.Extensions.DependencyInjection has not built-in type scanning functionality, so if your project will grow, using it can be pretty hard. But you can resolve this problem by using Scrutor. Scrutor is an extension for built-in DI that allows you scan assembly and decorate services. In this article, we will look at scanning functionality.
ILSpy - IL code viewer plugin
Recently when I worked with ILSpy I wanted to see the Intermediate Language but I could not find this functionality. So I decided to create a plugin to ILSpy called ILViewer. You can find it on github: https://github.com/kmatyaszek/ILSpy-ILViewer.
C# 7: check null properly
If you develop application with C# 7 and you want to be sure that null is always check properly you should use constant pattern “null”. This is one of the 3 types of patterns in the new feature in C# called “pattern matching”. In this case, you don’t have to bother if in the class is defined override of the equals operator. In the following code snippets, we will go through two types of checking null (is and ==) and with or without override == operator.
JSON Web Token - exp, nbf, iat claims - convert DateTime to Unix time
In JWT following claims must be a number containing a date as a number:
exp
(Expiration Time) Claimnbf
(Not Before) Claimiat
(Issued At) Claim
Extended WPF Toolkit - PropertyGrid AdvancedOptionsMenu - copy button
In this post, I will show you how you can add copy button to WPF Toolkit PropertyGrid. To do this we are going to use AdvancedOptionsMenu
property. In the PropertyGrid
you should set ShowAdvancedOptions
to True
and add context menu to AdvancedOptionsMenu
:
DevExpress WPF Themes - how to disable DevExpress theming on native WPF controls
DevExpress Themes supports painting of the most standard native WPF controls. The more complex native WPF controls are not supported but you can find a corresponding alternative DevExpress control insted.
Quickly Launch Apps with Windows Key + R
In Windows operating system the run command window is the fastest way to access almost all Windows’ function. To open the run command window just press Windows Key + R. After that, you can enter a command to the Open textbox and press Enter. Below I gathered the most useful commands which you can use in daily basis work.
DevExpress WPF Themes - how to apply theme to an entire application
DevExpress theme manager has an important property ApplicationThemeName
. This property allows you to set global theme applied to the entire application. Without settings this property, you have to specify theme name on every new window in your project.
Visual Studio 2013 designer cache & disk space leak
I have recently encountered a problem with using VS2013 designer when I develop WPF application. Problem is with a designer cache which is not cleared out. After about three months I have ended up with 0 bytes on my C drive and I was not able to do anything. When I started searching for the possible cause of this situation I found information about the bug in VS2013 connected with the correctly delete cache data from disk. In my case, about 30GB of storage was being used by VS2013 designer cache.