<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kehinde Ademola Olosunde</title>
    <description>The latest articles on DEV Community by Kehinde Ademola Olosunde (@olosunde_kehinde_e4f0c25d).</description>
    <link>https://dev.to/olosunde_kehinde_e4f0c25d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4244495%2F23c20821-44f4-4f7a-8115-41e08dce8bcf.jpg</url>
      <title>DEV Community: Kehinde Ademola Olosunde</title>
      <link>https://dev.to/olosunde_kehinde_e4f0c25d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olosunde_kehinde_e4f0c25d"/>
    <language>en</language>
    <item>
      <title>Points += 50 is a bug: a rewards engine for .NET that never pays twice</title>
      <dc:creator>Kehinde Ademola Olosunde</dc:creator>
      <pubDate>Sat, 14 Nov 2026 17:00:44 +0000</pubDate>
      <link>https://dev.to/olosunde_kehinde_e4f0c25d/points-50-is-a-bug-a-rewards-engine-for-net-that-never-pays-twice-28me</link>
      <guid>https://dev.to/olosunde_kehinde_e4f0c25d/points-50-is-a-bug-a-rewards-engine-for-net-that-never-pays-twice-28me</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc3uab2chl5m3lf1mtpoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc3uab2chl5m3lf1mtpoq.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
Every app with points has a line like this somewhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;learner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Points&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveChangesAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks harmless. Then real traffic arrives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The mobile app times out and &lt;strong&gt;retries&lt;/strong&gt; "lesson completed". The learner gets 100 points.&lt;/li&gt;
&lt;li&gt;A message queue delivers the same event &lt;strong&gt;twice&lt;/strong&gt;, which at-least-once delivery is allowed to do. The learner gets 100 points again.&lt;/li&gt;
&lt;li&gt;The learner &lt;strong&gt;double-taps&lt;/strong&gt; "Redeem" on the last tutor session. Two requests both read "1 left", and both succeed.&lt;/li&gt;
&lt;li&gt;Staff cancel a redemption, the request is retried, and the points are &lt;strong&gt;refunded twice&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these is unusual. Once points can buy something they behave like money, and code that handles money needs to be exact.&lt;/p&gt;

&lt;p&gt;I'm adding a rewards programme to StrideWell, the platform for gyms and coaches I'm building. Members earn points for sessions and streaks, and spend them on rewards their gym pays for. I built the engine as a separate library, and I've now open-sourced it as &lt;strong&gt;&lt;a href="https://github.com/modelhub-solutions/ModelHub.Gamification" rel="noopener noreferrer"&gt;ModelHub.Gamification&lt;/a&gt;&lt;/strong&gt;. It's the second library I've extracted from StrideWell. The first was &lt;a href="https://dev.to/olosunde_kehinde_e4f0c25d/the-forgotten-where-clause-claim-based-multi-tenancy-for-aspnet-core-and-ef-core-2o63"&gt;ModelHub.MultiTenancy&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package ModelHub.Gamification.EntityFrameworkCore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A worked example: an online learning platform
&lt;/h2&gt;

