title: asp.net-core / c# - check if a row exists in a table that has 2 primary keys?
body: <p>This is my table schema in Microsoft SQL db: </p>
  
  <pre><code>[account_id](PK, FK, int, not null)
  [business_id](PK, FK, int, not null)
  [identifier](nvarchar(100), null)
  </code></pre>
  
  <p>Before adding a new row into this table, I want to check if a row exists that has the same values in two primary keys. For example I have a table:</p>
  
  <pre><code>account_id | business_id | identifier 
  _____________________________________    
           1 |           2 | abc
           2 |           3 | cdf
  </code></pre>
  
  <p>Now I try different cases: </p>
  
  <pre><code>context.add(account_id: 1, business_id: 3, identifier: null) =&gt; success
  context.add(account_id: 1, business_id: 2, identifier: null) =&gt; fail
  context.add(account_id: 2, business_id: 2, identifier: null) =&gt; success
  context.add(account_id: 3, business_id: 3, identifier: null) =&gt; success
  </code></pre>
  
  <p>In short, I want to check if a row already exists with two same primary keys as <code>account_id</code> and <code>business_id</code>, otherwise, create a new row.</p>
  
  <p>This is my code without checking duplicates yet:</p>
  
  <pre><code>[HttpPost("generate-identifier")]
  [ProducesResponseType(typeof(void), 204)]
  public IActionResult GenerateIdentifier(GenerateIdentifierRequest model)
  {
    if (!ModelState.IsValid) return BadRequest(GetValidationErrors(ModelState));
  
    BusinessAccount businessAccount = new BusinessAccount() {
      AccountId = GetCurrentUserId(),
      BusinessId = model.BusinessId,
      Identifier = model.Identifier
    };
  
    context.Add(businessAccount);
    context.SaveChanges();
  
    return NoContent();
  }
  </code></pre>