Sunday, 6 July 2025

Blazorise Datepicker with highlighting of dates

The Blazorise Datepicker is a date picker control for setting dates in Blazor WASM web assembly apps. It uses Flatpickr to provide a user friendly UI for selecting a date and navigate among months and years. It is used in web apps that use Blazor. The date picker is documented here :
https://blazorise.com/docs/components/date-picker
This date picker does not support highlighting certain dates. It do support enabling specified dates or disabling specified dates. But highlighting dates is not possible. I have added a Github repo where this is added support for :

https://github.com/toreaurstadboss/BlazoriseDatePickerWithHolidays

First off, the following custom Blazor component provides the custom date picker with the support for highlighting specified dates.

CustomDatePicker.razor




@using Blazorise
@using Blazorise.Localization
@using BlazoriseDatePickerWithHolidays.Service
@using System.Globalization
@using static BlazoriseDatePickerWithHolidays.Service.HolidayService
@inject IJSRuntime JS
@inject ITextLocalizerService TextLocalizerService

<Addons>
    <Addon AddonType="AddonType.Body">
        <DatePicker TValue="DateTime"
                    Date="@SelectedDate"
                    FirstDayOfWeek="@FirstDayOfWeek"
                    DateChanged="@OnDateChanged"
                    InputFormat="@InputFormat"   
                    InputMode="@InputMode"
                    DisplayFormat="@DisplayFormat"                    
                    Placeholder="@Placeholder"
                    TimeAs24hr="@TimeAs24hr"
                    @attributes="@AdditionalAttributes"
                    @ref="datePicker" />
    </Addon>
    <Addon AddonType="AddonType.End">
        <Button Class="addon-margin" Color="Color.Primary" Clicked="@(() => datePicker?.ToggleAsync())">
            <Icon Name="IconName.CalendarDay" />
        </Button>
    </Addon>
</Addons>

@code {
    /// <summary>
    /// List of dates to be highlighted in the date picker. Each date can have an annotation (e.g., holiday name) that will be shown as a tooltip.
    /// </summary>
    [Parameter]
    [EditorRequired]
    public List<AnnotatedDateTime> HighlightedDays { get; set; }

    /// <summary>
    /// The CSS class to apply to highlighted dates in the calendar.
    /// </summary>
    [Parameter]
    public string HighlightCssClass { get; set; } = "pink-day"; //Default CSS class pink-day is a custom CSS class defined in wwwroot/css/app.css

    /// <summary>
    /// The first day of the week in the date picker calendar. Defaults to Monday.
    /// </summary>
    [Parameter]
    public DayOfWeek FirstDayOfWeek { get; set; } = DayOfWeek.Monday;

    /// <summary>
    /// Whether to display time in 24-hour format. Defaults to true.
    /// </summary>
    [Parameter]
    public bool TimeAs24hr { get; set; } = true;

    /// <summary>   
    /// The locale to use for the date picker, which affects date formatting and localization. 
    /// Supported locales : https://blazorise.com/docs/helpers/localization
    /// </summary>
    [Parameter]
    public string Locale { get; set; } = "en-US";

    /// <summary>
    /// The input mode for the date picker, which can be either Date or DateTime or Month.
    /// </summary>
    [Parameter]
    public DateInputMode InputMode { get; set; } = DateInputMode.Date;

    /// <summary>
    /// The currently selected date in the date picker.
    /// </summary>
    [Parameter] 
    public DateTime SelectedDate { get; set; }

    /// <summary>
    /// Event callback triggered when the selected date changes.
    /// </summary>
    [Parameter] 
    public EventCallback<DateTime> SelectedDateChanged { get; set; }

    /// <summary>
    /// The input format string for displaying the date in the input field.
    /// </summary>
    [Parameter] 
    public string? InputFormat { get; set; } = "dd.MM.yyyy";

    /// <summary>
    /// The display format string for showing the date in the calendar.
    /// </summary>
    [Parameter] 
    public string? DisplayFormat { get; set; } = "dd.MM.yyyy";

    /// <summary>
    /// Placeholder text to display when no date is selected.
    /// </summary>
    [Parameter] 
    public string? Placeholder { get; set; }

    /// <summary>
    /// Whether to show the clear button in the date picker.
    /// </summary>
    [Parameter] 
    public bool ShowClearButton { get; set; } = true;

    /// <summary>
    /// Additional attributes to be splatted onto the underlying DatePicker component.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)] 
    public Dictionary<string, object>? AdditionalAttributes { get; set; }

    /// <summary>
    /// Handles component rendering and registers JS interop for month and year changes.
    /// </summary>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _dotNetRef = DotNetObjectReference.Create(this);
            await JS.InvokeVoidAsync("registerFlatpickrMonthChange", "input.flatpickr-input", _dotNetRef);
            await JS.InvokeVoidAsync("registerFlatpickrYearChange", "input.flatpickr-input", _dotNetRef);
        }
        await ReloadDatesToHighlight();
    }

    protected override void OnParametersSet()
    {
        if (Locale != null){
            TextLocalizerService.ChangeLanguage(Locale);
        }
    }

    /// <summary>
    /// Invoked from JS when the month is changed in the calendar.
    /// </summary>
    [JSInvokable]
    public async Task OnMonthChanged()
    {
        await ReloadDatesToHighlight();
    }

    /// <summary>
    /// Invoked from JS when the year is changed in the calendar.
    /// </summary>
    [JSInvokable]
    public async Task OnYearChanged()
    {
        await ReloadDatesToHighlight();
    }

    /// <summary>
    /// Disposes JS interop references and the DatePicker component.
    /// </summary>
    public async ValueTask DisposeAsync()
    {
        _dotNetRef?.Dispose();
        if (datePicker != null)
            await datePicker.DisposeAsync();
    }
    
    /// <summary>
    /// Highlights the specified dates in the calendar using JS interop.
    /// </summary>
    private async Task ReloadDatesToHighlight()
    {
        if (HighlightedDays == null)
            return;

        string chosenLocale = this.Locale ?? "en-US";

        //Console.WriteLine("chosenLocale:" + chosenLocale);

        var datesToHighlight = HighlightedDays
            .Select(d => new { annotation = d.Annotation, date = d.Value.ToString("MMMM d, yyyy", new CultureInfo(chosenLocale)) })
            .ToArray();

        //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(datesToHighlight));

        await JS.InvokeVoidAsync(
            "highlightFlatpickrDates",
            ".flatpickr-calendar",
            datesToHighlight,
            HighlightCssClass // Pass the class as an extra argument
        );
    }

    /// <summary>
    /// Optionally provides a CSS class for a given date. Not used in this implementation.
    /// </summary>
    private string GetDateClass(DateTime date)
    {
        return string.Empty;
    }

    /// <summary>
    /// Handles the date change event from the DatePicker and propagates it to the parent component.
    /// </summary>
    private async Task OnDateChanged(DateTime newValue)
    {
        SelectedDate = newValue;
        await SelectedDateChanged.InvokeAsync(newValue);
    }

    private DatePicker<DateTime>? datePicker;
    private DotNetObjectReference<CustomDatePicker>? _dotNetRef;
}


Notice the trick done to capture unmatched values in case some parameters of DatePicker in the custom date picker is missing and we still want the user of this component to set a parameter of the date picker component :


    /// 
    /// Additional attributes to be splatted onto the underlying DatePicker component.
    /// 
    [Parameter(CaptureUnmatchedValues = true)] 
    public Dictionary<string, object>? AdditionalAttributes { get; set; }


The calls to IJSRunTime to register callback script handlers will register callbacks for the component, this is done in this line :

_dotNetRef = DotNetObjectReference.Create(this);
await JS.InvokeVoidAsync("registerFlatpickrMonthChange", "input.flatpickr-input", _dotNetRef); 
await JS.InvokeVoidAsync("registerFlatpickrYearChange", "input.flatpickr-input", _dotNetRef);           

The following client side script is registeret into the global window object to provide script event handlers that will be called when the end-user changes selected year or month, either via the UI controls or just entering the date.

datepicker-highlight.js


window.highlightFlatpickrDates = (selector, dates, highlightCssClass) => {
    //debugger
    const calendar = document.querySelector(selector); 
    if (!calendar) {
        return; //Wait for Flatpickr to render days
    }
    //debugger
    setTimeout(() => {
        //
        dates.forEach(date => {
            //debugger
            const dayElem = calendar.querySelector(`.flatpickr-day[aria-label="${date.date}"]`);
            if (dayElem) {
                dayElem.classList.add(highlightCssClass);
                dayElem.setAttribute('title', date.annotation);
            }
        });
    }, 50);
};

window.registerFlatpickrMonthChange = (selector, dotNetHelper) => {
    const fpInput = document.querySelector(selector);
    if (!fpInput || !fpInput._flatpickr) {
        setTimeout(() => window.registerFlatpickrMonthChange(selector, dotNetHelper), 50);
        return;
    }
    fpInput._flatpickr.config.onMonthChange.push(function () {
        dotNetHelper.invokeMethodAsync('OnMonthChanged');
    });
};

window.registerFlatpickrYearChange = (selector, dotNetHelper) => {
    const fpInput = document.querySelector(selector);
    if (!fpInput || !fpInput._flatpickr) {
        setTimeout(() => window.registerFlatpickrYearChange(selector, dotNetHelper), 50);
        return;
    }
    fpInput._flatpickr.config.onYearChange.push(function () {
        dotNetHelper.invokeMethodAsync('OnYearChanged');
    });
};

The following test page test out the component.

StyledDatePicker.razor



@page "/"
@using BlazoriseDatePickerWithHolidays.Components
@using BlazoriseDatePickerWithHolidays.Service
@using System.Globalization
@using static BlazoriseDatePickerWithHolidays.Service.HolidayService
@inject IHolidayService HolidayService

<h3>Custom Date Picker with Highlighted Days</h3>

