How to handle Many-To-Many in Entity Framework Core

Many-To-Many relationship in not as straightforward as other relationships. In this post I’ll show you how to create many-to-many relationships and how to use them in EF Core. The model Simple and practical example of many-to-many could be some sort of digital ecommerce store. Users can put items into shopping cart (one shopping cart can have many items), whereas items belong to multiple carts. Lets start off by creating Cart and Item classes.

Enumerable.Range by example

C# Enumerable.Range function generates sequence of numbers. This article explores details of the Range function as well as its use cases. Enumerable.Range overview It takes two arguments. First argument is the first number in sequence (e.g. 10, means that the first number in sequence is 10). Second argument is the number of items in sequence (e.g. 11 means that it will return 11 numbers). It returns IEnumerable<int> which contains generated sequence.

ASP.NET Core/Razor Pages localize nested classes

You probably already know how to localize regular classes in ASP.NET Core web app. It’s rather easy, at least in terms of resource files. Sometimes however we use nested classes which requires special way of naming RESX files. As it turns out it is actually pretty easy to do. If you want the answer right away, then here it is. You have to use + sign in the name of your resource file (like this: OuterClass+NestedClass.

Iterate dictionary in C#

This time I’m going to give a few quick tips for dictionaries in C#. Some basic points before we begin: Dictionary is an unordered data structure Dictionary maps keys to values Keys have to be unique Dictionary implements IEnumerable Let’s start by creating a simple dictionary with some data: 1 2 3 4 5 6 // e.g. name and birth date var people = new Dictionary<string, DateTime> { { "John", new DateTime(2000, 1, 1) }, { "Jane", new DateTime(2000, 1, 2) } }; Looping through dictionary is simple, since it implements IEnumerable all we have to do is to use foreach loop.

Where() Select()

This post is quick intro for beginners. I’ll introduce you to 2 Linq methods Select and Where. We use those functions to manipulate data set. All 2 can be applied to all types implementing IEnumerable interface. It means that you can apply them to data retrieved from database (IQueryable), list (List/IList implements IEnumerable), arrays (which also implements IEnumerable) and others. It’s important to know that each one of those functions return new IEnumerable.

Razor Pages ajax modals with validation

This tutorial will show you how to create ajax powered Bootstrap modals in Razor Pages. You’ll learn how to dynamically load partial content, how to send/save your form via ajax and how to validate it using Razor Pages as a backend technology. Final proof-of-concept source code is in this GitHub repository. As a side note, if you want to see how to the same thing in ASP.NET Core then go to my other tutorial: ASP.

C# file status (readable, writeable, is file, is directory)

Every once in a while you have to work with files. This article will teach you how to check wheter or not file is readable or writeable. You’re also going to learn how to check if given file or directory exists. Quick intro to C# File, FileInfo, Directory and DirectoryInfo classes All four classes reside inside System.IO namespace. It’s important to notice the differences between those clasesses, especially in terms of not-Info and Info classes.

Handling keyboard shortcuts in JavaScript

Keyboard shortcuts are not only for desktop applications. You can also create shortcuts for web applications, which can come in handly in your MPA or SPA apps. Read on to find out how to create keyboard shortcuts in JavaScript. The key events Inside web browsers we have 3 distinct events related to keyboard events. keyup – fired after you release a key keypress – fired once you press a key, which produces a character value like a letter or number, but not for special characters like CTRL keydown – fired after you press a key We can use these events to call desired function.

Strings in C# - concatenating (joining) and splitting

As a programmer you’ll always have to deal with the strings. In this article you’ll learn the most basic string operations: concatenating/joining and splitting. Concatenating/joining strings Concatenation means that you append a one string at the end of another string. This happens directly or indirectly, but you should know that there are a few ways you can join strings in C#. The + operator The simplest method of joining strings together is the + operator.

ASP.NET CORE OWASP TOP 10 - Cross-Site Scripting (XSS)

Cross-site scripting (known also as XSS) is a type of attack aimed at web application users. Attacker injects client-side code (typically a JavaScript) into vulnerable web application in such a way that the script is run on on users browsers visiting vulnerable page. Imagine that you’ve build an web application allowing your users to send private messages to each other. One of the users finds out that you do not encode messages, so it is possible to send pure HTML or JavaScript code to other person.