title: unit test c# with db context
body: <p>I have some functions I wrote in my project that return true\false and I'm trying to test them with unit test.</p>
  
  <pre><code> public class DbContextDal : DbContext
  {
  
      public DbContextDal() : base("BdContextDal_ConnectionString_appConfig3")
      {
          Configuration.LazyLoadingEnabled = true;
      }
  
      public virtual DbSet&lt;User&gt; users { get; set; }
      public virtual DbSet&lt;Student&gt; students { get; set; }
      public virtual DbSet&lt;Course&gt; courses { get; set; }
      public virtual DbSet&lt;Enrollment&gt; Enrollments { get; set; }
     }
  </code></pre>
  
  <p>as you can see DbContextCal is the connecting to data base and I have some functions that returns values.
  here I'm trying to test it with class I did for unit test</p>
  
  <pre><code>public class Student
  {
      // variables of this student..
  public float getGradeInCourse(Course c)
      {
          if (s == null || c == null) return -1f;
  
          float grade = -1f;
          DbContextDal dal = new DbContextDal();
          Enrollment e = dal.Enrollments.Find(s.ID, c.ID);
          if (e != null)
              grade = e.Grade;
          return grade;
      }
  }
  </code></pre>
  
  <p>I want to test this function getGradeInCourse and I did unit test for that:</p>
  
  <pre><code>[TestClass()]
  public class StudentTests
  {
      [TestMethod()]
      public void getGradeInCourseTest()
      {
          DbContextDal dal = new DbContextDal();
          Student s = dal.students.Find(100);
          Course c = dal.courses.Find(900);
  
          Assert.AreEqual(s.getGradeInCourse(c), 76);
      }
  } 
  </code></pre>
  
  <p>I'm getting this error:</p>
  
  <pre><code>   An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
  
  Additional information: Migrations is enabled for context 'DbContextDal' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.
  </code></pre>
  
  <p>in this DBcontext, i use almost in each function in my project.
  I can't use in Mocks because I use in this object Dbcontext many times, for example in the function I tried to test above.</p>