title: Integration Test for Data access Layer with Entity Framework code-first
body: <p>iam starting writing test for my data access layer and iam looking for best practice.</p>
  
  <p>Now iam testing with a real db wich is auto generated from EF every times it find some differences in my model (defined with code-first fluent mapping) but doing this with many developers is difficult since every ones have different object developed.</p>
  
  <p>1) So there are way to use some in memory database? <br/>
  a)If don't use a connection string in my test project seen that all works fine but where is this db created?<br/>
  b) EF create it one time for every test or only when first test start? Can i create it in explicit way?<br/>
  c) how can this db be deleted?<br/></p>
  
  <p>2) What's i have to test?<br/>
  a) in my past application i made many error in mapping object property to db column so this seem a thing to test: maybe i can create an object, save it to db, read it from db and test that all property read is equal to what i wrote<br/>
  b) i think i will use repository pattern, so every object will have own repository with CRUD operations, i have to test this CRUD or it means iam testing EntityFramework itself?<br/>
  c) after some times i will have many list method in my repository so i will have to insert some fake data in my db and try to query against them to test my method?<br/>
  d) other things to test?</p>
  
  <p>Thanks</p>
  
  <p>EDIT:
  i tiny example:</p>
  
  <p>if i map my class:</p>
  
  <pre><code>MyClass{
      public int MyClassIdField
      public string MyClassDescriptionField
  }
  </code></pre>
  
  <p>to table [T01_Table1] with field <br />
  int_T01_Table1Id <br />
  nvc_T01_Table1Description <br /></p>
  
  <p>i want to define a test for this mapping both for testing correctness and from the point of view of test driven development</p>
  
  <p><strong>Other question:</strong> when iam testing inserting operation in db i can use my domain layer object directly or i have to mock them? <br />
  without mock i have to use the object constructor that could not work</p>
  
  <p><strong>EDIT 2:</strong>
  in meantime iam using a local db to go on and find other issue:
  i create a class in this way:</p>
  
  <pre><code>var fixture = new Fixture();
  MyClass c = New MyClass();
  c.id = fixture.Create&lt;int&gt;();
  c.endDate= fixture.Create&lt;DateTime?&gt;();
  context.MyClass.Add(c);
  context.SaveChanges();
  
  context.Dispose(); // TO force EF query on DB
  context= new MyContext();
  
  MyClass actual = context.MyClass.Find(c.Id);
  
  //Assert
  actual.DataFine.Value.Should().Be(target.DataFine.Value);
  </code></pre>
  
  <p>but test fail with this error: <br />
  Failure: Expected date and time to be &lt;2014-02-22 16:14:53.901>, but found &lt;2014-02-22 16:14:53.900>.</p>
  
  <p>if don't dispose context the test in successful.
  how can the data change?</p>