&lt;p&gt;Imagine an online learning platform where each school is a tenant. Learners earn points like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;th&gt;Points&lt;/th&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LessonCompleted&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;up to 3 a day&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QuizPassed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;only for a score of 80 or more&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;StreakMilestone&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;150&lt;/td&gt;
&lt;td&gt;for a 7-day streak&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddGamification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AppDb&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DefaultRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RuleDefinition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"LessonCompleted"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MaxPerDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DefaultRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RuleDefinition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"QuizPassed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""{"score":{"gte":80}}"""&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DefaultRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RuleDefinition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"StreakMilestone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""{"streakDays":7}"""&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TimeZone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TimeZoneInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FindSystemTimeZoneById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Europe/London"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;modelBuilder.ApplyGamificationModel()&lt;/code&gt; in &lt;code&gt;OnModelCreating&lt;/code&gt; adds six tables to your own DbContext. Your app saves its own work first, then reports what happened:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QuizAttempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveChangesAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HandleAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GamificationEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;schoolId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;learnerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"QuizPassed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"score"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Score&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Credited, AlreadyCredited, NoMatchingRule, LimitReached or EarningDisabled&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One school, Northbridge Academy, wants to change two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an extra 10 points for lessons finished before 7am;&lt;/li&gt;
&lt;li&gt;a cap of 300 points a day.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;programs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveRuleAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PointsRule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;TenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;northbridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;EventType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"LessonCompleted"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Points&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MaxPerDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;programs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveRuleAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PointsRule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;TenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;northbridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;EventType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"LessonCompleted"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Points&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""{"localHour":{"lt":7}}"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;programs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveSettingsAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PointsProgram&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;TenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;northbridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;DailyEarnCap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A school's own rules for an event type &lt;strong&gt;replace&lt;/strong&gt; the defaults for that event type. That's why Northbridge restates the 50-point rule. It's also how a school switches an event off: it saves one disabled rule. When an event would take a learner over the cap, it pays only what's left under the cap.&lt;/p&gt;

&lt;p&gt;Learners spend their points on rewards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;redeemed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;rewards&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RedeemAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;northbridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;learnerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tutorSessionId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// redeemed.Redemption.Code == "RW-7K3QX9", which the learner reads out to staff&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the API. Now back to the four failures from the start of this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Paying once: let the database say no
&lt;/h2&gt;

&lt;p&gt;The obvious fix is to check first: "has this event already paid?" On its own, that isn't enough. Two copies of the same event can arrive a few milliseconds apart. Both check, both see "no", and both insert.&lt;/p&gt;

&lt;p&gt;So every credit carries an idempotency key, made from the event type and the reference you pass in, such as &lt;code&gt;earn:QuizPassed:8841&lt;/code&gt;. A unique index makes the database refuse the second one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HasIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WalletId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IdempotencyKey&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsUnique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The engine still checks first, which handles the usual case cheaply. The index handles the race. When the second insert fails, the engine does three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It drops its own unsaved changes. Any of your app's unsaved changes stay as they were.&lt;/li&gt;
&lt;li&gt;It reads the wallet again.&lt;/li&gt;
&lt;li&gt;It finds the key and returns &lt;code&gt;AlreadyCredited&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The same retry covers a learner's first two events arriving at once, because there can only be one wallet per tenant and user.&lt;/p&gt;

&lt;p&gt;Some rows don't need a key, such as expiry and staff adjustments. They leave it null, so they never collide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EF Core's SQL Server provider adds an &lt;code&gt;IS NOT NULL&lt;/code&gt; filter to unique indexes on nullable columns.&lt;/li&gt;
&lt;li&gt;Other databases treat NULLs as distinct from each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why the reference you pass matters. Use the id of the thing that happened, such as the quiz attempt, never &lt;code&gt;Guid.NewGuid()&lt;/code&gt;. Get that right, and retrying becomes safe everywhere: in a mobile app, a queue consumer or a background job.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Spending once: concurrency tokens and a single save
&lt;/h2&gt;

&lt;p&gt;The double-tap on the last tutor session is a different race. Two separate redemptions are each valid on their own, but they compete for the same stock and the same points.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PointsWallet.Balance&lt;/code&gt; and &lt;code&gt;Reward.Stock&lt;/code&gt; are EF Core concurrency tokens. A redemption changes three things, and they're saved in one &lt;code&gt;SaveChangesAsync&lt;/code&gt;. Simplified:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Redemption&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redemption&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// carries the code the learner shows&lt;/span&gt;
&lt;span class="n"&gt;reward&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Stock&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// concurrency token&lt;/span&gt;
&lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;reward&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// concurrency token, written with a ledger row&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveChangesAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// all of it, or none of it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;EF Core turns the stock change into &lt;code&gt;UPDATE ... WHERE Stock = 1&lt;/code&gt;. If another request got there first, no row matches. EF Core then throws &lt;code&gt;DbUpdateConcurrencyException&lt;/code&gt; and the whole save is rolled back. The engine reads again and returns the honest answer, &lt;code&gt;OutOfStock&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One test covers exactly this. Two learners want one item, and one of the requests is working from stale data. One learner gets the session. The other gets &lt;code&gt;OutOfStock&lt;/code&gt; and keeps all their points.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Refunding once
&lt;/h2&gt;

