title: How to create new entity object, modify it in Model before SaveChanges()
body: <p>I've been dwelling with this issue for hours and couldn't figure out the problem. So I'm using ASP.NET MVC 3 with Entity Framework 4.1. I am writing the account management controller and models from scratch. My problem is that whenever the Controller receives the filled-up entity object from the View and sends it onwards to the Model, the Model won't save it if I try to modify any of the fields beforehand.</p>
  
  <p>Here is the relevant portion of my model, named ModelManager.cs :</p>
  
  <pre><code>public class MemberManager
  {
      private DAL db = new DAL();
  
      public void Add(Member member)
      {
          member.Password = Crypto.HashPassword(member.Password);
          db.Members.Add(member);
          db.SaveChanges();
      }
  </code></pre>
  
  <p>That model gets the <code>Member member</code> entity object as a parameter from the Controller, relevant portion of-which is below:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Register(Member member)
      {
              try
              {
                  if (ModelState.IsValid)
                  {
                      MemberManager memberManager = new MemberManager();
                      if (!memberManager.UsernameExist(member.Username))
                      {
                          memberManager.Add(member);
                          FormsAuthentication.SetAuthCookie(member.FirstName, false);
                          return RedirectToAction("Welcome", "Home");
                      }
                      else
                      {
                          ModelState.AddModelError("", "Username already taken.");
  
                      }
                  }
              }
              catch
              {
                  ModelState.AddModelError("", "Could not complete account creation. Please try again.");
              }
  
              return View(member);
      }
  </code></pre>
  
  <p>If I remove <em>any</em> modification to any property of the entity object in the Model, the code works (i.e. I remove the line where I <code>Crypto.HashPassword</code> the entity object's Password field. At first I thought the issue was with HashPassword, but if I change that line to simply change the member.Password to string "1", it won't work either. </p>
  
  <p>So, what am I doing wrong? I'm entirely new to programming so bear with me if the issue is quite flagrant. Isn't it possible to create an entity object from the View upon account creation, send it through to the Controller that then passes it on to the Model which modifies the Password to hash it before saving it? </p>
  
  <p>BTW, the exception error raised is:</p>
  
  <blockquote>
    <p>System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at Politiq.Models.ObjectManager.MemberManager.Add(Member member) in C:\Users\C. Yehia\Documents\Visual Studio 2010\Projects\Politiq2\Models\ObjectManager\MemberManager.cs:line 21 at Politiq.Controllers.AccountController.Register(Member member) in C:\Users\C. Yehia\Documents\Visual Studio 2010\Projects\Politiq2\Controllers\AccountController.cs:line 39</p>
  </blockquote>