title: Error initializing entity framework database sequence contains no matching elements
body: <p>I've been staring at this for the last 5 hours, trying a bunch of things, but I'm not getting anywhere...</p>
  
  <p>When I try to start my MVC website who's database I've created using Entity Framework Code First, the database fails to initialize, throwing the following exception message:  </p>
  
  <blockquote>
    <p>Sequence contains no matching elements</p>
  </blockquote>
  
  <p>The first line of the stack trace below executes a Linq .Single query so I'm under the impression that it was looking for a single item in my model which doesn't exist. If this is correct, how do I find out what that item is?</p>
  
  <p>Additionally, having reached my wits-end and thus deciding to go through absolutely <strong>all</strong> debugging information after the exception is thrown, I noticed the following in my Autos window:</p>
  
  <p><img src="https://i.stack.imgur.com/nKzNz.png" alt="Error message from Autos window"></p>
  
  <p>This is in direct reference to my OnModelCreating override:</p>
  
  <pre><code>    protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Configurations.Add(new CompanyDBConfiguration());
          modelBuilder.Configurations.Add(new ClientDBConfiguration());
          modelBuilder.Configurations.Add(new QuoteDBConfiguration());
          modelBuilder.Configurations.Add(new RoleDBConfiguration());
          modelBuilder.Configurations.Add(new UserDBConfiguration());
          modelBuilder.Configurations.Add(new InvoiceDBConfiguration());
          modelBuilder.Configurations.Add(new JobDBConfiguration());
          modelBuilder.Configurations.Add(new JobStatusDBConfiguration());            
      }
  </code></pre>
  
  <p>Any information that can be used to get me closer to a solution will be greatly appreciated.</p>
  
  <p>I'm sorry if there isn't enough information here, I'm at a loss at what to provide. Here's the stack trace though: <strong>(scroll down for edits)</strong></p>
  
  <blockquote>
    <p>System.InvalidOperationException was unhandled by user code<br>
      HResult=-2146233079<br>
      Message=Sequence contains no matching element<br>
      Source=System.Core<br>
      StackTrace:<br>
           at System.Linq.Enumerable.Single[TSource](IEnumerable<code>1 source, Func</code>2 predicate)<br>
           at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.&lt;>c__DisplayClass1.b__0(Tuple<code>2 pm)
           at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable</code>1 ts, Action<code>1 action)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable</code>1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList<code>1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)<br>
           at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)<br>
           at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)<br>
           at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)<br>
           at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)<br>
           at System.Data.Entity.Internal.RetryLazy</code>2.GetValue(TInput input)<br>
           at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()<br>
           at System.Data.Entity.Internal.LazyInternalContext.MarkDatabaseInitialized()<br>
           at System.Data.Entity.Database.Initialize(Boolean force)<br>
           at Jobber.Web.MvcApplication.Application_Start() in e:\Development\Jobber\Jobber.Web\Global.asax.cs:line 28<br>
      InnerException: </p>
  </blockquote>
  
  <p><strong>EDIT 1</strong>  </p>
  
  <p>Here's the code for Global.asax</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
  
          WebApiConfig.Register(GlobalConfiguration.Configuration);
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
  
          WebDBContext db = new WebDBContext();
          db.Database.Initialize(true);
          db.Seed();
      }
  }
  </code></pre>
  
  <p>And here's <code>WebDBContext()</code></p>
  
  <pre><code>// BaseDBContext is a generic class that inherits from DbContext, see it's code below
  public class WebDBContext : BaseDBContext
  {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Configurations.Add(new CompanyDBContext());
          modelBuilder.Configurations.Add(new ClientDBContext());
          modelBuilder.Configurations.Add(new QuoteDBContext());
          modelBuilder.Configurations.Add(new RoleDBContext());
          modelBuilder.Configurations.Add(new UserDBContext());
          modelBuilder.Configurations.Add(new InvoiceDBContext());
          modelBuilder.Configurations.Add(new JobDBContext());
          modelBuilder.Configurations.Add(new JobStatusDBContext());            
      }
  
      public WebDBContext()
      {
          Database.SetInitializer&lt;WebDBContext&gt;(new DropCreateDatabaseAlways&lt;WebDBContext&gt;());
      }
  
      public void Seed()
      {
          // seeds initial data into the database
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Role&gt; Roles { get; set; }
      public DbSet&lt;Client&gt; Clients { get; set; }
      public DbSet&lt;Company&gt; Companies { get; set; }
      public DbSet&lt;Invoice&gt; Invoices { get; set; }
      public DbSet&lt;Job&gt; Jobs { get; set; }
      public DbSet&lt;JobStatus&gt; JobStatuses { get; set; }
      public DbSet&lt;Quote&gt; Quotes { get; set; }
  </code></pre>
  
  <p>BaseDBContext:<br>
  This class just provides a generic context that is not specific to any application. This allows for CRUD functionality to be written for objects which may be common between projects</p>
  
  <pre><code>public class BaseDBContext : DbContext
  {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Configurations.Add(new BaseUserDBContext());
      }
  
      public BaseDBContext()
          : base()
      {
          Database.SetInitializer&lt;BaseDBContext&gt;(new DropCreateDatabaseIfModelChanges&lt;BaseDBContext&gt;());
      }
  
      public DbSet&lt;BaseUser&gt; Users { get; set; }
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong><br>
  Chris requested the ConnectionString and My UserDBContext (more about this below - <em>VERY IMPORTANT STUFF</em>)</p>
  
  <pre><code>  &lt;connectionStrings&gt;
      &lt;add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Jobber.Web-20150105094927;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Jobber.Web-20150105094927.mdf" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>public class UserDBContext : DBBaseObject&lt;User&gt;
  {
      // application-specific columns
      public UserDBContext()
          : base()
      {
          //HasOptional(r =&gt; r.Role)
          //    .WithMany(m =&gt; m.Users)
          //   .Map(x =&gt; x.MapKey("fkRoleID"))
          //    .WillCascadeOnDelete(false);
  
          ToTable("Users");
      }
  }
  </code></pre>
  
  <p>To start explaining this... I'm developing an application framework that handles all "globally common" structures and methods which is to say that it handles all things that can logically be assumed to be common between applications.</p>
  
  <p><em>Things like a user object that contains a username and password for example.</em></p>
  
  <p>The framework is put together with a lot of inheritance, abstract classes and some use of generics. Here's more or less what it looks like without all that:</p>
  
  <p><strong>Framework:</strong>  </p>
  
  <pre><code>Logan.Base
  {
      Logan.Base.BaseObject  
      - defines common fields that all tables will always have (PK, auditing, etc)  
  
      Logan.Base.BaseUser  
      - defines common fields that user tables will have (username, password, etc)
  }  
  Logan.DBBase  
  {
      Logan.DBBase.BaseUserDBContext  
      - defines C#-&gt;SQL data type mappings for the BaseUser along with CRUD functions  
  
      Logan.DBBase.DBBaseObject  
      - defines C#-&gt;SQL data type mappings for columns that all tables will always have  
  
      Logan.DBBase.DBContext  
      - defines a DbContext to be used in the DBBaseObject
  }
  </code></pre>
  
  <p>Logan.Base</p>
  
  <pre><code>namespace Logan.Base
  {
      public abstract class BaseObject
      {
          public Int64 PKey { get; set; }
          public Int64 CreatedBy { get; set; }
          public DateTime CreatedOn { get; set; }
          public Int64 ModifiedBy { get; set; }
          public DateTime ModifiedOn { get; set; }
  
          public BaseObject()
          {
              PKey = 0;
              CreatedBy = 0;
              ModifiedBy = 0;
          }
      }
  
      public abstract class BaseUser : BaseObject
      {
          public string EmailAddress { get; set; }
          public string Password { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
  
          public BaseUser()
          {
              EmailAddress = String.Empty;
              Password = String.Empty;
              FirstName = String.Empty;
              LastName = String.Empty;
          }        
      }
  }
  </code></pre>
  
  <p>Logan.DBBase</p>
  
  <pre><code>namespace Logan.DBBase
  {
  
      public abstract class DBBaseObject&lt;T&gt; : EntityTypeConfiguration&lt;T&gt;
          where T : BaseObject
      {
  
          public DBBaseObject()
              : base()
          {
              HasKey(p =&gt; p.PKey);
          }
      }
  
      public class BaseUserDBContext : DBBaseObject&lt;BaseUser&gt;
      {
  
          // common columns
          public BaseUserDBContext()
              : base()
          {
              Property(p =&gt; p.EmailAddress)
                  .HasColumnName("sEmailAddress")
                  .HasMaxLength(200)
                  .IsRequired();
  
              Property(p =&gt; p.Password)
                  .HasColumnName("sPassword")
                  .HasMaxLength(255)
                  .IsRequired();
  
              Property(p =&gt; p.FirstName)
                  .HasColumnName("sFirstName")
                  .IsRequired();
  
              Property(p =&gt; p.LastName)
                  .HasColumnName("sLastName")
                  .IsRequired();
  
              ToTable("Users");
          }
  
          // User CRUD functions that use the below DbContext go here
      }
  
      public class BaseDBContext : DbContext
      {
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
  
              modelBuilder.Configurations.Add(new BaseUserDBContext());
          }
  
          public BaseDBContext()
              : base()
          {
              Database.SetInitializer&lt;BaseDBContext&gt;(new DropCreateDatabaseIfModelChanges&lt;BaseDBContext&gt;());
          }
  
          public DbSet&lt;BaseUser&gt; Users { get; set; }
      }
  }
  </code></pre>