&lt;p&gt;Staff can cancel a pending redemption. Cancelling refunds the points and puts the item back in stock, again in a single save. Two separate safeguards stop a double refund:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Redemption.Status&lt;/code&gt; is a concurrency token, so only one of fulfil, cancel or expire can succeed.&lt;/li&gt;
&lt;li&gt;The refund has its own idempotency key, &lt;code&gt;refund:&amp;lt;redemption id&amp;gt;&lt;/code&gt;. Even if a bug somewhere else tried, the same redemption can't be refunded twice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Keep a ledger, not just a number
&lt;/h2&gt;

&lt;p&gt;Each user has a wallet with a balance, but the ledger is the real record of their points. Every change is a row:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;earn:&lt;/strong&gt; points from the rules&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;award:&lt;/strong&gt; a fixed prize outside the rules&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;redeem&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;refund&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;expire&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;adjust:&lt;/strong&gt; a staff correction, which requires a note and a name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The balance only ever changes together with a ledger row. That gives you three things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;History.&lt;/strong&gt; "Where did my points go?" is a query, not an investigation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Levels that never go down.&lt;/strong&gt; Levels come from lifetime points, so spending on a reward never costs a learner a level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fair expiry.&lt;/strong&gt; If points expire after 30 days, which points expire? Spending should use the oldest points first. With a ledger, that's simple arithmetic: everything credited before the cutoff, minus everything that has ever left the wallet.

&lt;ul&gt;
&lt;li&gt;For example: earn 100 points on day 0, spend 30 on day 10, and earn 50 on day 20.&lt;/li&gt;
&lt;li&gt;On day 31, 70 points expire, and the 50 from day 20 stay.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  "Today" is not a UTC date
&lt;/h2&gt;

&lt;p&gt;"Up to 3 lessons a day" sounds simple until you ask whose day you mean. Count by the UTC date, and a learner five hours ahead of UTC who finishes a lesson at 00:30 their time is still on "yesterday". An early-morning bonus would also fire at the wrong hour.&lt;/p&gt;

&lt;p&gt;The engine works out where each day and week starts and ends in the configured time zone. It then queries the ledger using those boundaries converted to UTC.&lt;/p&gt;

&lt;p&gt;It also adds two values to every event's context: &lt;code&gt;localHour&lt;/code&gt; and &lt;code&gt;dayOfWeek&lt;/code&gt;. That's how Northbridge's early-bird rule works.&lt;/p&gt;

&lt;p&gt;An event reported late counts toward the day it happened, not the day it arrived.&lt;/p&gt;

&lt;p&gt;One small trap: in a few time zones, midnight doesn't exist on the day the clocks change. On those days, the engine starts the day at the first valid time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug the extraction found
&lt;/h2&gt;

&lt;p&gt;Pulling code out of an app makes you read it with fresh eyes. StrideWell counted streaks within each week and reset them every Monday, which suits a weekly progress view. But the rewards rules also included a bonus for a 30-day streak. A streak that resets every Monday can never get past 7 days, so that bonus could never pay out.&lt;/p&gt;

&lt;p&gt;The rewards programme hasn't launched yet, so no member missed out. But it's exactly the kind of bug that survives because each piece looks right on its own.&lt;/p&gt;

