title: Copying view model to data model
body: <p>I'm trying to develop my first ASP.net MVC project with Entity Framework. I wasn't using any view models, i was using same models for both in views and database transactions. Here is the thing, i have custom user table. In creation, i have 5 things in my <code>User</code> model: <code>UserName</code>, <code>UserPassword</code>, <code>FullName</code>, <code>Branch</code> and <code>BranchId</code>(Navigation to another table). But when i want to edit a user, i don't need the UserPassword field, because changing the password won't be possible for now. So i created a model same with the <code>User</code> model except the <code>UserPassword</code> field named <code>UserEdit</code>.</p>
  
  <p>In create view i use my User model, in edit view i use the UserEdit model. In controller i'm using automapper and copy values from <code>User</code> to <code>UserEdit</code> and return that to view. It's working fine, problem is about updating.</p>
  
  <p>I'm trying to update the user like this:</p>
  
  <pre><code>    public bool Update(UserEdit userEdit) {
          User user = Find(userEdit.UserUsername);
  
          Mapper.CreateMap&lt;UserEdit, User&gt;();
  
          user = (User)Mapper.Map(userEdit, user, typeof(UserEdit), typeof(User));
  
          if (_modelState.IsValid)
          {
              _transactionManager.GetContext().Entry(user).State = System.Data.Entity.EntityState.Modified;
              _transactionManager.CommitTransaction();
              return true;
          }
          else
          {
              return false;
          }
      }
  </code></pre>
  
  <p>But it gives me this error:</p>
  
  <blockquote>
    <p>Additional information: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.</p>
  </blockquote>
  
  <p>When i check the user i'm trying to update, i see Entity Framework related objects in it. So i'm thinking reason of this error is probably about branch table and those related objects in the user object. I really don't need those entity related objects in my user object. If i could only copy the properties in the model, it would be great. Maybe i'm doing something else wrong. Because i know people use view models all the time, there should be an easy way to do it.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks</p>