title: C# Entity Framework: Data validation between add to context and saveChanges()
body: <p>I have a simple scenario using the Entity Framework in C#. I have an Entity Post: </p>
  
  <pre><code>public class Post
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>In my PostManager I have these methods:</p>
  
  <pre><code>public int AddPost(string name, string description)
      {
          var post = new Post() { Name = name, Description = description };
  
          using (var db = new DbContext())
          {
            var res = db.Posts.Add(post);
            res.Validate();
            db.SaveChanges();
            return res.Id;
          }
      }
  
      public void UpdatePost(int postId, string newName, string newDescription)
      {
          using (var db = new DbContext())
          {
              var data = (from post in db.Posts.AsEnumerable()
                  where post.Id == postId
                  select post).FirstOrDefault();
  
              data.Name = newName;
              data.Description = newDescription;
              data.Validate();
              db.SaveChanges();
          }
      }
  </code></pre>
  
  <p>The method validate() refers to class:</p>
  
  <pre><code>public static class Validator
  {
      public static void Validate(this Post post)
      {
          if ( // some control)
              throw new someException();
      }
  </code></pre>
  
  <p>I call the validate method before the savechanges() but after adding the object to the context. What's the best practice to validate data in this simple scenario? It's better validate the arguments instead? What's happen to object post if the validate method throw exception after adding the object to the context?</p>
  
  <p>UPDATE:</p>
  
  <p>I have to throw a custom set of exception depending on data validation error.</p>