title: How do I deal with concurrency regarding references between aggregate roots?
body: <p>I have an Aggregate Root (<code>Slave</code>) whcih references anothe AG (<code>Master</code>), that is I have a foreign key constraint in DB.</p>
  
  <p>It's very unlikely, but still possible that referenced <code>Master</code> can be deleted right before inserting referencing <code>Slave</code>. This is possible to avoid using <code>repeatable read</code> or <code>serializable</code> transaction isolation levels, but it affects performnce negatively.</p>
  
  <p>I prefer optimistic concurrency check in this case: try and may be fail. However, it is difficult to trace thrown <code>DbUpdateEception</code> back to the semantics of the problem. </p>
  
  <p>In other words having textual message is not a very convenient for understanding WHICH reference actually failed:</p>
  
  <blockquote>
    <p>An error occurred while updating the entries. See the inner exception
    for details.</p>
    
    <p>Inner exception SqlException - "The INSERT statement conflicted with
    the FOREIGN KEY constraint "FK_Slaves_Masters_MasterId". The conflict
    occurred in database "EFExperiments", table "dbo.Masters", column
    'Id'. The statement has been terminated."</p>
  </blockquote>
  
  <p>I've scanned <code>SqlEception</code> and it looks like neither index name, nor table with column names present there separetely, only the message. Yet it would be very nice to interpret this error as 'ReferencedEntityNotFoundException`containing Name and type of the entity. This makes it possible to show friendly message to the user explaining what happend: </p>
  
  <blockquote>
    <p>Master (id=123) does not exist anymore. Assign Slave to another one.</p>
  </blockquote>
  
  <p>Which is the best aproach here? Is it possible to make SQL server be more verbose and return names of the tables and columns, or do I have to parse the message? Does any EF extension library exist for this purpose?</p>
  
  <p>P.S. There is also an option to remove FK constraint at all and allow the existence of dangling slaves showing them to the user as having nonvalid state. But it is definitely an overkill.</p>