<p>

    <b>The date picker used here is from Blazorise library and customized to allow setting days to highlight including tooltips annotating a description of the date being highlighted.</b>
    <br />
    Blazorise DatePicker component is described here:
    <br /><br />
    <a href="https://blazorise.com/docs/components/date-picker">https://blazorise.com/docs/components/date-picker</a>
    <br /><br />
    The highlighted dates selected for this demo are Christian Holidays and other public days off in Norway.
    In Norwegian, these days off are called 'Offentlige høytidsdager'.<br />
    These days are marked with a pink background, white foreground and rounded corners. <br />
    Tooltips are added showing the Holiday name <br/>

</p>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-2">
            <CustomDatePicker
                @bind-SelectedDate="selectedDate" 
                Locale="en-US"
                FirstDayOfWeek="DayOfWeek.Monday"
                InputMode="DateInputMode.DateTime"
                DisplayFormat="HH:mm dd.MM.yyyy"
                InputFormat="HH:mm dd.MM.yyyy"
                TimeAs24hr="true"
                HighlightedDays="_holidays"
                @ref="@_datePicker" />
        </div>
    </div>
</div>

@{
    var chosenLocale = new CultureInfo(_datePicker?.Locale ?? "en-US");
}

<p class="mt-3">Selected date <strong>@selectedDate.ToString("HH:mm dd.MM.yyyy", chosenLocale)</strong></p>

@code {
    private DateTime selectedDate = new DateTime(2025, 5, 4);
    private static int minYear = DateTime.Today.AddYears(-20).Year;
    private List<AnnotatedDateTime>? _holidays;

    private CustomDatePicker? _datePicker;

    protected override void OnParametersSet()
    {  
        _holidays = Enumerable.Range(minYear, 40)
            .SelectMany(y => HolidayService.GetHolidays(y))
            .ToList();
    }

}


The dates to select uses an implementation of IHolidayService. This implemented like this, note that this finds 'Offentlige høytidsdager' and (Christian-belief) holidays in Norway.

HolidayService.cs



namespace BlazoriseDatePickerWithHolidays.Service;

public interface IHolidayService
{
    DateTime AddWorkingDaysToDate(DateTime date, int days);
    List<string> GetHolidayNames();
    IEnumerable<HolidayService.AnnotatedDateTime> GetHolidays(int year);
    bool IsHoliday(DateTime date);
    bool IsWorkingDay(DateTime date);
}

public partial class HolidayService : IHolidayService
{

    private static MemoryCache<int, HashSet<AnnotatedDateTime>> holidays = new();

    private static readonly List<string> holidayNames = new List<string> {
            "1. nyttårsdag",
            "Palmesøndag",
            "Skjærtorsdag",
            "Langfredag",
            "1. påskedag",
            "2. påskedag",
            "Offentlig høytidsdag",
            "Grunnlovsdag",
            "Kristi Himmelfartsdag",
            "1. pinsedag",
            "2. pinsedag",
            "1. juledag",
            "2. juledag"
    };

    /// <summary>
    /// Adds the given number of working days to the given date. A working day is
    /// specified as a regular Norwegian working day, excluding weekends and all
    /// national holidays.
    /// 
    /// Example 1:
    /// - Add 5 working days to Wednesday 21.03.2007 -> Yields Wednesday
    /// 28.03.2007. (skipping saturday and sunday)
    /// 
    /// Example 2:
    /// - Add 5 working days to Wednesday 04.04.2007 (day before
    /// easter-long-weekend) -> yields Monday 16.04.2007 (skipping 2 weekends and
    /// 3 weekday holidays).
    /// </summary>
    /// <param name="date">The original date</param>
    /// <param name="days">The number of working days to add</param>
    /// <returns>The new date</returns>
    public DateTime AddWorkingDaysToDate(DateTime date, int days)
    {
        var localDate = date;
        for (var i = 0; i < days; i++)
        {
            localDate = localDate.AddDays(1);
            while (!IsWorkingDay(localDate))
            {
                localDate = localDate.AddDays(1);
            }
        }
        return localDate;
    }

    /// <summary>
    /// Will check if the given date is a working day. That is check if the given
    /// date is a weekend day or a national holiday.
    /// </summary>
    /// <param name="date">The date to check</param>
    /// <returns>true if the given date is a working day, false otherwise</returns>
    public bool IsWorkingDay(DateTime date)
    {
        return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday
               && !IsHoliday(date);
    }

    public List<string> GetHolidayNames()
    {
        return holidayNames;
    }

