Posts

Showing posts with the label C#

Adding Multiple DB Contexts in your DotNet Project

Introduction  In this article we will discuss adding multiple DB contexts to our DotNet projects, we will use EF core as our ORM to make a "Code First Database" approach.  In this article example, we will use DotNet 7 connecting to SQL Server through EF Core 7, all of this will be in a web API project to see how to define those DB contexts as services in program.cs  file.  You can download the example source code from here .  Why would we need multiple DB Contexts in our Solution?  There are two situations where you need multiple DB Context in your project or Solution:  We have a single database but need multiple  schemas in our database, and each scheme will have a group of entities.  We have Multiple databases and our project needs to connect them to do its operations.  We will take the first situation as our code example. Creating Our models Let's assume we have a si...

How to Automatically deploy Configuration Files with Azure DevOps CD pipelines

Introduction Part of the CI/CD Automation is to add environment-related configuration files through deployment automatically, rather than manually uploading them. This will provide less human error and more security by splitting the developer's responsibility of knowing the environment configuration or editing it, as to be handled by Operation side. We will take an example of CI for DotNet Core 6 WebApi project, and on that project, we need to copy the related appsetting.json to its environment. Pre-Requested Knowledge: To benefit from this article you need to be familiar with the following:  Azure DevOps. the Concept of CI/CD. Azure CI/CD Pipelines. DotNet Core Web Api Deployment (or similar). Adding From Source Code Branch In some cases, we will have configuration files included in our source code, for example, in...

Writing code is a mind and time-consuming process, a short story to prove

 Two days ago, while I’m trying to test a Customer module that I had codded, testing some data insertion from my code, I found out my insertion code on exception does not roll-back the insertion, I use to make this kind of transaction handling  at the DBMS on the sorted procedures, but I decided to make this from my code this time, so I searched that, and on C# .net you can use what called TransactionScope , which I implemented successfully, I wasted another hour on reading about the other types of transactions that available on .net framework, implicit and explicit , on one method or through the whole methods and across threads, “I hated threads!” I said, “Let's check about it … no! let’s get focused”.  So I continued my tests and based on some new requirements that I just remembered to do, I have to add forging a key relationship between the Customer table and Company table, that each customer MUST be linked to a company that s/he worked at, this is a simple change, r...