title: Get path to SQL Server Compact file from Entity Framework context?
body: <p>I'm trying to programmatically backup my SQL Server CE database. Apparently, this should simply involve copying the database file.</p>
  
  <p>My first solution looked like this:</p>
  
  <pre><code>var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString;
  var efBuilder = new System.Data.EntityClient.EntityConnectionStringBuilder(connectionString);
  var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(efBuilder.ProviderConnectionString);
  
  var src = builder.DataSource.Replace("|DataDirectory|", System.AppDomain.CurrentDomain.BaseDirectory);
  var dest = @"C:\temp\backup.sdf";
  
  // file-copy src to dest here
  </code></pre>
  
  <p>Then it struck me: I'm jumping through hoops here just to duplicate functionality that the Entity Framework is doing internally anyway (not to mention substituting <code>|DataDirectory|</code> with a fixed path).</p>
  
  <p>Then I tried this:</p>
  
  <pre><code>using (var conn = entities.Database.Connection)
  {
      // conn.Open();
      var path = conn.Database;
  }
  </code></pre>
  
  <p>where entities is my context but that still only gives me: <code>|DataDirectory|\db.sdf</code></p>
  
  <p>Question: Is there a proper way to get the actual path to the SQL Server CE file, with <code>|DataDirectory|</code> substituted?</p>
  
  <p>Thanks!</p>