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
Get path to SQL Server Compact file from Entity Framework context?
-
21-04-2022 - |
سؤال
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!
المحلول
لا تنتمي إلى StackOverflow