    /// <summary>
    /// Check if given Date object is a holiday.
    /// </summary>
    /// <param name="date">date to check if is a holiday</param>
    /// <returns>true if holiday, false otherwise</returns>
    public bool IsHoliday(DateTime date)
    {
        var year = date.Year;
        var holidaysForYear = GetHolidaySet(year);
        foreach (var holiday in holidaysForYear)
        {
            if (CheckDate(date, holiday.Value))
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// Return a sorted array of holidays for a given year.
    /// </summary>
    /// <param name="year">The year to get holidays for</param>
    /// <returns>Holidays, sorted by date</returns>
    public IEnumerable<AnnotatedDateTime> GetHolidays(int year)
    {
        var days = GetHolidaySet(year);
        var listOfHolidays = new List<AnnotatedDateTime>(days);
        listOfHolidays.Sort((date1, date2) => date1.Value.CompareTo(date2.Value));
        return listOfHolidays;
    }

    /// <summary>
    /// Get a set of holidays for a given year
    /// </summary>
    /// <param name="year">The year to get holidays for</param>
    /// <returns>Holidays for year</returns>
    private IEnumerable<AnnotatedDateTime> GetHolidaySet(int year)
    {
        if (holidays == null)
        {
            holidays = new MemoryCache<int, HashSet<AnnotatedDateTime>>();
        }
        if (holidays.Get(year) == null)
        {
            var yearSet = new HashSet<AnnotatedDateTime>();

            // Add set holidays.
            yearSet.Add(new AnnotatedDateTime("1. nyttårsdag " + year, new DateTime(year, 1, 1)));
            yearSet.Add(new AnnotatedDateTime("Offentlig høytidsdag " + year, new DateTime(year, 5, 1)));
            yearSet.Add(new AnnotatedDateTime("Grunnlovsdag " + year, new DateTime(year, 5, 17)));
            yearSet.Add(new AnnotatedDateTime("1. juledag " + year, new DateTime(year, 12, 25)));
            yearSet.Add(new AnnotatedDateTime("2. juledag " + year, new DateTime(year, 12, 26)));

            // Add movable holidays - based on easter day.
            var easterDay = GetEasterDay(year);

            // Sunday before easter.
            yearSet.Add(new AnnotatedDateTime("Palmesøndag " + year, easterDay.AddDays(-7)));

            // Thurday before easter.
            yearSet.Add(new AnnotatedDateTime("Skjærtorsdag " + year, easterDay.AddDays(-3)));

            // Friday before easter.
            yearSet.Add(new AnnotatedDateTime("Langfredag " + year, easterDay.AddDays(-2)));

            // Easter day.
            yearSet.Add(new AnnotatedDateTime("1. påskedag " + year, easterDay));

            // Second easter day.
            yearSet.Add(new AnnotatedDateTime("2. påskedag " + year, easterDay.AddDays(1)));

            // "Kristi himmelfart" day.
            yearSet.Add(new AnnotatedDateTime("Kristi Himmelfartsdag " + year, easterDay.AddDays(39)));

            // "Pinse" day.
            yearSet.Add(new AnnotatedDateTime("1. pinsedag " + year, easterDay.AddDays(49)));

            // Second "Pinse" day.
            yearSet.Add(new AnnotatedDateTime("2. pinsedag " + year, easterDay.AddDays(50)));

            holidays.Add(year, yearSet);
        }
        return holidays.GetAsValue(year)!;
    }

    /// <summary>
    ///  Calculates easter day (sunday) by using Spencer Jones formula found here:
    ///  http://no.wikipedia.org/wiki/P%C3%A5skeformelen
    /// </summary>
    /// <param name="year">year</param>
    /// <returns>easterday for year</returns>
    private DateTime GetEasterDay(int year)
    {
        int a = year % 19;
        int b = year / 100;
        int c = year % 100;
        int d = b / 4;
        int e = b % 4;
        int f = (b + 8) / 25;
        int g = (b - f + 1) / 3;
        int h = (19 * a + b - d - g + 15) % 30;
        int i = c / 4;
        int k = c % 4;
        int l = (32 + 2 * e + 2 * i - h - k) % 7;
        int m = (a + 11 * h + 22 * l) / 451;
        int n = (h + l - 7 * m + 114) / 31; // This is the month number.
        int p = (h + l - 7 * m + 114) % 31; // This is the date minus one.

        return new DateTime(year, n, p + 1);
    }

    private bool CheckDate(DateTime date, DateTime other)
    {
        return date.Day == other.Day && date.Month == other.Month;
    }

}


Also, some CSS rules are added here for the custom control :

App.css



.flatpickr-day.pink-day {
    background: #ff69b4 !important; /* Hot pink */   
    color: white !important;
    border-radius: 50%;
}

.addon-margin {
    margin-left: 0.25rem !important; /* Adjust as needed */
}


Finally, a screenshot showing how the Custom date picker works. We have highlighted dates in May 2025 here, I chose to generate holidays for +- 20 years here. Obviously, an optimization would be the possibility to only pass in the chose year of the date picker and not a wide range of years. This is however looking to be fast anyways and I believe you can pass in a large number of dates to highlight here. Hopefully, this article has given you more inspiration how to add highlighting of dates into the Blazorise Datepicker component for Blazor !

Tuesday, 1 July 2025

Pluralizers in Entity Framework

In Entity Framework, you can implement a IPluralizationService to control how entities' names will be pluralized. In additional singularalized. The standard setup is an English pluralizer. In addition, a Spanish pluralizer is available. If you want to support another language for pluralization (and singularization), I have added a sample of this in the following Github repo of mine :

https://github.com/toreaurstadboss/BulkOperationsEntityFramework

Note that the Norwegian Pluralization service could also pluralize words in English and try to check if the word to pluralize is either English or Norwegian. It is about 500,000 English nouns and 100,000, so it might be hard to create a perfect pluralizer for both English or Norwegian. A list of about 40,000+ nouns are available here: https://gist.github.com/trag1c/f74b2ab3589bc4ce5706f934616f6195/ The Norwegian Pluralization service next could use that wordlist to check if the the word is English and use the standard built in English pluralizer service. I will give an updated version of the NorwegianPluralizationService at the end of the article.

NorwegianPluralizationService.cs



using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Pluralization;
using System.Diagnostics;
using System.Linq;

namespace BulkOperationsEntityFramework.Lib.Services
{

    /// <summary>
    /// Sources for the pluralization rules for Norwegian language:
    /// https://toppnorsk.com/2018/11/18/flertall-hovedregler/
    /// </summary>
    public class NorwegianPluralizationService : IPluralizationService
    {

        public static List<string> PluralizedWords = new List<string>();

        public string Pluralize(string word)
        {
            if (PluralizedWords.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                return word; // Return the already pluralized word
            }

//#if DEBUG
//            Debugger.Break();
//            Debugger.Launch(); // Uncomment this line to break into the debugger when this method is called, for example when database migrations are made with EF Code First
//#endif

            word = NormalizeWord(word);

            string pluralizedWord;

            if (_specialCases.ContainsKey(word))
            {
                pluralizedWord = _specialCases[word];
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsChangingVowelToÆ.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                if (word.Equals("Håndkle", StringComparison.OrdinalIgnoreCase))
                {
                    pluralizedWord = "Håndklær";
                    PluralizedWords.Add(pluralizedWord);
                    return pluralizedWord;
                }
                pluralizedWord = word.Replace("å", "æ").Replace("e", "æ") + "r";
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsForUnits.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsForRelatives.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                switch (word.ToLower())
                {
                    case "far": pluralizedWord = "Fedre"; break;
                    case "mor": pluralizedWord = "Mødre"; break;
                    case "datter": pluralizedWord = "Døtre"; break;
                    case "søster": pluralizedWord = "Søstre"; break;
                    case "fetter": pluralizedWord = "Fettere"; break;
                    case "onkel": pluralizedWord = "Onkler"; break;
                    case "svigerbror": pluralizedWord = "Svigerbrødre"; break;
                    case "svigerfar": pluralizedWord = "Svigerfedre"; break;
                    case "svigersøster": pluralizedWord = "Svigersøstre"; break;
                    case "svigermor": pluralizedWord = "Svigermødre"; break;
                    case "bror": pluralizedWord = "Brødre"; break;
                    default: pluralizedWord = word; break;
                }
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsNeutralGenderEndingWithEumOrIum.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                if (word.EndsWith("eum"))
                    pluralizedWord = word.Substring(0, word.Length - 3) + "eer";
                else if (word.EndsWith("ium"))
                    pluralizedWord = word.Substring(0, word.Length - 3) + "ier";
                else
                    pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsNoPluralizationForNeutralGenderOneSyllable.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordChangingVowelsInPluralFemale.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = NormalizeWord(word.ToLower().Replace("å", "e").Replace("a", "e") + "er");
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsChangingVowelsInPluralMale.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                string rewrittenWord = NormalizeWord(word.Replace("o", "ø"));
                if (rewrittenWord.Equals("føt", StringComparison.OrdinalIgnoreCase))
                    pluralizedWord = rewrittenWord + "ter";
                else if (rewrittenWord.EndsWith("e"))
                    pluralizedWord = rewrittenWord + "r";
                else if (!rewrittenWord.EndsWith("er"))
                    pluralizedWord = rewrittenWord + "er";
                else
                    pluralizedWord = rewrittenWord;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_nonEndingWordsInPlural.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            // General rules
            if (word.EndsWith("er"))
                pluralizedWord = word.Substring(0, word.Length - 2) + "ere";
            else if (word.EndsWith("el"))
                pluralizedWord = word.Substring(0, word.Length - 2) + "ler";
            else if (word.EndsWith("e"))
                pluralizedWord = word + "r";
            else if (word.EndsWith("en"))
                pluralizedWord = word + "er";
            else
                pluralizedWord = word + "er";

            PluralizedWords.Add(pluralizedWord);
            return pluralizedWord;
        }

        public string Singularize(string word)
        {
            word = NormalizeWord(word);

            // Reverse special cases
            var specialSingular = _specialCases.FirstOrDefault(kvp => kvp.Value.Equals(word, StringComparison.OrdinalIgnoreCase));
            if (!specialSingular.Equals(default(KeyValuePair<string, string>)))
                return specialSingular.Key;

            // Words that are the same in singular and plural
            if (_nonEndingWordsInPlural.Contains(word, StringComparer.OrdinalIgnoreCase) ||
                _wordsNoPluralizationForNeutralGenderOneSyllable.Contains(word, StringComparer.OrdinalIgnoreCase) ||
                _wordsForUnits.Contains(word, StringComparer.OrdinalIgnoreCase))
                return word;

            // Irregulars and vowel changes (expand as needed)
            if (word.Equals("Bøker", StringComparison.OrdinalIgnoreCase)) return "Bok";
            if (word.Equals("Føtter", StringComparison.OrdinalIgnoreCase)) return "Fot";
            if (word.Equals("Brødre", StringComparison.OrdinalIgnoreCase)) return "Bror";
            if (word.Equals("Menn", StringComparison.OrdinalIgnoreCase)) return "Mann";
            if (word.Equals("Kvinner", StringComparison.OrdinalIgnoreCase)) return "Kvinne";
            if (word.Equals("Gutter", StringComparison.OrdinalIgnoreCase)) return "Gutt";
            if (word.Equals("Netter", StringComparison.OrdinalIgnoreCase)) return "Natt";
            if (word.Equals("Tær", StringComparison.OrdinalIgnoreCase)) return "Tå";
            if (word.Equals("Tenner", StringComparison.OrdinalIgnoreCase)) return "Tann";
            if (word.Equals("Trær", StringComparison.OrdinalIgnoreCase)) return "Tre";
            if (word.Equals("Knær", StringComparison.OrdinalIgnoreCase)) return "Kne";
            if (word.Equals("Bønder", StringComparison.OrdinalIgnoreCase)) return "Bonde";
            if (word.Equals("Hender", StringComparison.OrdinalIgnoreCase)) return "Hand";
            if (word.Equals("Døtre", StringComparison.OrdinalIgnoreCase)) return "Datter";
            if (word.Equals("Fedre", StringComparison.OrdinalIgnoreCase)) return "Far";
            if (word.Equals("Mødre", StringComparison.OrdinalIgnoreCase)) return "Mor";
            if (word.Equals("Søstre", StringComparison.OrdinalIgnoreCase)) return "Søster";
            if (word.Equals("Øyne", StringComparison.OrdinalIgnoreCase)) return "Øye";

            // "ler" ending (from "el")
            if (word.EndsWith("ler"))
            {
                return word.Substring(0, word.Length - 2);
            }
            if (word.EndsWith("ter"))
            {
                return word.Substring(0, word.Length - 1);
            }

            // "ere" ending (from "er" ending in singular, e.g. "Lærere" -> "Lærer")
            if (word.EndsWith("ere"))
                return word.Substring(0, word.Length - 1);

            // "er" ending (general case, e.g. "Biler" -> "Bil", "Stoler" -> "Stol", "Jenter" -> "Jente")
            if (word.EndsWith("er"))
                return word.Substring(0, word.Length - 2);

            // "r" ending (from "e" ending in singular, e.g. "Jenter" -> "Jente" already handled above)
            if (word.EndsWith("r"))
            {
                var possibleSingular = word.Substring(0, word.Length - 1);
                return possibleSingular;
            }

            // Default: return as is
            return word;
        }

        /// <summary>
        /// Make the world normalized, i.e. first letter upper case and rest lower case letters, the word is trimmed.
        /// Not considering using invariant culture here, as this is a Norwegian pluralization service.
        /// </summary>
        /// <remarks>In case an empty word (null or empty) is passed in, just return the word.
        /// Edge case: In case just One non-empty letter was passed in, make the word also uppercase.</remarks>
        private string NormalizeWord(string word)
        {
            word = word?.Trim();
            if (string.IsNullOrEmpty(word) || word.Trim().Length <= 1) {
                return word?.ToUpper();
            }
            return word.Substring(0, 1).ToUpper() + word.Trim().ToLower().Substring(1);
        }

        private string[] _nonEndingWordsInPlural = new string[] {
            "mus", "sko", "ski", "feil", "ting" }; // Add more non-ending words in plural as needed

        private string[] _wordsChangingVowelsInPluralMale = new string[]
        {
            "bonde", "fot", "bok", "bot", "rot"
        };

        private Dictionary<string, string> _specialCases = new Dictionary<string, string>
        {
            { "Mann", "Menn" } , // 'mann' => 'menn'
            { "Barn", "Barn" }, // 'barn' => 'barn' (no pluralization)
            { "Øye", "Øyne" }, // 'øye' => 'øyne' (plural form of 'eye') //consider adding more special cases here in case all the other pluralization rules do not cover the given word
        };

        private string[] _wordsChangingVowelToÆ = new string[]
        {
            "Håndkle", "Kne", "Tre", "Tå"
        };

        private string[] _wordsForUnits = new string[]
        {
            "meter", "centimeter", "millimeter", "kilometer", "gram", "kilogram", "tonn", "liter", "desiliter", "centiliter", "dollar", "lire",
            "pesetas", "euro", "yen", "franc", "pund", "rupee", "ringgit", "peso", "real", "won", "yuan"
        };

        private string[] _wordChangingVowelsInPluralFemale = new string[]
        {
            "and", "hand", "hånd", "natt", "stang", "strand", "tang", "tann"
        };

        private string[] _wordsForRelatives = new string[]
        {
            "far", "mor", "datter", "fetter", "onkel", "bror", "svigerbror", "svigerfar", "svigermor", "svigersøster", "søster"
        };

        private string[] _wordsNoPluralizationForNeutralGenderOneSyllable = new string[]
        {
            "hus", "fjell", "blad"
        };

        private string[] _wordsNeutralGenderEndingWithEumOrIum = new string[]
        {
            "museum", "Jubileum", "kjemikalium"
        };

    }
}


The following DbConfiguration set up for the DbContext sets up the pluralization service to use for Entity Framework.

ApplicationDbConfiguration.cs



using BulkOperationsEntityFramework.Lib.Services;
using System;
using System.Data.Entity;
using System.Data.Entity.SqlServer;

namespace BulkOperationsEntityFramework
{
    public class ApplicationDbConfiguration : DbConfiguration
    {

        public ApplicationDbConfiguration()
        {
        
            SetPluralizationService(new NorwegianPluralizationService());  //Set up the NorwegianPluralizationService as the Pluralizer         
            
            //more code etc..
        }

    }

}


I have also created a Schema attribute to control schema names of tables convention previously in the solution, so the Norwegian pluralizer is also being used there.

SchemaConvention.cs



using BulkOperationsEntityFramework.Attributes;
using BulkOperationsEntityFramework.Lib.Services;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Reflection;

namespace BulkOperationsEntityFramework.Conventions
{
    public class SchemaConvention : Convention
    {
        public SchemaConvention()
        {
            var pluralizer = new NorwegianPluralizationService();

            Types().Configure(c =>
            {
                var schemaAttr = c.ClrType.GetCustomAttribute<SchemaAttribute>(false);
                var tableName = pluralizer.Pluralize(c.ClrType.Name);

                if (schemaAttr != null && !string.IsNullOrEmpty(schemaAttr.SchemaName))
                {
                    c.ToTable(tableName, schemaAttr.SchemaName ?? "dbo");
                }
                else
                {
                    c.ToTable(tableName);
                }
            });
        }
    }
}


The DbContext will use the IPluralizationService. Consider first this example DbContext :

ApplicationDbContext



using BulkOperationsEntityFramework.Conventions;
using BulkOperationsEntityFramework.Models;
using BulkOperationsEntityFramework.Test;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;

namespace BulkOperationsEntityFramework
{

    [DbConfigurationType(typeof(ApplicationDbConfiguration))]
    public class ApplicationDbContext : DbContext
    {       

        public ApplicationDbContext(DbConnection connection) : base(connection, false)
        {
        }

        public ApplicationDbContext() : base("name=App")
        {
        }

        public virtual DbSet Bruker { get; set; }

        public DbSet ArkivertBruker { get; set; }

        public DbSet ArkivertGjest { get; set; }

        public DbSet Sesjon { get; set; }

        public DbSet Jubileum { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {            
            modelBuilder.Conventions.Add(new SchemaConvention());
            //more code etc
          
        }

    }

}


The following test cases checks how good the pluralizer works.

ApplicationDbContextTests.cs



using Bogus;
using BulkOperationsEntityFramework.Lib.Services;
using BulkOperationsEntityFramework.Models;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace BulkOperationsEntityFramework.Test
{

    [TestFixture]
    public class ApplicationDbContextTests
    {

       
        [Test]
        [TestCaseSource(nameof(NorwegianPluralizationCases))]
        public void CanUsePluralizationService(string word, string expected)
        {
            var norwegianPluralizationService = new NorwegianPluralizationService();
            string pluralizedWord = norwegianPluralizationService.Pluralize(word);
            pluralizedWord.Should().Be(expected, "Norwegian Pluralization service should return the correct plural form of the word.");
        }

        [Test, TestCaseSource(nameof(NorwegianSingularizationCases))]
        public void NorwegianPluralizationService_CanSingularize(string plural, string expectedSingular)
        {
            var norwegianPluralizationService = new NorwegianPluralizationService();

            var actual = norwegianPluralizationService.Singularize(plural);
            Assert.That(actual, Is.EqualTo(expectedSingular), $"Expected singular of '{plural}' to be '{expectedSingular}', but got '{actual}'.");
        }

        public static IEnumerable<TestCaseData> NorwegianPluralizationCases
        {
            get
            {
                yield return new TestCaseData("Bil", "Biler");
                yield return new TestCaseData("Bok", "Bøker");
                yield return new TestCaseData("Hund", "Hunder");
                yield return new TestCaseData("Stol", "Stoler");
                yield return new TestCaseData("Jente", "Jenter");
                yield return new TestCaseData("Gutt", "Gutter");
                yield return new TestCaseData("Lærer", "Lærere");
                yield return new TestCaseData("Barn", "Barn");
                yield return new TestCaseData("Fjell", "Fjell");
                yield return new TestCaseData("Sko", "Sko");
                yield return new TestCaseData("Ting", "Ting");
                yield return new TestCaseData("Mann", "Menn");
                yield return new TestCaseData("Kvinne", "Kvinner");
                yield return new TestCaseData("Bror", "Brødre");
                yield return new TestCaseData("Far", "Fedre");
                yield return new TestCaseData("Mor", "Mødre");
                yield return new TestCaseData("Datter", "Døtre");
                yield return new TestCaseData("Søster", "Søstre");
                yield return new TestCaseData("Øye", "Øyne");
                yield return new TestCaseData("Hand", "Hender");
                yield return new TestCaseData("Fot", "Føtter");
                yield return new TestCaseData("Tå", "Tær");
                yield return new TestCaseData("Tann", "Tenner");
                yield return new TestCaseData("Natt", "Netter");
                yield return new TestCaseData("Tre", "Trær");
                yield return new TestCaseData("Kne", "Knær");
                yield return new TestCaseData("Bonde", "Bønder");

                // _nonEndingWordsInPlural
                yield return new TestCaseData("Mus", "Mus");
                yield return new TestCaseData("Ski", "Ski");
                yield return new TestCaseData("Feil", "Feil");

                // _wordsChangingVowelsInPluralMale
                yield return new TestCaseData("Bot", "Bøter");
                yield return new TestCaseData("Rot", "Røter");

                // _wordsChangingVowelToÆ
                yield return new TestCaseData("Håndkle", "Håndklær");
                yield return new TestCaseData("Kne", "Knær");

                // _wordsForUnits (should not pluralize)
                yield return new TestCaseData("Meter", "Meter");
                yield return new TestCaseData("Gram", "Gram");
                yield return new TestCaseData("Dollar", "Dollar");

                // _wordChangingVowelsInPluralFemale
                yield return new TestCaseData("And", "Ender");
                yield return new TestCaseData("Hånd", "Hender");
                yield return new TestCaseData("Stang", "Stenger");
                yield return new TestCaseData("Strand", "Strender");
                yield return new TestCaseData("Tang", "Tenger");
                yield return new TestCaseData("Tann", "Tenner");

                // _wordsForRelatives (some already covered, but add missing)
                yield return new TestCaseData("Fetter", "Fettere");
                yield return new TestCaseData("Onkel", "Onkler");
                yield return new TestCaseData("Svigerbror", "Svigerbrødre");
                yield return new TestCaseData("Svigerfar", "Svigerfedre");
                yield return new TestCaseData("Svigermor", "Svigermødre");
                yield return new TestCaseData("Svigersøster", "Svigersøstre");

                // _wordsNoPluralizationForNeutralGenderOneSyllable
                yield return new TestCaseData("Hus", "Hus");
                yield return new TestCaseData("Blad", "Blad");

                // _wordsNeutralGenderEndingWithEumOrIum
                yield return new TestCaseData("Museum", "Museer");
                yield return new TestCaseData("Jubileum", "Jubileer");
                yield return new TestCaseData("Kjemikalium", "Kjemikalier");
            }
        }

        public static IEnumerable<TestCaseData> NorwegianSingularizationCases
        {
            get
            {
                yield return new TestCaseData("Biler", "Bil");
                yield return new TestCaseData("Bøker", "Bok");
                yield return new TestCaseData("Hunder", "Hund");
                yield return new TestCaseData("Stoler", "Stol");
                yield return new TestCaseData("Jenter", "Jente");
                yield return new TestCaseData("Gutter", "Gutt");
                yield return new TestCaseData("Lærere", "Lærer");
                yield return new TestCaseData("Barn", "Barn");
                yield return new TestCaseData("Fjell", "Fjell");
                yield return new TestCaseData("Sko", "Sko");
                yield return new TestCaseData("Ting", "Ting");
                yield return new TestCaseData("Menn", "Mann");
                yield return new TestCaseData("Kvinner", "Kvinne");
                yield return new TestCaseData("Brødre", "Bror");
                yield return new TestCaseData("Fedre", "Far");
                yield return new TestCaseData("Mødre", "Mor");
                yield return new TestCaseData("Døtre", "Datter");
                yield return new TestCaseData("Søstre", "Søster");
                yield return new TestCaseData("Øyne", "Øye");
                yield return new TestCaseData("Hender", "Hand");
                yield return new TestCaseData("Føtter", "Fot");
                yield return new TestCaseData("Tær", "Tå");
                yield return new TestCaseData("Tenner", "Tann");
                yield return new TestCaseData("Netter", "Natt");
                yield return new TestCaseData("Trær", "Tre");
                yield return new TestCaseData("Knær", "Kne");
                yield return new TestCaseData("Bønder", "Bonde");
            }
        }

    }
}


The pluralization is used when creating migrations and map entites to table names. The following migration shows how the Norwegian pluralization has pluralized the table names.

202506292302222_Init.cs



namespace BulkOperationsEntityFramework.Migrations
{
    using System.Data.Entity.Migrations;

    public partial class Init : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "Archive.Arkivertbrukere",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Email = c.String(maxLength: 255),
                    FirstName = c.String(maxLength: 255),
                    LastName = c.String(maxLength: 255),
                    PhoneNumber = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "Arkiv.Gjester",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Email = c.String(maxLength: 255),
                    FirstName = c.String(maxLength: 255),
                    LastName = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Brukere",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Email = c.String(maxLength: 255),
                    FirstName = c.String(maxLength: 255),
                    LastName = c.String(maxLength: 255),
                    PhoneNumber = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Jubileer",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Date = c.DateTime(),
                    Description = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Sesjoner",
                c => new
                {
                    Key = c.Guid(nullable: false),
                    CreatedAt = c.DateTime(nullable: false),
                    ExpiresAt = c.DateTime(),
                    IpAddress = c.String(maxLength: 255),
                    UserAgent = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Key);

        }

        public override void Down()
        {
            DropTable("dbo.Sesjoner");
            DropTable("dbo.Jubileer");
            DropTable("dbo.Brukere");
            DropTable("Arkiv.Gjester");
            DropTable("Archive.Arkivertbrukere");
        }
    }
}


The table names above are pluralized into their Norwegian pluralization. In Norwegian we call these words "ubestemt flertall", indefinite plural. It is possible to add detection if the passed in noun is an English noun. But beware that there are several Norwegian nouns overlapping these English nouns. So in fairness, a Norwegian wordlist should also be checked. However, the following update only shows how an English dictionary can be checked. The English noun list is put into a static variable into memory for quick access. The world list I have tested with got about 40,0000+ noun as mentioned previously in this article.

NorwegianPluraizationService.cs

(updated with a check against a English noun list and using the default EnglishPluralizationService that Entity Framework contains to add support for English noun pluralization (and singularization)


using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Pluralization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace BulkOperationsEntityFramework.Lib.Services
{

    /// <summary>
    /// Sources for the pluralization rules for Norwegian language:
    /// https://toppnorsk.com/2018/11/18/flertall-hovedregler/
    /// </summary>
    public class NorwegianPluralizationService : IPluralizationService
    {
        private EnglishPluralizationService _englishPluralizationService = new EnglishPluralizationService();
        
        public static List<string> PluralizedWords = new List<string>();

        private static List<string> EnglishNounsWordList = null;

        public string Pluralize(string word)
        {
            if (PluralizedWords.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                return word; // Return the already pluralized word
            }

            //#if DEBUG
            //            Debugger.Break();
            //            Debugger.Launch(); // Uncomment this line to break into the debugger when this method is called, for example when database migrations are made with EF Code First
            //#endif

            word = NormalizeWord(word);

            if (EnglishNounsWordList == null)
            {
                EnglishNounsWordList = new List<string>();
                var assembly = typeof(NorwegianPluralizationService).Assembly;
                // Adjust the resource name to match your project's default namespace and folder structure
                var resourceName = "BulkOperationsEntityFramework.Lib.Services.EnglishNouns.txt";
                using (var stream = assembly.GetManifestResourceStream(resourceName))
                using (var reader = new StreamReader(stream))
                {
                    EnglishNounsWordList.AddRange(reader.ReadToEnd().Split('\n').Select(l => l.Trim()));
                }
            }

            string pluralizedWord;

            if (EnglishNounsWordList.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = _englishPluralizationService.Pluralize(word);
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_specialCases.ContainsKey(word))
            {
                pluralizedWord = _specialCases[word];
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }
            
            //more code


To sum up, we can customize Entity Framework pluralization to support other languages. But please note that there is a lot of work to make a good pluralization service. Also, you probably want to support English nouns too. The last version above did not use a Norwegian wordlist. A better pluralization service could directly try to lookup pluralized and singularized nouns of both languages instead of all the general and specific rules presented in these code samples. The English pluralization service do not do this, instead it relies on rule sets, such as the first version of
the Norwegian pluralizer shown earlier in this article is using.

Saturday, 28 June 2025

Setting up connection resiliency for Entity Framework

In Entity Framework, it is possible to add more connection resiliency. This can be done for example if you are working against a more unstable database connection, maybe because the database is served in the Cloud and/or is not scaled properly to its load. Whatever reason, it is possible to add more connection resiliency. The connection resiliency can be used in other scenarios that just SQL servers hosted in Azure, such as On-Premise databases. It should add more resiliency and stability for scenarios where connections to database needs to be improved. This could also be due to mobile clients being moved in and out of areas with good network access, such as within buildings and factories on different levels trying to access a wireless connection that connects to a database. The code in this article is available in my Github repo here:

https://github.com/toreaurstadboss/BulkOperationsEntityFramework

first off, the ExecutionStrategy is set up. A DbConfiguration is added to set this up.

ApplicationDbModelConfiguration.cs



using System;
using System.Data.Entity;
using System.Data.Entity.SqlServer;

namespace BulkOperationsEntityFramework
{
    public class ApplicationDbConfiguration : DbConfiguration
    {

        public ApplicationDbConfiguration()
        {
            SetExecutionStrategy(SqlProviderServices.ProviderInvariantName, () =>
             new CustomSqlAzureExecutionStrategy(maxRetryCount: 10, maxDelay: TimeSpan.FromSeconds(5))); //note : max total delay of retries is 30 seconds per default in SQL Server
        }

    }

}


In EF Core 8 (using Entity Framework with .NET 8, you can set it up like this :

Program.cs




    public class ApplicationDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                "DefaultConnection",
                sqlOptions =>
                {
                    sqlOptions.EnableRetryOnFailure(
                        maxRetryCount: 10,
                        maxRetryDelay: TimeSpan.FromSeconds(5),
                        errorNumbersToAdd: null
                    );
                });
        }



Setting up the interval strategy

The CustomSqlAzureExecutionStrategy inherits from the SqlAzureExecutionStrategy. delay = min ( maxDelay , random × ( 2 retryCount 1 ) × baseDelay ) The default base delay in Entity Framework is to wait one second, so the next wait time will be about 2 seconds, then the next delays will quickly grow up the max wait time of five seconds. The custom sql azure execution strategy implementation inherits from SqlAzureExecutionStrategy.

CustomSqlAzureExecutionStrategy.cs



using System;
using System.Data.Entity.SqlServer;

namespace BulkOperationsEntityFramework
{

    public class CustomSqlAzureExecutionStrategy : SqlAzureExecutionStrategy
    {

        [ThreadStatic]
        private static int _currentRetryCount = 0;

        public CustomSqlAzureExecutionStrategy(int maxRetryCount, TimeSpan maxDelay)
        : base(maxRetryCount, maxDelay) { }

        protected override bool ShouldRetryOn(Exception ex)
        {
            _currentRetryCount++;
            Console.WriteLine($"{nameof(CustomSqlAzureExecutionStrategy)}: Retry-count within thread: {_currentRetryCount}");
            Log.Information("{Class}: Retry-count within thread: {RetryCount} {ExceptionType}", nameof(CustomSqlAzureExecutionStrategy), _currentRetryCount, ex.GetType().Name);

            return base.ShouldRetryOn(ex) || ex is SimulatedTransientSqlException;
        }

    }

}


Of course, just logging out to console probably is not a very elegant solution, and it could instead be logged out to for example SeriLog, which is used in the line with the Log.Information call. The SimulatedTransientSqlException looks like this:

SimulatedTransientSqlException.cs



 public class SimulatedTransientSqlException : Exception
 {
     public SimulatedTransientSqlException()
     : base("Simulated transient SQL exception.") { }
 }
 

The following db interceptor is added to simulate transient failures happening, at 10% chance of it happening.

TransientFailureInterceptor.cs



using System;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;

namespace BulkOperationsEntityFramework
{

    public class TransientFailureInterceptor : DbCommandInterceptor
    {
        private static readonly Random _random = new Random();

        public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            SimulateTransientFailure(interceptionContext);
            base.ReaderExecuting(command, interceptionContext);
        }

        public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            SimulateTransientFailure(interceptionContext);
            base.ScalarExecuting(command, interceptionContext);
        }

        public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            SimulateTransientFailure(interceptionContext);
            base.NonQueryExecuting(command, interceptionContext);
        }

        private void SimulateTransientFailure<TResult>(DbCommandInterceptionContext<TResult> context)
        {
            // Simulate a transient failure 10% of the time
            double r = _random.NextDouble();
            if (r < 0.1)
            {
                var ex = new SimulatedTransientSqlException();
                string info = "Throwing a transient SqlException. ";
                Trace.WriteLine($"{info} {ex.ToString()}");
                context.Exception = ex;
            }
        }
    }

    public class SimulatedTransientSqlException : Exception
    {
        public SimulatedTransientSqlException()
        : base("Simulated transient SQL exception.") { }
    }

}


Next up connecting the dots in the DbContext, setting up the db configuration

ApplicationDbContext.cs



[DbConfigurationType(typeof(ApplicationDbConfiguration))]
public class ApplicationDbContext : DbContext
{

    static ApplicationDbContext()
    {
        if (!AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName.StartsWith("Effort")))
        {
            DbInterception.Add(new TransientFailureInterceptor()); //add an interceptor that simulates a transient connection error occuring (30% chance of it happening)
            DbInterception.Add(new SerilogCommandInterceptor()); //do not add logging if EF6 Effor is used (for unit testing)
        }
    }
    
    //more code..


Also note that you usually do not want to add the TransientFailureInterceptor, it is just added for testing. You could for example add a boolean property on your DbContext to set if you are testing out connection resiliency and add the TransientFailureInterceptor when you can to test it, or provide a public method to add the TransientfailureInterceptor, and remove it afterwards if desired. Within a Test-project, you should be able to test out connection resiliency.

Tuesday, 24 June 2025

Custom code conventions in Entity Framework

This article will once more look at code conventions in Entity Framework. A custom code convention will be added where if the property (column of an entity) is called "Key" and is of type Guid, it is set as the key of the entity (surrounding type the property resides in). The code in this article is available in my Github repo here:

https://github.com/toreaurstadboss/BulkOperationsEntityFramework

The custom code convention for setting all properties called Key of property type Guid as the key of an entity (table) looks like the following:

GuidKeyConvention.cs



using System;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Reflection;

namespace BulkOperationsEntityFramework.Conventions
{

    public class GuidKeyConvention : Convention
    {
        public GuidKeyConvention()
        {
            Types().Configure(t =>
            {
                var keyProperty = t.ClrType
                    .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                    .FirstOrDefault(p => p.PropertyType == typeof(Guid)
                    && string.Equals(p.Name, "Key", StringComparison.OrdinalIgnoreCase));

                if (keyProperty != null)
                {
                    t.HasKey(keyProperty);
                }
            });            
        }
    }

}



The custom code convention can then be added in the OnModelCreating method shown below :

ApplicationDbContext.cs



 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     modelBuilder.Entity<User>().HasKey(u => u.Id);
     modelBuilder.Entity<User>().Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

     modelBuilder.Properties<string>().Configure(p => p.HasMaxLength(255)); // Set max length for all string properties

     modelBuilder.Conventions.Add(new GuidKeyConvention());
     
     //more code

 }


As shown above, a custom convention inherits from the System.Data.Entity.ModelConfiguration.Conventions.Convention class. The behavior of the custom code convention is set up in the constructor of the custom code convention. It is not overriding any methods, instead it is making use over inherited public methods Types() or Properties() or Properties(). An example of an entity that then will use this custom code convention is shown with the following entity (POCO) :

Session.cs



using System;

namespace BulkOperationsEntityFramework.Models
{
    public class Session
    {

