title: How do I get DB generated IDs back when using repository pattern?
body: <p>In my solution I have a Domain project and a DataAccess project. My Domain project contains domain objects and interfaces for various repositories and an interface for a UnitOfWork which will contain all my repositories. My DataAccess project contains the implementations of all these interfaces and the EF versions of my Domain objects.</p>
  
  <p>So if I have a class called <code>Person</code> and I want to add a <code>Person</code> to the DB it would look like this:</p>
  
  <pre><code>unitOfWork.People.Add(person);
  unitOfWork.Complete();
  </code></pre>
  
  <p>Inside <code>unitOfWork.People.Add(person)</code> I map my domain <code>Person</code> object to my EF <code>Person</code> object with AutoMapper. Then I add my EF <code>Person</code> object to the EF <code>DbContext</code> object.</p>
  
  <pre><code>public void Add(Person person) {
    DataAccess.Entities.Person dbPerson = Mapper.Map&lt;DataAccess.Entities.Person&gt;(person);
    dbContext.People.Add(dbPerson);
  }
  </code></pre>
  
  <p>The Complete() function just wraps SaveChanges().</p>
  
  <pre><code>public int Complete() {
    dbContext.SaveChanges();
  }
  </code></pre>
  
  <p>Normally EF updates an inserted object's DB generated ID after insertion. But in this case EF knows nothing about my Domain Person object.</p>
  
  <p>Since my Domain Person object doesn't have it's ID, I can't update the DB entry or re-fetch it from the database.</p>
  
  <p>The only alternative I can think of is generating a guid on the server and using that as an ID. But this creates redundancy in the form of two IDs. It also isn't very user friendly since users will reference this ID in search fields. Remembering ID 135 is much simpler than remembering a guid.</p>
  
  <p>Is there some way to get dbContext to update the domain object's ID directly or alternatively have the ID bubble up through the layers?</p>