title: Where to handle DB exceptions in Entity Framework and repository pattern
body: <p>how would you design this scenario (using Entity Framework 4.1, Code First and repository pattern):
  the Visual Studio solution contains the following projects</p>
  
  <pre><code>Solution
  |-Web Application Project
  |-DAL Project
  |-Model Project
  </code></pre>
  
  <p>So in the Model Project there are various classes. Suppose we have in there a class called User with the following definition (stripped down):</p>
  
  <pre><code>public class User{
  
      [Key]
      public int UserId { get; set; }
  
      ....
  
      //this property has a unique constraint created in a custom DB Initializer class
      public string email { get; set; }
  
      ....
  }
  </code></pre>
  
  <p>In the DAL Project reside the repository methods (Insert, Update etc.) and also the Initializer class:</p>
  
  <pre><code>public class MyDatabaseInitializer : IDatabaseInitializer&lt;MyDatabase&gt;
  {
      public void InitializeDatabase(MyDatabase context)
      {
          try
          {
              if (!context.Database.Exists())
              {
                  context.Database.Create();
                  context.Database.ExecuteSqlCommand(
                      "ALTER TABLE Users ADD CONSTRAINT uc_Email UNIQUE(Email)");
              }
          }
          catch (Exception ex)
          {
              throw ex.InnerException;
          }
      }
  }
  </code></pre>
  
  <p>The Commit method of my Unit of Work class looks like this:</p>
  
  <pre><code>public string Commit()
  {
      string errorMessage = string.Empty;
  
      try
      {
          Database.Commit();
      }
      catch (DbUpdateException updExc)
      {                                 
          errorMessage = updExc.InnerException.Message;            
      }                       
  
      return errorMessage;
  }
  </code></pre>
  
  <p>As you see I'm handling <code>DbUpdateException</code> in the <code>Commit()</code> method of the Unit of Work class; this means for each class which could cause an update error, this would be handled here.</p>
  
  <p>Suppose one inserts the User records with the following Data:</p>
  
  <pre><code>(UserId,....,Email,...)
  1, ... , person1@mail.com , ...
  2, ... , person1@mail.com , ...
  </code></pre>
  
  <p>It 's obvious that this will cause a DbUpdateException to occur. Of course this can be caught and propagated to the place where it should show up. I have the feeling that this design is completely wrong:</p>
  
  <ol>
  <li><p>Validation should occur for each property separately: shouldn't this be true also for the uniqueness of values of field? Does this mean that I have to merge DAL and MODEL into one project?</p></li>
  <li><p>How would I handle errors caused by a violation of the uniqueness for fieldA in table A, fieldB in table B, fieldC in table C? Using a generic error message "The value already exists" or "Uniqueness violation" is not very descriptive!</p></li>
  <li><p>Should I insert another project-Business layer which takes care of such error handling?</p></li>
  <li><p>Should I handle the errors in the (ASP.NET MVC) Action/Controller which does the update?</p></li>
  <li><p>How to handle a proper error message in a multi language application?</p></li>
  </ol>