title: How to use entity framework (code first) with a dynamic created connection string and a single-file database?
body: <p>I'm using SqlServerCompact, Entity framework with code-first and I'm generating the connection string dinamically because the database it's generated when the user requests it.</p>
  
  <p>I followed suggestions from this question: <a href="https://stackoverflow.com/questions/6003085/how-do-i-programmatically-set-the-connection-string-for-entity-framework-code-fir">How do I programmatically set the connection string for Entity-Framework Code-First?</a> however, I still have problems, infact I get an exception complaining that the schema in the model is not the same as used in code.</p>
  
  <p>That's obvius however, I want the schema to be generated from code, so I really can't understand why this happen</p>
  
  <p>Can someone help me?Here is the code used:</p>
  
  <p><strong>NerdDinners.cs</strong></p>
  
  <pre><code>class NerdDinners : DbContext
  {
      public static string CreateConnectionString(string dbPath = @"|DataDirectory|\NerdDinners.sdf")
      {
          SqlCeConnectionStringBuilder sqlConnection = new SqlCeConnectionStringBuilder();
          sqlConnection.Password = "9023fase93";
          sqlConnection.DataSource = dbPath;
  
          EntityConnectionStringBuilder connection = new EntityConnectionStringBuilder();
          connection.Metadata = @"res://*/NerdDinnersModel.csdl|res://*/NerdDinnersModel.ssdl|res://*/NerdDinnersModel.msl";
          connection.Provider = "System.Data.SqlServerCe.3.5";
          connection.ProviderConnectionString = sqlConnection.ToString();
  
          return connection.ToString();
      }
  
      public NerdDinners() : base() { }
      public NerdDinners(string nameOrConnectionString) : base(nameOrConnectionString) { }
  
      public DbSet&lt;Dinner&gt; Dinners { get; set; }
      public DbSet&lt;RSVP&gt; RSVPs { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          Database.SetInitializer&lt;NerdDinners&gt;(new NerdDinnersInitializer());
          Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe");
  
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          NerdDinners nerdDinner = new NerdDinners(NerdDinners.CreateConnectionString());
          Console.WriteLine("Inizio test");
  
          var res = from d in nerdDinner.Dinners
                    select d;
  
          Console.WriteLine("Dinners:");
          foreach (var item in res)
              Console.WriteLine(item.ToString());
          Console.WriteLine("End");
  
          // Usato per terminazione
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>An this is my solution situation (an image):</p>
  
  <p><img src="https://i.stack.imgur.com/11tCs.png" alt="Solution rapresentation"></p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>The error is a MetadataException, it complains about invalid schema</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p><strong>NerdDinnersInitializer.cs</strong></p>
  
  <pre><code>class NerdDinnersInitializer : DropCreateDatabaseAlways&lt;NerdDinners&gt;
  {
      public NerdDinnersInitializer() : base() { }
  
      protected override void Seed(NerdDinners context)
      {
          var toadd = new List&lt;Dinner&gt;
          {
              new Dinner
              {
                  Address = "bla",
                  DinnerID = 0,
                  EventDate = DateTime.Parse("2010-12-03"),
                  HostedBy = "mclink",
                  Title = "Notitle",
              },
              new Dinner
              {
                  Address = "Fiwe",
                  DinnerID = 0,
                  EventDate = DateTime.Parse("2011-12-03"),
                  HostedBy = "ngi",
                  Title = "Bah",
              },
          };
  
          toadd.ForEach(item =&gt; context.Dinners.Add(item));
  
          context.SaveChanges();
  
          base.Seed(context);
      }
  }
  </code></pre>