        public Guid Key { get; set; } // Primary key by convention

        public DateTime CreatedAt { get; set; }

        public DateTime? ExpiresAt { get; set; }

        public string IpAddress { get; set; }

        public string UserAgent { get; set; }
    }
}


Adding this custom code convention, the following database migration is then added and shows that the property (field/column) called Key of type Guid.


namespace BulkOperationsEntityFramework.Migrations
{
    using System.Data.Entity.Migrations;

    public partial class Sessions : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Sessions",
                c => new
                {
                    Key = c.Guid(nullable: false),
                    CreatedAt = c.DateTime(nullable: false),
                    ExpiresAt = c.DateTime(),
                    IpAddress = c.String(maxLength: 255),
                    UserAgent = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Key);

        }

        public override void Down()
        {
            DropTable("dbo.Sessions");
        }
    }
}


As shown in the database migration, the primary key is set to the field Key, which means that the field (property/column) Key is set as the primary key. It is of course easier to just attribute the property Key with the Key attribute or set up the primary key in the OnModelConfiguring (Fluent API). Custom code conventions are best in use when you make a custom code convention that saves a lot of setup where you have a large data model and want to standardize code conventions, this sample just is a demonstration of how such a custom code convention can be created by inheriting the Convention class in the namespace System.Data.Entity.ModelConfiguration.Conventions. In the previous article, a custom code convention encapsulated in a custom marker attribute and wiring up the logic via helper extension methods. Instead of inheriting from the Convention class, the wiring up of Schema attribute shown in previous article could be more standardized in this way. Such a SchemaConvention could be set up like this instead, inheriting Convention class.

