title: How to map auto increment Id from domain model to entity model in Entity Framework ASP.NET Web API
body: <p>I am trying to follow DDD for my current project. My question is specifically regarding POST request that contains values for my domain model. To simplify, let's say following is my domain model:</p>
  
  <pre><code>class Person
  {
      public int Id {get, set};
      public string name {get, set};
  }
  </code></pre>
  
  <p>And below is the entity model backing above domain:</p>
  
  <pre><code>class PersonEF
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id {get, set};
      public string name {get, set};
  }
  </code></pre>
  
  <p>Now when I receive a POST request to create a new Person it doesn't contain the Id as it's supposed to be generated by the database. I first map Person to PersonEF using AutoMapper which sets the Id to 0 in PersonEF and hence the INSERT in database fails. So to fix these my options are:</p>
  
  <ol>
  <li><p>Name Id as something else in Person. If I do this then I would have to write custom mapping just for Id for GET requests.</p></li>
  <li><p>Make Id nullable (int?) in both Person and PersonEF and this way auto increment will work, but having key as nullable int doesn't sound like a good idea in itself.</p></li>
  </ol>
  
  <p>So please suggest the best way to do this. </p>
  
  <p>EDIT</p>
  
  <p>My bad. Code was missing context.SaveOrUpdate(). I added that and it's working as expected.</p>