title: Mock Database with ID column
body: <p>I'm (fairly) new to TDD, so be gentle!</p>
  
  <p>I am using CodeFirst to create my application in C# using EF6 in VBS2015 Community.</p>
  
  <p>I have an Interface created for my data access and am able to create a Mock Database in memory for Unit testing.</p>
  
  <p>Each of my Models has a unique identifier column which is named Id (For example, UserID)</p>
  
  <p>Ok, so when I add records to my Mock database, I need to have it create a new ID value for the record, as I reference this in later code. How do I do this with a generic set, when each table has a unique ID column? Do I have to mock out each table independently?</p>
  
  <p>My database interface looks like this:</p>
  
  <pre><code>Namespace Labinator2016.Lib.Headers
  {
      using System;
      using System.Collections.Generic;
      using System.Linq;
      public interface ILabinatorDb : IDisposable
      {
          IQueryable&lt;T&gt; Query&lt;T&gt;() where T : class;
          void Add&lt;T&gt;(T entity) where T : class;
          void Delete&lt;T1&gt;(System.Linq.Expressions.Expression&lt;Func&lt;T1, bool&gt;&gt; target) where T1 : class;
          void Remove&lt;T&gt;(T entity) where T : class;
          void Update&lt;T&gt;(T entity) where T : class;
          void SaveChanges();
      }
  }
  </code></pre>
  
  <p>My Mock Database then looks like this:</p>
  
  <pre><code>namespace Labinator2016.Tests.TestData
  {
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using Lib.Headers;
      using Labinator2016.Lib.Models;
      class FakeDatabase : ILabinatorDb
      {
          public IQueryable&lt;T&gt; Query&lt;T&gt;() where T : class
          {
              return this.Sets[typeof(T)] as IQueryable&lt;T&gt;;
          }
          public void Dispose() { }
          public void AddSet&lt;T&gt;(IQueryable&lt;T&gt; objects)
          {
              this.Sets.Add(typeof(T), objects);
          }
          public void Add&lt;T&gt;(T entity) where T : class
          {
              this.Added.Add(entity);
              IQueryable&lt;T&gt; temp = this.Sets[typeof(T)] as IQueryable&lt;T&gt;;
              List&lt;T&gt; existing = temp.ToList();
              existing.Add(entity);
              Sets[typeof(T)] = existing;
          }
          void ILabinatorDb.Delete&lt;T1&gt;(System.Linq.Expressions.Expression&lt;Func&lt;T1, bool&gt;&gt; Target)
          {
          }
          public void Update&lt;T&gt;(T entity) where T : class
          {
              this.Updated.Add(entity);
          }
          public void Remove&lt;T&gt;(T entity) where T : class
          {
              this.Removed.Add(entity);
          }
          public void SaveChanges()
          {
              this.saved++;
          }
          public Dictionary&lt;Type, object&gt; Sets = new Dictionary&lt;Type, object&gt;();
          public List&lt;object&gt; Added = new List&lt;object&gt;();
          public List&lt;object&gt; Updated = new List&lt;object&gt;();
          public List&lt;object&gt; Removed = new List&lt;object&gt;();
          public int saved = 0;
      }
  }
  </code></pre>
  
  <p>Currently, I have some code that gets a user object from a user table, based on a name. If it doesn't exist, then one is created first. Here is my code:</p>
  
  <pre><code>User existingUser = this.db.Query&lt;User&gt;().Where(u =&gt; u.UserName == newUserName).FirstOrDefault();
  if (existingUser == null)
  {
      User userToAdd = new User();
      userToAdd.UserName = newUserName;
      userToAdd.Password = PasswordHash.CreateHash("password");
      this.db.Add&lt;User&gt;(userToAdd);
      this.db.SaveChanges();
      existingUser = this.db.Query&lt;User&gt;().Where(u =&gt; u.EmailAddress == nu).FirstOrDefault();
  }
  </code></pre>
  
  <p>So, am I using the wrong approach completely in my application, or is there a way to mock out this functionality?</p>