SchemaConvention.cs




using BulkOperationsEntityFramework.Attributes;
using System.Data.Entity.Infrastructure.Pluralization;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Reflection;

namespace BulkOperationsEntityFramework.Conventions
{
    public class SchemaConvention : Convention
    {
        public SchemaConvention()
        {
            var pluralizer = new EnglishPluralizationService();

            Types().Configure(c =>
            {
                var schemaAttr = c.ClrType.GetCustomAttribute<SchemaAttribute>(false);
                var tableName = pluralizer.Pluralize(c.ClrType.Name);

                if (schemaAttr != null && !string.IsNullOrEmpty(schemaAttr.SchemaName))
                {
                    c.ToTable(tableName, schemaAttr.SchemaName ?? "dbo");
                }
                else
                {
                    c.ToTable(tableName);
                }
            });
        }
    }
}  



Saturday, 21 June 2025

Creating a convention based Schema attribute in Entity Framework for .NET Framework

This article will present a way to create a Schema attribute for Entity Framework in .NET Framework. Some people still use .NET Framework, hopefully the latest supported version .NET Framework 4.8, due to compability reasons and legacy code and having a Schema attribute is not supported in Entity Framework for .NET Framework, still in Entity Framework 6.5.0 which is the newest version. This is similar to the way we can set Schema via attribute on an entity in Entity Framework .NET Core 8. The code presented in the article is in the following Github repo : https://github.com/toreaurstadboss/BulkOperationsEntityFramework The Schema attribute is a simple marker attribute where the schema name to be set to the entity (class) upon it is applied on.

