title: entity framework looking in different table during unittests
body: <p>I have a single DbContext in a library along with entities and their configuration in fluent api.</p>
  
  <p>I use this same context in two different projects.
  The first one is a test project and the second one a simple console application.</p>
  
  <p>I execute a query including some of it's relations like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var vacancies = (from v in ctx.Vacancies
                                .Include("Categories")
                                .Include("Levels")
                                .Include("Contracts")
                                .Include("JobTypes"));
  </code></pre>
  
  <p>Strangely enough the console application works perfectly, however the unittest starts looking in the wrong tables all of a sudden.</p>
  
  <p>Both applications produce very different queries when i look at them.</p>
  
  <p>I'm using EF5 atm and I've been stuck on this for a few days now with not a single clue to what might cause this problem.</p>
  
  <p>EDIT:
  The point is i don't do any different queries, i also only have on context and only one connectionstring which i have confirmed during runtime connects to the right database.</p>
  
  <p>All i can say is that i feel like the fluent api has no effect in unittests even though it hits the code just fine like it should.</p>
  
  <pre><code> public class SchedulerContext : BaseContext&lt;SchedulerContext&gt;{
  
  
      public DbSet&lt;Click&gt; Clicks { get; set; }
      public DbSet&lt;Category&gt; Categories { get; set; }
      public DbSet&lt;ContractType&gt; Contracts { get; set; }
      public DbSet&lt;JobType&gt; JobTypes { get; set; }
      public DbSet&lt;Level&gt; Levels { get; set; }
      public DbSet&lt;ExternalMapping&gt; ExternalMappings { get; set; }     
      public DbSet&lt;GeoName&gt; GeoNames { get; set; }
      public DbSet&lt;GeoAlternateName&gt; GeoAlternateNames { get; set; }     
      public DbSet&lt;Trigger&gt; JobTriggers { get; set; }
      public DbSet&lt;RegistryValue&gt; RegistryValues { get; set; }
      public DbSet&lt;JobResult&gt; JobResults { get; set; }
      public DbSet&lt;JobInfo&gt; JobInfo { get; set; }
      public DbSet&lt;JobError&gt; JobErrors { get; set; }
      public DbSet&lt;JobNotification&gt; JobNotifications { get; set; }
      public DbSet&lt;Company&gt; Customers { get; set; }
      public DbSet&lt;Vacancy&gt; Vacancies { get; set; }
  
      public SchedulerContext() {
          //this.Configuration.ProxyCreationEnabled = false;
          //this.Configuration.LazyLoadingEnabled = false;
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder) {
  
          modelBuilder.Configurations.Add(new VacancyEntityTypeConfiguration());
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  
  <p>That is my context.</p>
  
  <p>And my entity configuration:</p>
  
  <pre><code>public class VacancyEntityTypeConfiguration : EntityTypeConfiguration&lt;Vacancy&gt; {
  
      public VacancyEntityTypeConfiguration() {
          this.HasMany(c =&gt; c.Levels).
         WithMany().
         Map(
          m =&gt; {
              m.ToTable("VacancyLevels");
          });
  
          this.HasMany(c =&gt; c.Categories).
            WithMany().
            Map(
             m =&gt; {
                 m.ToTable("VacancyCategories");
             });
  
          this.HasMany(c =&gt; c.Contracts).
            WithMany().
            Map(
             m =&gt; {
                 m.ToTable("VacancyContractTypes");
             });
  
          this.HasMany(c =&gt; c.Jobtypes).
            WithMany().
            Map(
             m =&gt; {
                 m.ToTable("VacancyJobTypes");
             });
      }
  }
  </code></pre>
  
  <p>And the exact method which shows the problem.</p>
  
  <pre><code> public List&lt;Vacancy&gt; GetVacanciesForCustomer(Company customer, bool onlineInclusive) {
          using (var ctx = new JHSchedulerContext.SchedulerContext()) {
              var vacancies = (from v in ctx.Vacancies.Include("Categories").Include("Levels").Include("Contracts").Include("JobTypes") where v.SourceSite == customer.SourceSite &amp;&amp; (v.Online || onlineInclusive == false) select v).ToList();
              return vacancies.ToList();
          }
      }
  </code></pre>
  
  <p>And the Vacancy class:</p>
  
  <pre><code> public class Vacancy {
  
      public Vacancy()
      {
          this.Language = "";
      }
  
      [SolrUniqueKey("id")]
      [Key]
      [Required]
      public int Id { get; set; }
  
      [StringLength(50, ErrorMessage = "Maximum length 50 characters")]
      public string ExternalId { get; set; }
  
      [SolrField("title")]
      [StringLength(1024, ErrorMessage = "Maximum length 1024 characters")]
      public string Title { get; set; }
  
      [SolrField("company")]
      [StringLength(1024, ErrorMessage = "Maximum length 1024 characters")]
      public string Company { get; set; }
  
      [StringLength(4096, ErrorMessage = "Maximum length 4096 characters")]
      public string CompanyText { get; set; }
  
      [SolrField("location")]
      [StringLength(1024, ErrorMessage = "Maximum length 1024 characters")]
      public string Location { get; set; }
  
      [SolrField("url")]
      [StringLength(1024, ErrorMessage = "Maximum length 1024 characters")]
      public string Url { get; set;}
  
      [SolrField("source")]
      [StringLength(1024, ErrorMessage = "Maximum length 1024 characters")]
      public string SourceSite { get; set; }
  
      private string text;
      [SolrField("text")]
      [StringLength(4096, ErrorMessage = "Maximum length 4096 characters")]
      public string Text {
          get {
              return StringHelper.TruncateAtWord(text, 500);
              //return new String(text.Take(500).ToArray());
              //return text.Substring(text.Length &lt; 900 ? 0 : (text.Length / 2), (int)Math.Min(text.Length, 500));
          }
          set{
              text = value;
          }
      }
  
      [StringLength(4096, ErrorMessage = "Maximum length 4096 characters")]
      public string TextProfile { get; set; }
  
      [StringLength(4096, ErrorMessage = "Maximum length 4096 characters")]
      public string TextOffer { get; set; }
  
      [NotMapped]
      [ScriptIgnore]
      public SqlGeography Coordinate {
          get
          {
              if (Latitude.HasValue &amp;&amp; Longitude.HasValue)
                  return SqlGeography.Point(Latitude.Value, Longitude.Value, 4326);
              else
                  return null;
          }
      }
  
      [SolrField("coordinate_0_coordinate")]
      public double? Latitude { get; set; }
  
      [SolrField("coordinate_1_coordinate")]
      public double? Longitude { get; set; }
  
      [StringLength(1024, ErrorMessage = "Maximum length 1024 characters")]
      public string NormalizedLocation { get; set; }
  
      public DateTime? ScrapedDate { get; set; }
  
      [SolrField("insertdate")]
      public DateTime? ImportDate { get; set; }
  
      public DateTime? ExpireDate { get; set; }     
  
      [NotMapped]
      public string DaysAgo {
          get {
              if (ImportDate != null)
                  return (DateTime.Now - (DateTime)ImportDate).Days.ToString();
              else {
                  return "N/A";
              }
          }
      }
  
      [SolrField("category")]
      public ICollection&lt;string&gt; CategoryNames {
          get { return Categories != null ? Categories.Select(c =&gt; c.Name).ToList() : null; }
      }
  
      [SolrField("level")]
      public ICollection&lt;string&gt; LevelNames {
          get { return Levels != null ? Levels.Select(l =&gt; l.Name).ToList() : null; }
      }
  
      [SolrField("contract")]
      public ICollection&lt;string&gt; ContractNames {
          get { return Contracts != null ? Contracts.Select(c =&gt; c.Name).ToList() : null; }
      }
  
      [SolrField("time")]
      public ICollection&lt;string&gt; JobTypeNames {
          get { return Jobtypes != null ? Jobtypes.Select(c =&gt; c.Name).ToList() : null; }
      }
  
      public string ContactName { get; set; }       
  
      [Required]
      public bool Online { get; set; }
  
      [NotMapped]
      public int? Distance { get; set; }
  
      public string Language { get; set; }
  
      public string PriorityType { get; set; }
  
      public int NumClicks { get; set; }
  
      [NotMapped]
      public string GridContactName {
          get{
              return string.Format("{0} (Total Clicks:{1})",ContactName,NumClicks);
          }
      }
  
      public virtual ICollection&lt;Level&gt; Levels { get; set; }
  
      public virtual ICollection&lt;Category&gt; Categories { get; set; }
  
      public virtual ICollection&lt;ContractType&gt; Contracts { get; set; }
  
      public virtual ICollection&lt;JobType&gt; Jobtypes { get; set; }     
  
      public override string ToString() {
          return string.Format("Id={0} ExternalId={1} Title={2} Company={3} Location={4} Url={5} SourceSite={6} Text={7} NormalizedLocation={8} InsertDate={9} ImportDate={10} DaysAgo={11}",Id, ExternalId,Title,Company,Location,Url, SourceSite,Text,NormalizedLocation,ScrapedDate,ImportDate,DaysAgo);
      }
  
  }
  </code></pre>
  
  <p>And the Level class(all the other relation classes have the same structure(name,id):</p>
  
  <pre><code>   public class Level {
  
      [Key]
      [Required]
      public int Id { get; set; }
  
      [Required]
      [StringLength(100, ErrorMessage = "Maximum length 100 characters")]
      public string Name { get; set; }      
  
      public override string ToString() {
          return string.Format("Id={0} Name={1}", Id, Name);
      }
  
  }
  </code></pre>
  
  <p>As far as connection to database goes you'll just have to believe me that i've checked both are connecting to the same database.(I even generated a new database with migrations enabled on this one since we use another context for migrations. That still had the exact same issue).</p>
  
  <p>To clarify:
  The unittest looks for table: CategoryVacancies.
  The console application or even my windows service i use this in look for the right table: VacancyCategories.</p>