title: C# EntityFramework Core - Invalid object name, cannot update database
body: <ol>
  <li>Added MDF database to project called FxTrader.mdf</li>
  <li><p>Created simple Model:</p>
  
  <pre><code>public class MarketTickContext : DbContext
  
  {
  
  
  public DbSet&lt;MarketTickRecord&gt; Records { get; set; }
  
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
      string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = ""C:\Users\asmodat\Documents\Visual Studio 2015\Projects\Asmodat FxTrader\Asmodat FxTrader Data\FxTrader.mdf""; Integrated Security = True";
      optionsBuilder.UseSqlServer(connectionString);
  
  }
  
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;MarketTickRecord&gt;()
          .HasKey(mtr =&gt; new { mtr.Company, mtr.Symbol });
  
      modelBuilder.Entity&lt;MarketTickRecord&gt;()
          .HasMany(mtr =&gt; mtr.Ticks);
  
      modelBuilder.Entity&lt;MarketTick&gt;()
          .HasKey(mt =&gt; mt.Time);
  }
  }
  </code></pre></li>
  <li><p>Run in console: Add-Migration MarketTickMigration.v3 -> success</p></li>
  <li>Then Update-Database -> success</li>
  </ol>
  
  <p>But first. Database after this operation does not look like I expected.
  There should be 2 tables in my opinion: MarketTickRecord and MarketTick table</p>
  
  <p><a href="https://i.stack.imgur.com/doP3W.png" rel="nofollow noreferrer">But only some wierd table appears, that looks like this.</a></p>
  
  <p>So when I run this code:</p>
  
  <pre><code>_context = new MarketTickContext();
  _context.Records.Add(new MarketTickRecord());
  _context.SaveChanges();
  </code></pre>
  
  <p>The last line generates exception:</p>
  
  <pre><code>An unhandled exception of type 'Microsoft.EntityFrameworkCore.DbUpdateException' occurred in Microsoft.EntityFrameworkCore.dll
  
  Additional information: An error occurred while updating the entries. See the inner exception for details.
  
  {"Invalid object name 'Records'."}
  </code></pre>
  
  <p>same goes if I try: </p>
  
  <pre><code>_context.Records.Count()
  </code></pre>
  
  <p>Can anybody point me to direction what am I doing wrong ?
  How can I update my database so it works ?</p>
  
  <p>EDIT ############################### -> Progress</p>
  
  <p>I am slowly figuring out what is going on, so I created the simplest possible model to step by step check what is wrong.</p>
  
  <pre><code>public class TestContext : DbContext
      {
          public DbSet&lt;TestRecord&gt; Tests { get; set; }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Asmodat_FxTrader_Data.MarketTicksDb.v14;Trusted_Connection=True;");
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;TestRecord&gt;()
                  .HasKey(c =&gt; new { c.SomeK1, c.SomeK2 });
          }
      }
  </code></pre>
  
  <p>Above model is working great. So problem appears after creating new 'Test' class that will be placed inside list in TestRecord class, for example:</p>
  
  <pre><code> public class TestRecord
      {
  
          public string SomeK1 { get; set; }
  
          public string SomeK2 { get; set; }
  
          public List&lt;Test&gt; Tests { get; set; }
      }
  
  public class Test
      {
  
          public long SomeK1 { get; set; }
  
          public int V1 { get; set; }
  
          public int V2 { get; set; }
      }
  </code></pre>
  
  <p>If I now update OnModelCreating to this:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;TestRecord&gt;()
                  .HasKey(c =&gt; new { c.SomeK1, c.SomeK2 });
  
              modelBuilder.Entity&lt;Test&gt;()
                  .HasKey(c =&gt; c.SomeK1);
          }
  </code></pre>
  
  <p>The same exception appears as in original question....</p>
  
  <p>So problem, is somewhere here, and has to do with this List object or something missing in OnModelCreating. Any ideas ?</p>
  
  <p>Edit ########################################## </p>
  
  <p>I also tried this, as suggested in the comments:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              var tr = modelBuilder.Entity&lt;TestRecord&gt;();
                  tr.HasKey(c =&gt; new { c.SomeK1, c.SomeK2 });
                  tr.HasMany(c =&gt; c.Tests);
                  tr.ToTable("Records");
  
              var t = modelBuilder.Entity&lt;Test&gt;();
                  t.HasKey(c =&gt; c.SomeK1);
                  t.ToTable("Tests");
          }
  </code></pre>
  
  <p>still the same problem remains only name inside error changes while tying to execute </p>
  
  <pre><code>TestContext con = new TestContext();
              con.Tests.Add(new TestRecord() { SomeK1 = "a1", SomeK2 = "a2", Tests = new List&lt;Test&gt;() });
              con.SaveChanges();
  </code></pre>
  
  <p>Edit ##########################################</p>
  
  <p>What I need is to have Many TestRecord's that can have many Test's inside them stored in list (One to Many). </p>
  
  <p>But what is also required is that, lets say TestRecord with SomeK = {'a','b'}, and TestRecord with SomeK = {'c', 'd'}, might have Test's that have the same id, but are a different object (for example different V1 and V2).</p>
  
  <p>For example (pseudocode): </p>
  
  <pre><code>TestRecord['a','b'].Tests[1].v1 == 1
  TestRecord['c','d'].Tests[1].v1 == 2
  </code></pre>
  
  <p>Is it possible ? does anyone knows how to achieve this with ef core flex API ? Because all my tries are failing. </p>