SchemAttribute.cs



using System;

namespace BulkOperationsEntityFramework.Attributes
{

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class SchemaAttribute : Attribute
    {
        private readonly string _schemaName;

        public SchemaAttribute(string schemaName)
        {
            _schemaName = schemaName ?? "dbo"; //fallback to default schema 'dbo' if null is passed in here             
        }

        public string SchemaName => _schemaName;

    }

}


The attribute will be used as an attribute-based code convention for Entity Framework. First off, the code conventions are set up using a helper extension. Note that it is not necessary to cast this to DbContext here, it just makes it more readable that we are passing in DbContext here.

ApplicationDbContext.cs



protected override void OnModelCreating(DbModelBuilder modelBuilder) {

// more code here if needed

  modelBuilder.ApplyCustomCodeConventions((DbContext)this); // Apply custom code conventions based on DbSet types. Pass in db context.

// more code here if needed

}

The helper method ApplyCustomCodeConventions will loop through all the DbSet generic properties of the passed in DbContext. DbModelBuilder is the instance that the helper extension method provides more functionality on.

ModelBuilderExtensions.cs



using BulkOperationsEntityFramework.Attributes;
using System.Data.Entity;
using System.Linq;
using System.Reflection;

namespace BulkOperationsEntityFramework
{

    public static class ModelBuilderExtensions
    {

        /// <summary>
        /// Applies custom code conventions to the specified <see cref="DbModelBuilder"/> instance based on the <see
        /// cref="DbSet{TEntity}"/> types defined in the provided <see cref="DbContext"/>.
        /// </summary>
        /// <remarks>This method inspects the <see cref="DbSet{TEntity}"/> properties of the provided <see
        /// cref="DbContext"/> and applies schema conventions to each entity type. It is typically used to enforce
        /// custom schema rules or configurations during model creation.</remarks>
        /// <param name="modelBuilder">The <see cref="DbModelBuilder"/> instance to which the conventions will be applied.</param>
        /// <param name="context">The <see cref="DbContext"/> containing the <see cref="DbSet{TEntity}"/> types to analyze.</param>
        public static void ApplyCustomCodeConventions(this DbModelBuilder modelBuilder, DbContext context)
        {
            var dbSetTypes = context
                .GetType()
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>))
                .Select(p => p.PropertyType.GetGenericArguments()[0]);

            foreach (var type in dbSetTypes)
            {
                ApplySchemaAttributeConvention(modelBuilder, type);
            }

        }

        /// <summary>
        /// Adds a convention to apply the Schema attribute to set the schema name of entities in the DbContext.
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <param name="type"></param>
        private static void ApplySchemaAttributeConvention(DbModelBuilder modelBuilder, System.Type type)
        {
            var schema = type.GetCustomAttribute<SchemaAttribute>(false)?.SchemaName;
            if (schema != null)
            {
                var entityMethod = typeof(DbModelBuilder).GetMethod("Entity").MakeGenericMethod(type);
                var entityTypeConfiguration = entityMethod.Invoke(modelBuilder, null);
                var toTableMethod = entityTypeConfiguration.GetType().GetMethod("ToTable", new[] { typeof(string), typeof(string) });
                toTableMethod.Invoke(entityTypeConfiguration, new object[] { type.Name, schema });
            }
        }
    }

}


The schema attribute could also set one and one entity in the OnModelConfiguration method like for example the following:

ApplicationDbContext.cs



    modelBuilder.Types().Where(p => p.GetCustomAttributes(false).OfType<SchemaAttribute>().Any())
        .Configure(t => t.ToTable(t.ClrType.Name, t.ClrType.GetCustomAttribute<SchemaAttribute>().SchemaName ?? "dbo")); //add support for setting Schema via Schema attribute using custom code convention


But the code above is much better but in a reusable helper method as shown higher up in this article so you can easier just paste in the helper method and do a much cleaner call in your DbContext's OnConfiguring method. Note that the Schema attribute is available if you use .NET Core Entity Framework Core 8, that is using .NET 8. Example usage:


ArchivedUser.cs



using BulkOperationsEntityFramework.Attributes;

namespace BulkOperationsEntityFramework.Models
{

    [Schema("Archive")]
    public class ArchivedUser
    {

        public int Id { get; set; }

        public string Email { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string PhoneNumber { get; set; }

    }
    
}


When this is set up, we can easily set up the schema of an entity by just attributing the table using this Schema attribute.

Tuesday, 10 June 2025

Database logging Entity Framework queries to SeriLog

Logging database queries from Entity Framework can be done using Sql Server Profiler or using Profiling tools inside VS 2022. A problem with this approach is that Sql server profiler tends to log very much other events than just the queries to the database and if you use the Profiling tool, they will truncate large queries. Note that database logging the traffic will quickly grow into large logs, of several gigabyte. Database logging can also reveal sensitive data, another challenge. But having a tailored database log can for developers be a helpful tool to both profile and check queries, especially when using an ORM like Entity Framework that can hide the actual queries being sent to the database since that part is abstracted away via IQueryable. Once you have the queries you want to inspect, you can profile them inside SQL Server Management Studio and check if the use of indexes in database are optimal or if more indexes should be added. In case you as a developer want to inspect queries that your application does, maybe activated via an app setting in config, you might be better off with a tailored way of logging the database queries that the application performs. This article will look at Entity Framework 6 for .NET Framework and Db interceptor that logs compacted, one-lined, interpolated queries that Entity Framework runs. Note that also later version of Entity Framework support db interceptors, such as EF Core 8 (for .NET 8). Let's first look at the repo for the code of this article :

https://github.com/toreaurstadboss/BulkOperationsEntityFramework

SerilogCommandInterceptor

The db interceptor looks like this, it will log to a file that uses SeriLog as a logging framework and to format the logging file(s).


using Serilog;
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;

namespace BulkOperationsEntityFramework.Test
{

