title: How can I enforce a rule where only one instance of a class can have a certain parameter set to true?
body: <p>In my .Net Core Entity Framework game, I have a relationship between the Castle and CastleKnights classes.</p>
  <p>The Castle can have many CastleKnights associcated with one Castle.</p>
  <p>But each Castle can only have one CastleKnight that is flagged as the imposter(IsImposter).</p>
  <p>I'm trying to figure out how I can ensure that each Castle only has one CastleKnight that has the IsImposter flag set to <code>true</code>.</p>
  <p>I tried this , putting this in my CastleKnights class, but it did not work:</p>
  <pre><code>if(IsImposter = true) {
  
  }
  </code></pre>
  <p>It tells me that <code>the name &quot;IsImposter&quot; does not exist in the current context</code>.</p>
  <p>How can I get the Casetle class to &quot;know&quot; when a Castle already has a CastleKnight with <code>IsImposter</code> set to true?</p>
  <p>Thanks!</p>
  <p>Here are my 2 classes:</p>
  <pre><code> public partial class Castle
  {
      public Castle()
      {
          CastleKnights = new HashSet&lt;CastleKnights&gt;();
      }
      [Key]
      public Guid CastleId { get; set; }
      public string CastleDescription { get; set; }
  
      public virtual ICollection&lt;CastleKnights&gt; CastleKnights { get; set; }
  }
  
  
  public partial class CastleKnights
  {
      public Guid KnightId { get; set; }
      public Guid? CastleId { get; set; }
      public bool? IsImposter { get; set; }
  
      public virtual Castle Castle { get; set; }
  }
  </code></pre>