&lt;p&gt;In the library, the two jobs are separate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Streaks.Current&lt;/code&gt; counts consecutive days, with no week boundary.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Streaks.Week&lt;/code&gt; builds the weekly view.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A milestone's reference includes the date its run started, such as &lt;code&gt;streak:2026-11-10:7&lt;/code&gt;. So each run pays its bonus once, and a new run can pay again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It runs after your save, not inside it.&lt;/strong&gt; If your process stops between your &lt;code&gt;SaveChangesAsync&lt;/code&gt; and the call to the points engine, that event isn't credited until something retries it. Retrying is always safe, because each event pays once. An outbox or a background job that replays events closes the gap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One time zone per app.&lt;/strong&gt; Each school can't have its own time zone yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI runs on SQLite.&lt;/strong&gt; The library only uses standard EF Core features (unique indexes and concurrency tokens), but SQL Server and PostgreSQL aren't tested in CI yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's the accounting, not the game.&lt;/strong&gt; There are no leaderboards, no UI and no cash value. Those go on top.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The expiry job saves one wallet at a time.&lt;/strong&gt; That's fine for thousands of learners, but it isn't tuned for millions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/modelhub-solutions/ModelHub.Gamification" rel="noopener noreferrer"&gt;https://github.com/modelhub-solutions/ModelHub.Gamification&lt;/a&gt;. The sample API in &lt;code&gt;/samples&lt;/code&gt; is the learning platform from this post, with a &lt;code&gt;sample.http&lt;/code&gt; file you can run requests from.&lt;/li&gt;
&lt;li&gt;NuGet: &lt;a href="https://www.nuget.org/packages/ModelHub.Gamification.EntityFrameworkCore" rel="noopener noreferrer"&gt;https://www.nuget.org/packages/ModelHub.Gamification.EntityFrameworkCore&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;It targets .NET 8 and .NET 10, has 80 tests and uses the MIT licence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is version 0.1, so feedback is very welcome. Four issues are labelled &lt;strong&gt;good first issue&lt;/strong&gt;, ranging from an &lt;code&gt;in&lt;/code&gt; operator for rule conditions to a test for points expiry across several schools. If you've built points or loyalty into a .NET app, I'd love to hear what broke first.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm Kehinde, founder of ModelHub Solutions in Wales. I'm building StrideWell, a platform for gyms and coaches, and open-sourcing the parts that might help other teams.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>efcore</category>
      <category>gamification</category>
    </item>
    <item>
      <title>The forgotten Where clause: claim-based multi-tenancy for ASP.NET Core and EF Core</title>
      <dc:creator>Kehinde Ademola Olosunde</dc:creator>
      <pubDate>Sat, 14 Nov 2026 13:17:03 +0000</pubDate>
      <link>https://dev.to/olosunde_kehinde_e4f0c25d/the-forgotten-where-clause-claim-based-multi-tenancy-for-aspnet-core-and-ef-core-2o63</link>
      <guid>https://dev.to/olosunde_kehinde_e4f0c25d/the-forgotten-where-clause-claim-based-multi-tenancy-for-aspnet-core-and-ef-core-2o63</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fytmhpb4ig9xbyxtm8pcc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fytmhpb4ig9xbyxtm8pcc.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every multi-tenant app has the same bug waiting to happen. Someone adds an endpoint late on a Friday:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MapGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/appointments/today"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AppDb&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UtcNow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Appointments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StartsAt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToListAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works, the tests pass and it ships. It also returns &lt;strong&gt;every customer's&lt;/strong&gt; appointments, because nobody wrote &lt;code&gt;&amp;amp;&amp;amp; a.TenantId == currentTenant&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'm building StrideWell, a multi-tenant platform for gyms and personal trainers, on my own. The API has close to 100 database entities and more than 600 endpoints. Remembering the tenant filter on every query wasn't going to work, so I made the database layer remember it for me. I've now extracted that code into an open-source library: &lt;strong&gt;&lt;a href="https://github.com/modelhub-solutions/ModelHub.MultiTenancy" rel="noopener noreferrer"&gt;ModelHub.MultiTenancy&lt;/a&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package ModelHub.MultiTenancy
