title: EF Code First - Invalid column name
body: <p>Im getting an error "Invalid column name 'FeeLevel_LevelId' which makes absolutely no sense considering all properties are simple types and there is no FeeLevel nor a LevelId object in this object.</p>
  
  <p>So my context is:</p>
  
  <pre><code>  public partial class FeesDbContext : DisconnectedEntityContext
      {
  
          public DbSet&lt;Currency&gt; Currencies { get; set; }
  
          public DbSet&lt;FeeLevel&gt; FeeLevels { get; set; }
  
          public DbSet&lt;FeeLevelDetail&gt; FeeLevelDetails { get; set; }
  
          public DbSet&lt;FeeType&gt; FeeTypes { get; set; }
  
          public DbSet&lt;MemberFeeDiscountLevel&gt; MemberFeeDiscountLevels { get; set; }
  
          public FeesDbContext()
              : base("FeesDb") { }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Configurations.Add(new FeeLevelMap());
              modelBuilder.Configurations.Add(new FeeLevelDetailMap());
              modelBuilder.Configurations.Add(new FeeTypeMap());
              modelBuilder.Configurations.Add(new CurrencyMap());
              modelBuilder.Configurations.Add(new MemberFeeDiscountLevelMap());
          }
  
          public static void ApplyChanges&lt;TEntity&gt;(TEntity root)
                      where TEntity : class, IObjectWithState
          {
              using (var context = new FeesDbContext())
              {
                  context.Set&lt;TEntity&gt;().Add(root);
                  foreach (var entry in context.ChangeTracker.Entries&lt;IObjectWithState&gt;())
                  {
                      IObjectWithState stateInfo = entry.Entity;
                      entry.State = ConvertState(stateInfo.State);
                  }
                  context.SaveChanges();
              }
          }
  }
  </code></pre>
  
  <p>The POCO is:</p>
  
  <pre><code>public partial class MemberFeeDiscountLevel : AbstractState
      {
  
          public long MemberFeeDiscountLevelId { get; set; }
          public System.Guid MemberId { get; set; }
          public short MemberAsType { get; set; }
          public long FeeDiscountLevelId { get; set; }
          public System.DateTime FeeDiscountLevelAppliedDate { get; set; }
          public Nullable&lt;System.DateTime&gt; FeeDiscountLevelExpiresDate { get; set; }
          public Nullable&lt;long&gt; FallbackFeeDiscountLevelId { get; set; }
          public System.Guid UserId { get; set; }
          public System.DateTime LastModified { get; set; }
  
  
          public MemberFeeDiscountLevel(ObjectState state) : base(state) { }
  
          public MemberFeeDiscountLevel()
          {
          }
      }
  </code></pre>
  
  <p>It's mapping is:</p>
  
  <pre><code>  public class MemberFeeDiscountLevelMap : EntityTypeConfiguration&lt;MemberFeeDiscountLevel&gt;
      {
          public MemberFeeDiscountLevelMap()
          {
              // Primary Key
              this.HasKey(t =&gt; t.MemberFeeDiscountLevelId);
  
              this.Ignore(t =&gt; t.State);
  
              // Properties
              // Table &amp; Column Mappings
              this.ToTable("MemberFeeDiscountLevel");
              this.Property(t =&gt; t.MemberFeeDiscountLevelId).HasColumnName("MemberFeeDiscountLevelId");
              this.Property(t =&gt; t.MemberId).HasColumnName("MemberId");
              this.Property(t =&gt; t.MemberAsType).HasColumnName("MemberAsType");
              this.Property(t =&gt; t.FeeDiscountLevelId).HasColumnName("FeeDiscountLevelId");
              this.Property(t =&gt; t.FeeDiscountLevelAppliedDate).HasColumnName("FeeDiscountLevelAppliedDate");
              this.Property(t =&gt; t.FeeDiscountLevelExpiresDate).HasColumnName("FeeDiscountLevelExpiresDate");
              this.Property(t =&gt; t.FallbackFeeDiscountLevelId).HasColumnName("FallbackFeeDiscountLevelId");
              this.Property(t =&gt; t.UserId).HasColumnName("UserId");
              this.Property(t =&gt; t.LastModified).HasColumnName("LastModified");
          }
      }
  </code></pre>
  
  <p>The database table is:</p>
  
  <p><img src="https://i.stack.imgur.com/shEjK.gif" alt="enter image description here"></p>
  
  <p>and it has not relationships. Yet EF is generating the following SQL:</p>
  
  <pre><code>exec sp_executesql N'INSERT [dbo].[MemberFeeDiscountLevel]([MemberId], [MemberAsType], [FeeDiscountLevelId], [FeeDiscountLevelAppliedDate], [FeeDiscountLevelExpiresDate], [FallbackFeeDiscountLevelId], [UserId], [LastModified], [FeeLevel_LevelId])
  VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, NULL)
  SELECT [MemberFeeDiscountLevelId]
  FROM [dbo].[MemberFeeDiscountLevel]
  WHERE @@ROWCOUNT &gt; 0 AND [MemberFeeDiscountLevelId] = scope_identity()',N'@0 uniqueidentifier,@1 smallint,@2 bigint,@3 datetime2(7),@4 datetime2(7),@5 bigint,@6 uniqueidentifier,@7 datetime2(7),@8 int',@0='DAF771D1-079F-4743-B5C7-FD0FA1C63E19',@1=0,@2=1012,@3='2014-01-24 12:05:36.0608347',@4='2014-02-01 00:00:00',@5=1018,@6='EEDF2C83-2123-4B1C-BF8D-BE2D2FA26D09',@7='2014-01-24 12:05:36.0608347'
  go
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Creating a new Fees2DbContext stripping out the other DbSets "fixes" the problem.... but I dont know why... none of these classes / sets are related to the class in question.</p>
  
  <pre><code> public partial class Fees2DbContext : DisconnectedEntityContext
      {
          public DbSet&lt;MemberFeeDiscountLevel&gt; MemberFeeDiscountLevels { get; set; }
  
          public Fees2DbContext()
              : base("FeesDb") { }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Configurations.Add(new MemberFeeDiscountLevelMap());
          }
  
          public static void ApplyChanges&lt;TEntity&gt;(TEntity root)
                      where TEntity : class, IObjectWithState
          {
              using (var context = new Fees2DbContext())
              {
                  context.Set&lt;TEntity&gt;().Add(root);
                  foreach (var entry in context.ChangeTracker.Entries&lt;IObjectWithState&gt;())
                  {
                      IObjectWithState stateInfo = entry.Entity;
                      entry.State = ConvertState(stateInfo.State);
                  }
                  context.SaveChanges();
              }
          }
      }
  </code></pre>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <pre><code>       public partial class FeeLevel : AbstractState
          {
              public FeeLevel()
              {
                  this.FeeLevelDetails = new List&lt;FeeLevelDetail&gt;();
                  this.MemberFeeDiscountLevels = new List&lt;MemberFeeDiscountLevel&gt;();
              }
  
              public long LevelId { get; set; }
  
              public string LevelName { get; set; }
  
              public int CurrencyId { get; set; }
  
              public System.DateTime LastModified { get; set; }
  
              public bool IsSystemOwned { get; set; }
  
              public System.Guid UserId { get; set; }
  
              public virtual Currency Currency { get; set; }
  
              [ScriptIgnore]
              public virtual ICollection&lt;FeeLevelDetail&gt; FeeLevelDetails { get; set; }
  
              public virtual ICollection&lt;MemberFeeDiscountLevel&gt; MemberFeeDiscountLevels { get; set; }
          }
  
  public class FeeLevelMap : EntityTypeConfiguration&lt;FeeLevel&gt;
      {
          public FeeLevelMap()
          {
              // Primary Key
              this.HasKey(t =&gt; t.LevelId);
  
              this.Ignore(t =&gt; t.State);
  
              // Properties
              this.Property(t =&gt; t.LevelId);
              // .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
              this.Property(t =&gt; t.LevelName)
                  .IsRequired()
                  .HasMaxLength(50);
  
              // Table &amp; Column Mappings
              this.ToTable("FeeLevel");
              this.Property(t =&gt; t.LevelId).HasColumnName("LevelId");
              this.Property(t =&gt; t.LevelName).HasColumnName("LevelName");
              this.Property(t =&gt; t.CurrencyId).HasColumnName("CurrencyId");
              this.Property(t =&gt; t.LastModified).HasColumnName("LastModified");
              this.Property(t =&gt; t.UserId).HasColumnName("UserId");
  
              // Relationships
              this.HasRequired(t =&gt; t.Currency)
                  .WithMany(t =&gt; t.FeeLevels)
                  .HasForeignKey(d =&gt; d.CurrencyId);
  
          }
      }
  </code></pre>