    /// <summary>
    /// Intercepts Entity Framework database commands and logs them using Serilog.
    /// 
    /// This interceptor captures and logs SQL command text for NonQuery, Reader, and Scalar operations.
    /// Logging is configured via Serilog and can be customized using <c>App.config</c> or <c>Web.config</c> settings.
    /// 
    /// <para>
    /// <b>Configuration via AppSettings:</b>
    /// </para>
    /// <list type="table">
    ///   <item>
    ///     <term>serilog:write-to:File.path</term>
    ///     <description>Path to the log file. Default: <c>databaselogs\log.txt</c></description>
    ///   </item>
    ///   <item>
    ///     <term>serilog:write-to:File.rollingInterval</term>
    ///     <description>Rolling interval for log files (e.g., <c>Day</c>, <c>Hour</c>). Default: <c>Day</c></description>
    ///   </item>
    ///   <item>
    ///     <term>serilog:minimum-level</term>
    ///     <description>Minimum log level (e.g., <c>Information</c>, <c>Warning</c>). Default: <c>Information</c></description>
    ///   </item>
    ///   <item>
    ///     <term>serilog:write-to:File.retainedFileCountLimit</term>
    ///     <description>Number of log files to retain. Default: <c>21</c></description>
    ///   </item>
    /// </list>
    /// 
    /// <para>
    /// <b>Example App.config override:</b>
    /// </para>
    /// <code language="xml">
    /// <appSettings>
    ///   <add key="serilog:write-to:File.path" value="C:\Logs\mydb.log" />
    ///   <add key="serilog:write-to:File.rollingInterval" value="Hour" />
    ///   <add key="serilog:minimum-level" value="Warning" />
    ///   <add key="serilog:write-to:File.retainedFileCountLimit" value="10" />
    /// </appSettings>
    /// </code>
    /// </summary>
    public class SerilogCommandInterceptor : IDbCommandInterceptor
    {

        private static bool _isInitialized = false;

        public SerilogCommandInterceptor()
        {
            if (_isInitialized)
            {
                return;
            }

            var logPath = ConfigurationManager.AppSettings["serilog:write-to:File.path"] ?? "databaselogs\\log.txt";
            var logIntervalRaw = ConfigurationManager.AppSettings["serilog:write-to:File.rollingInterval"];
            var logInterval = Enum.TryParse(logIntervalRaw, true, out RollingInterval interval) ? interval : RollingInterval.Day;
            var minLevelRaw = ConfigurationManager.AppSettings["serilog:minimum-level"];
            var logLevel = Enum.TryParse(minLevelRaw, true, out Serilog.Events.LogEventLevel level) ? level : Serilog.Events.LogEventLevel.Information;
            var retainedCountRaw = ConfigurationManager.AppSettings["serilog:write-to:File.retainedFileCountLimit"];
            var retainedCount = int.TryParse(retainedCountRaw, out int count) ? count : 21;

            //Set up Serilog logging for the database logging interceptor - set up the minimum level to Information and
            //write to a file with rolling intervals. Set up the file size limit to 500 MB per file
            //in case the log file grows too large on a given day, it will roll over to a new file with the with a running number suffixed to it 
            //the logs will be stored in the "databaselogs" subfolder, or configured path in config file
            //logs will be kept or a maximum number of 21 days or specified number of days
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Is(logLevel)
                .WriteTo.File(
                    logPath, 
                    rollingInterval: logInterval, 
                    rollOnFileSizeLimit: true,
                    fileSizeLimitBytes: 500 * 1000 * 1000,
                    outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] [SQL] {Message:lj}{NewLine}",
                    retainedFileCountLimit: retainedCount
                )
                .CreateLogger();

            _isInitialized = true;
        }

        public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { }

        public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) =>
            Log.Information("{Tag} {Sql}", GetSqlTag(command.CommandText), CompactAndInterpolateSql(command));

        public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { }

        public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) =>
            Log.Information("{Tag} {Sql}", GetSqlTag(command.CommandText), CompactAndInterpolateSql(command));

        public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { }

        public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) =>
            Log.Information("{Tag} {Sql}", GetSqlTag(command.CommandText), CompactAndInterpolateSql(command));

        private string CompactAndInterpolateSql(DbCommand dbCommand)
        {
            string sql = InterpolateSql(dbCommand);
            return CompactSql(sql);
        }

        private string CompactSql(string sql) =>
            sql.Replace(Environment.NewLine, " ").Replace("\n", "").Replace("\r", " ").Trim();

        private string GetSqlTag(string sql)
        {
            if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)) return "[SELECT]";
            if (sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase)) return "[INSERT]";
            if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase)) return "[UPDATE]";
            if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase)) return "[DELETE]";
            return "[SQL]";
        }

        private string InterpolateSql(DbCommand dbCommand)
        {
            string sql = dbCommand.CommandText;
            foreach (DbParameter parameter in dbCommand.Parameters)
            {
                string value = FormatParameterValue(parameter.Value);
                sql = sql.Replace(parameter.ParameterName, value);
            }
            return sql;
        }

        private string FormatParameterValue(object value)
        { 
            if (value == null || value == DBNull.Value)
            {
                return "NULL";
            }
            if (value is string || value is DateTime || value is Guid)
            {
                return $"'{value}'"; // Wrap strings, DateTime, and Guid in single quotes
            }
            if (value is bool b)
            {
                return b ? "1" : "0";
            }

            return value.ToString();
        }        

    }
}


Adding a db interceptor should be preferably done once, for example in a static constructor, to ensure the db interceptor is available right away. Example of a simple db context that adds the interceptor shown above.


using BulkOperationsEntityFramework.Models;
using BulkOperationsEntityFramework.Test;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;

namespace BulkOperationsEntityFramework
{

    public class ApplicationDbContext : DbContext
    {

        static ApplicationDbContext()
        {
            DbInterception.Add(new SerilogCommandInterceptor());
        }

        public ApplicationDbContext() : base("name=App")
        {
        }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasKey(u => u.Id);
            modelBuilder.Entity<User>().Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }

    }

}




Since Serilog is used, a great flexibility to the logging is possible to do. For example, logging database queries can be done with rolling intervals, such as logging per hour or per day or even per minute for example. Also, a max size for logs can be set. The logging within the same time interval for the logging is split into multiple log files, when log files reaches size limit. Example app config settings are here:


<appSettings>
	<add key="serilog:write-to:File.path" value="logs\dblogv3.txt" />
	<add key="serilog:minimum-level" value="Information" />
	<add key="serilog:write-to:File.rollingInterval" value="Day" />
	<add key="serilog:file:retentionDays" value="21" />
</appSettings>


If you use a tool like TailBlazer, opening very large logs of many gigabytes is fairly fast and you can define highlighting rules to discern between different types of queries.
A screenshot of the highlighting rules of Tail Blazor for the previous screen shot shown is shown below:



Tail Blazer tool

The Tail Blazer tool can be downloaded from GitHub here:

https://github.com/RolandPheasant/TailBlazer

Sunday, 1 June 2025

Using SqlBulkCopy with EntityFramework

This article tests out variying methods of ways of doing bulk inserts in EntityFramework. The code shown in this article uses .NET Framework 4.8
and Entity Framework 6.5.0. Support is better in Entity Framework Core, so the code shown here for SqlBulkCopy must be adjusted a little bit to also work with EntityFramework Core. A Github repo has been added here:

https://github.com/toreaurstadboss/BulkOperationsEntityFramework

A benchmark has been run to test out the different approaches. Not suprisingly, SqlBulkCopy is the fastest and most economic way of performing the bulk inserts (uses the least memory). The code for handling SqlBulkCopy is provided via extension methods listed below:

BulkInsertExtensions.cs




using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace BulkOperationsEntityFramework.Lib.Extensions
{

    /// <summary>
    /// Provides extension methods for performing bulk insert operations using SqlBulkCopy.
    /// </summary>
    public static class BulkInsertExtensions
    {
        /// <summary>
        /// Performs a bulk insert of the specified entities into the database.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <param name="context">The DbContext instance.</param>
        /// <param name="entities">The collection of entities to insert.</param>
        /// <param name="tableName">
        /// Optional: The name of the destination table. If null, the entity type name is used.
        /// </param>
        /// <param name="columnMappings">
        /// Optional: Custom column mappings (propertyName → columnName).
        /// 
        /// <example>
        /// Example 1: Rename columns
        /// <code>
        /// var mappings = new Dictionary<string, string>
        /// {
        ///     { "Name", "ProductName" },
        ///     { "Price", "UnitPrice" }
        /// };
        /// await context.BulkInsertAsync(products, "Products", mappings);
        /// </code>
        /// </example>
        /// 
        /// <example>
        /// Example 2: Ignore properties using attributes
        /// <code>
        /// public class Customer
        /// {
        ///     public int Id { get; set; }
        ///     public string FullName { get; set; }
        /// 
        ///     [BulkIgnore]
        ///     public string TempNotes { get; set; }
        /// 
        ///     [NotMapped]
        ///     public string CalculatedField { get; set; }
        /// }
        /// 
        /// var mappings = new Dictionary<string, string>
        /// {
        ///     { "FullName", "CustomerName" }
        /// };
        /// await context.BulkInsertAsync(customers, "Customers", mappings);
        /// </code>
        /// </example>
        /// 
        /// <example>
        /// Example 3: Snake_case mapping
        /// <code>
        /// var mappings = new Dictionary<string, string>
        /// {
        ///     { "FirstName", "first_name" },
        ///     { "LastName", "last_name" },
        ///     { "Email", "email_address" }
        /// };
        /// await context.BulkInsertAsync(users, "user_accounts", mappings);
        /// </code>
        /// </example>
        /// </param>
        /// <param name="bulkCopyTimout">The bulk copy timeout in seconds. Default is set to 30.</param>
        public static void BulkInsert<T>(this DbContext context, IEnumerable<T> entities, string tableName = null,
            Dictionary<string, string> columnMappings = null, int bulkCopyTimeout = 30)
            where T : class
        {
            var dataTable = entities.ToBulkDataTable(columnMappings, out var finalMappings);
            BulkCopy(context, dataTable, tableName ?? typeof(T).Name, finalMappings, bulkCopyTimeout);
        }

        /// <summary>
        /// Asynchronously performs a bulk insert of the specified entities into the database.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <param name="context">The DbContext instance.</param>
        /// <param name="entities">The collection of entities to insert.</param>
        /// <param name="tableName">
        /// Optional: The name of the destination table. If null, the entity type name is used.
        /// </param>
        /// <param name="columnMappings">
        /// Optional: Custom column mappings (propertyName → columnName).
        /// 
        /// <example>
        /// Example 1: Rename columns
        /// <code>
        /// var mappings = new Dictionary<string, string>
        /// {
        ///     { "Name", "ProductName" },
        ///     { "Price", "UnitPrice" }
        /// };
        /// await context.BulkInsertAsync(products, "Products", mappings);
        /// </code>
        /// </example>
        /// 
        /// <example>
        /// Example 2: Ignore properties using attributes
        /// <code>
        /// public class Customer
        /// {
        ///     public int Id { get; set; }
        ///     public string FullName { get; set; }
        /// 
        ///     [BulkIgnore]
        ///     public string TempNotes { get; set; }
        /// 
        ///     [NotMapped]
        ///     public string CalculatedField { get; set; }
        /// }
        /// 
        /// var mappings = new Dictionary<string, string>
        /// {
        ///     { "FullName", "CustomerName" }
        /// };
        /// await context.BulkInsertAsync(customers, "Customers", mappings);
        /// </code>
        /// </example>
        /// 
        /// <example>
        /// Example 3: Snake_case mapping
        /// <code>
        /// var mappings = new Dictionary<string, string>
        /// {
        ///     { "FirstName", "first_name" },
        ///     { "LastName", "last_name" },
        ///     { "Email", "email_address" }
        /// };
        /// await context.BulkInsertAsync(users, "user_accounts", mappings);
        /// </code>
        /// </example>
        /// </param>
        /// <param name="bulkCopyTimout">The bulk copy timeout in seconds. Default is set to 30.</param>
        public static async Task BulkInsertAsync<T>(this DbContext context, IEnumerable<T> entities, string tableName = null,
            Dictionary<string, string> columnMappings = null, int bulkCopyTimout = 30)
            where T : class
        {
            var dataTable = entities.ToBulkDataTable(columnMappings, out var finalMappings);
            await BulkCopyAsync(context, dataTable, tableName ?? typeof(T).Name, finalMappings, 30);
        }

        private static void BulkCopy(DbContext context, DataTable table, string tableName,
            Dictionary<string, string> finalMappings, int bulkCopyTimeout = 30)
        {
            var connection = (SqlConnection)context.Database.Connection;
            var wasClosed = connection.State == ConnectionState.Closed;

            if (wasClosed)
                connection.Open();

            using (var bulkCopy = new SqlBulkCopy(connection))
            {
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.BulkCopyTimeout = bulkCopyTimeout;

                foreach (var map in finalMappings)
                {
                    bulkCopy.ColumnMappings.Add(map.Key, map.Value);
                }
                bulkCopy.WriteToServer(table);
            }

            if (wasClosed)
                connection.Close(); // Ensure the connection is closed after the operation, if it was closed before
        }

        private static async Task BulkCopyAsync(DbContext context, DataTable table, string tableName, Dictionary<string, string> mappings,
            int bulkCopyTimeout = 30)
        {
            var connection = (SqlConnection)context.Database.Connection;
            var wasClosed = connection.State == ConnectionState.Closed;

            if (wasClosed)
                await connection.OpenAsync();

            using (var bulkCopy = new SqlBulkCopy(connection))
            {
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.BulkCopyTimeout = bulkCopyTimeout;
                foreach (var map in mappings)
                {
                    bulkCopy.ColumnMappings.Add(map.Key, map.Value);
                }
                await bulkCopy.WriteToServerAsync(table);
            }

            if (wasClosed)
                connection.Close();  //Ensure the connection is closed after the operation, if it was closed before
        }
    }
}





