0

I'm trying to programmatically backup my SQL Server CE database. Apparently, this should simply involve copying the database file.

My first solution looked like this:

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

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 |DataDirectory| with a fixed path).

Then I tried this:

using (var conn = entities.Database.Connection)
{
    // conn.Open();
    var path = conn.Database;
}

where entities is my context but that still only gives me: |DataDirectory|\db.sdf

Question: Is there a proper way to get the actual path to the SQL Server CE file, with |DataDirectory| substituted?

Thanks!

1 Answer 1

1

You are faced with a bug in SQL Server Compact 4.0 SP1, the conn.Database property used to resolve the full path. You can use the SubscriberConnectionString of the SqlCeReplication object as a workaround - see my blog post here: http://erikej.blogspot.dk/2013/02/sql-server-compact-code-snippet-of-week_19.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.