dotnet add package ModelHub.MultiTenancy.EntityFrameworkCore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The idea: the tenant comes from the token
&lt;/h2&gt;

&lt;p&gt;In most SaaS apps the signed-in user's token already says which tenant they belong to. So instead of resolving tenants from hostnames or a tenant store, the library reads a claim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddMultiTenancy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TenantIdClaimType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tenant_id"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// default&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PlatformAdminRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"support"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TenantAdminRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"practice_manager"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SubTenantScopedRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"receptionist"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UseAuthentication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UseTenantResolution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// after authentication, before authorization&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UseAuthorization&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anything that needs the current tenant injects &lt;code&gt;ITenantContext&lt;/code&gt;, which is scoped to the request.&lt;/p&gt;

&lt;h2&gt;
  
  
  A worked example: a vet practice SaaS
&lt;/h2&gt;

&lt;p&gt;Imagine VetDesk, practice-management software for groups of vet clinics. Each practice group is a &lt;strong&gt;tenant&lt;/strong&gt; and each clinic is a &lt;strong&gt;sub-tenant&lt;/strong&gt;. Northfield Vets (Leeds and York) and Riverside Animal Care share one database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Appointment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ITenantEntity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ISubTenantEntity&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Guid&lt;/span&gt; &lt;span class="nc"&gt;TenantId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;      &lt;span class="c1"&gt;// the practice group&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Guid&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;SubTenantId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// the clinic&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;PetName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;DateTime&lt;/span&gt; &lt;span class="nc"&gt;StartsAt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VetDb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DbContextOptions&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;VetDb&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ITenantContext&lt;/span&gt; &lt;span class="n"&gt;tenantContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DbContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ITenantAwareDbContext&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ITenantContext&lt;/span&gt; &lt;span class="nc"&gt;TenantContext&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tenantContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;DbSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Appointment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Appointments&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Appointment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnModelCreating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ModelBuilder&lt;/span&gt; &lt;span class="n"&gt;modelBuilder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;modelBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ApplyTenantQueryFilters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddDbContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;VetDb&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UseSqlServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UseMultiTenancy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what that buys you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The forgotten filter can't leak.&lt;/strong&gt; The Friday-afternoon endpoint above now returns Northfield's appointments only. EF Core adds the tenant condition to the SQL for every entity that implements &lt;code&gt;ITenantEntity&lt;/code&gt;, so nobody has to remember the &lt;code&gt;Where&lt;/code&gt;. A request with no tenant gets no rows, not all of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Sub-tenants.&lt;/strong&gt; A receptionist at York has a token with &lt;code&gt;role=receptionist&lt;/code&gt; and &lt;code&gt;sub_tenant_id=&amp;lt;York&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;diary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Appointments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhereVisibleToSubTenants&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToListAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The receptionist sees York. The practice manager runs the same code and sees both clinics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Writes are protected too.&lt;/strong&gt; Read filters are only half the story. A faulty CSV import running as Northfield tries to save a row stamped with Riverside's id:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Appointments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Appointment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;TenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;riversideId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PetName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Biscuit"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveChangesAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// TenantMismatchException: Cannot add Appointment: it belongs to a different tenant.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A save interceptor checks every added, modified and deleted tenant entity before anything reaches the database. New rows without a &lt;code&gt;TenantId&lt;/code&gt; get the current tenant stamped on them automatically. Moving a row to another tenant is refused.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Support can help without seeing everything.&lt;/strong&gt; A support engineer with a platform-admin role can send &lt;code&gt;X-Tenant-Id: &amp;lt;Northfield&amp;gt;&lt;/code&gt; to act as that tenant. For that request their cross-tenant bypass is switched &lt;strong&gt;off&lt;/strong&gt;, so they see exactly what Northfield sees, and the impersonation is logged. Anyone else sending the header is ignored, and that's logged too.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the filter works under the hood
&lt;/h2&gt;