As the code shows, we use the DbContext from EntityFramework and get the connection string from the Database.Connection.ConnectionString property. We also use the Database.Connection property and cast it to SqlConnection to get the connection object. As we see, SqlBulkCopy is part of ADO.Net and relies upon column mappings. The code above will ignore the properties marked by the BulkIgnore attribute and the NotMapped attribute

BulkIgnoreAttribute.cs



using System;

namespace BulkOperationsEntityFramework.Lib.Attributes
{

    /// 
    /// Indicates that a property should be ignored during bulk insert operations.
    /// 
    [AttributeUsage(AttributeTargets.Property)]
    public class BulkIgnoreAttribute : Attribute
    {
    }

}


The following helper class creates a DataTable for a collection of entities (an IEnumerable) and uses reflection to make such a mapping. Note that column mappings can also be provided in the BulkInsertExtensions methods shown above to override property to column mapping, if desired. Default sql bulk copy timeout is set to 30 seconds (it is also the default timeout)

DataTableExtensions.cs


The following code provides code for creating data table for a collection of objects (IEnumerable). The BulkInsertExtensions uses the specific method ToBulkDataTable. This data table skips properties that are marked with [NotMapped] or [BulkIgnore] attributes. Both ToDataTable methods shown here allows column mappings to customize the property to column mappings.

using BulkOperationsEntityFramework.Lib.Attributes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace BulkOperationsEntityFramework.Lib.Extensions
{

    public static class DatatableExtensions
    {

        /// <summary>
        /// Converts an IEnumerable of type T to a DataTable.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="columnMappings"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(this IEnumerable<T> data, Dictionary<string, string> columnMappings = null)
        {
            var dataTable = new DataTable();
            var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var prop in properties)
            {
                var columnName = columnMappings != null && columnMappings.ContainsKey(prop.Name) ? columnMappings[prop.Name] : prop.Name;
                dataTable.Columns.Add(columnName, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }

            foreach (var item in data)
            {
                var values = properties.Select(p => p.GetValue(item) ?? DBNull.Value).ToArray();
                dataTable.Rows.Add(values);
            }

            return dataTable;
        }

        /// <summary>
        /// Converts an IEnumerable of type T to a DataTable with specified column mappings. Tailored for Bulk operations.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        /// <param name="columnMappings"></param>
        /// <param name="finalMappings"></param>
        /// <returns></returns>
        public static DataTable ToBulkDataTable<T>(this IEnumerable<T> entities, Dictionary<string, string> columnMappings, out Dictionary<string, string> finalMappings)
        {
            var dataTable = new DataTable();
            var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p =>
                    !Attribute.IsDefined(p, typeof(System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute)) &&
                    !Attribute.IsDefined(p, typeof(BulkIgnoreAttribute)))
                .ToArray();

            finalMappings = new Dictionary<string, string>();

            foreach (var prop in properties)
            {
                var columnName = columnMappings != null && columnMappings.ContainsKey(prop.Name)
                    ? columnMappings[prop.Name]
                    : prop.Name;

                dataTable.Columns.Add(columnName, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                finalMappings[prop.Name] = columnName;
            }

            foreach (var entity in entities)
            {
                var values = properties.Select(p => p.GetValue(entity) ?? DBNull.Value).ToArray();
                dataTable.Rows.Add(values);
            }

            return dataTable;
        }

    }
}




BulkInsertBenchmark

The following benchmark is available in the solution for benchmarking the different approaches to bulk copy with EntityFramework.
  • EF - add one and save in a loop. Not suprisingly, performs the worst due to the many roundtrips to the database
  • EF - add one by one and save at the end. Better performance, since we have one roundtrip. Will handle poor cases where we try to add many items in a batch.
  • EF - addrange and save at the end. Similar to the one above, one roundtrip to database
  • Dapper - add as batch and save. Minor better speed than the previous two. One roundtrip to database.
  • SqlBulkCopy - clearly the fastest way to insert a batch of entities to the database



using BenchmarkDotNet.Attributes;
using Bogus;
using BulkOperationsEntityFramework.Lib.Extensions;
using BulkOperationsEntityFramework.Models;
using Dapper;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace BulkOperationsEntityFramework.Benchmarks
{

    [MemoryDiagnoser]
    public class BulkInsertBenchmark
    {

        private static readonly Faker Faker = new Faker();

        [Params(100)]
        public int Size { get; set; }

        //First benchmark - Naive approach - add one and one entity and save changes everytime with round trip to database

        [Benchmark(Description = "EFAddOneAndSave - Add one user and then save. Then add another one. Results in many round-trips to database.")]
        public async Task EFAddOneAndSave()
        {
            using (var context = new ApplicationDbContext())
            {
                foreach (var user in GetUsers())
                {
                    context.Users.Add(user);
                    await context.SaveChangesAsync();
                }
            }
        }

        [Benchmark(Description = "EFAddOneByOneAndSave - Add one by one user, but save once after adding them. Results in one round-trip to database")]
        public async Task EFAddOneByOneAndSave()
        {
            using (var context = new ApplicationDbContext())
            {
                foreach (var user in GetUsers())
                {
                    context.Users.Add(user);

                }
                await context.SaveChangesAsync();
            }
        }

        [Benchmark(Description = "EFAddRange - Adds the users, then does the save. Results in one round-trip to database")]
        public async Task EFAddRange()
        {
            using (var context = new ApplicationDbContext())
            {
                var users = GetUsers();
                context.Users.AddRange(users);
                await context.SaveChangesAsync();
            }
        }

        [Benchmark(Description = "DapperInsertRange - Uses Dapper to insert the users. Results in one round-trip to database")]
        public async Task DapperInsertRange()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["App"].ConnectionString;

            string sql = @"
                INSERT INTO Users (Email, FirstName, LastName, PhoneNumber)
                VALUES (@Email, @FirstName, @LastName, @PhoneNumber)
            ".Trim();

            using (var connection = new SqlConnection(connectionString))
            {
                var users = GetUsers().Select(u => new 
                {
                    u.Email,
                    u.FirstName,
                    u.LastName,
                    u.PhoneNumber
                }).ToArray();

                await connection.ExecuteAsync(sql, users);
            }
        }

        [Benchmark(Description = "SqlBulkCopy - Uses SqlBulkCopy to insert the users. Results in one round-trip to database")]
        public async Task SqlBulkCopy()
        {
            using (var context = new ApplicationDbContext())
            {
                await context.BulkInsertAsync(GetUsers(), "Users");
            }
        }



        private User[] GetUsers() =>
            Enumerable.Range(1, Size).Select(i => new User
            {
                Email = Faker.Internet.Email(),
                FirstName = Faker.Name.FirstName(),
                LastName = Faker.Name.LastName(),
                PhoneNumber = Faker.Phone.PhoneNumber()
            }).ToArray();

    }

}


The following screenshots shows the benchmark results after running Benchmark.NET for the mentioned approaches and comparing the performance.

Results

Benchmark for a given batch size of 100

Benchmark for a given batch size of 100