&lt;p&gt;EF Core builds the model once and caches it, so how can one model filter by a different tenant on every request?&lt;/p&gt;

&lt;p&gt;The trick is that EF Core treats a reference to the &lt;code&gt;DbContext&lt;/code&gt; instance inside a query filter specially. When &lt;code&gt;ApplyTenantQueryFilters(this)&lt;/code&gt; builds the filter expression, it points at the context's &lt;code&gt;TenantContext&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TenantContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsPlatformAdmin&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TenantId&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TenantContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TenantId&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At query time EF Core swaps in the &lt;em&gt;current&lt;/em&gt; context instance and turns &lt;code&gt;TenantId&lt;/code&gt; into a SQL parameter. One cached model, a different tenant per request, and no string concatenation.&lt;/p&gt;

&lt;p&gt;Two version differences mattered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;EF Core 10&lt;/strong&gt; supports &lt;strong&gt;named query filters&lt;/strong&gt;, so the tenant filter is added as &lt;code&gt;ModelHub.MultiTenancy.Tenant&lt;/code&gt; and sits alongside your own filters (soft delete, for example).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EF Core 8&lt;/strong&gt; allows one filter per entity, so the library combines with any filter already declared. Call &lt;code&gt;ApplyTenantQueryFilters&lt;/code&gt; last in &lt;code&gt;OnModelCreating&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test suite runs on both, including a test that reuses one cached model for two tenants back to back and checks nothing leaks between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design decisions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Safe by default.&lt;/strong&gt; No tenant claim means no tenant rows. A malformed claim is treated as no tenant, never as "all tenants".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set once per scope.&lt;/strong&gt; In background jobs you call &lt;code&gt;TenantContext.SetTenant(id)&lt;/code&gt; yourself. A second call throws, so one tenant's job can't quietly switch to another mid-way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small surface area.&lt;/strong&gt; One interface, one middleware, one model-builder call, one interceptor.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limitations, honestly
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IgnoreQueryFilters()&lt;/code&gt; turns the protection off. Treat it like &lt;code&gt;sudo&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Raw SQL that bypasses LINQ (Dapper, &lt;code&gt;ExecuteSqlRaw&lt;/code&gt;) isn't filtered.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ExecuteUpdate&lt;/code&gt; and &lt;code&gt;ExecuteDelete&lt;/code&gt; respect the query filter, but they skip &lt;code&gt;SaveChanges&lt;/code&gt;, so the write checks in the interceptor don't run.&lt;/li&gt;
&lt;li&gt;With DbContext pooling, make sure each context gets the request's &lt;code&gt;ITenantContext&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you resolve tenants from hostnames or need per-tenant databases, &lt;a href="https://github.com/Finbuckle/Finbuckle.MultiTenant" rel="noopener noreferrer"&gt;Finbuckle.MultiTenant&lt;/a&gt; is the mature choice. This library is deliberately narrower: the common case where the token already knows the tenant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/modelhub-solutions/ModelHub.MultiTenancy" rel="noopener noreferrer"&gt;https://github.com/modelhub-solutions/ModelHub.MultiTenancy&lt;/a&gt; (a sample minimal API with JWT is in &lt;code&gt;/samples&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;NuGet: &lt;a href="https://www.nuget.org/packages/ModelHub.MultiTenancy" rel="noopener noreferrer"&gt;https://www.nuget.org/packages/ModelHub.MultiTenancy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Targets .NET 8 and .NET 10, MIT licence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's version 0.1, so feedback is very welcome. Issues and pull requests are open, and the issues labelled &lt;strong&gt;good first issue&lt;/strong&gt; are a good way in. If you run a multi-tenant .NET app, I'd love to hear how you handle tenant isolation today.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm Kehinde, founder of ModelHub Solutions in Wales. I'm building StrideWell, a platform for gyms and coaches, and open-sourcing the parts that might help other teams.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>efcore</category>
      <category>saas</category>
    </item>
  </channel>
</rss>
