[英]Get path to SQL Server Compact file from Entity Framework context?
I'm trying to programmatically backup my SQL Server CE database. 我正在尝试以编程方式备份我的SQL Server CE数据库。 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). 然后让我震惊:我在这里只是为了复制实体框架无论如何在内部所做的功能(更不用说用固定路径替换
|DataDirectory|
了)。
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
实体是我的上下文,但是仍然只能给我:
|DataDirectory|\\db.sdf
Question: Is there a proper way to get the actual path to the SQL Server CE file, with |DataDirectory|
问题:是否存在使用
|DataDirectory|
获取SQL Server CE文件的实际路径的正确方法|DataDirectory|
substituted? 替代?
Thanks! 谢谢!
You are faced with a bug in SQL Server Compact 4.0 SP1, the conn.Database property used to resolve the full path. 您在SQL Server Compact 4.0 SP1(用于解析完整路径的conn.Database属性)中遇到错误。 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
您可以将SqlCeReplication对象的SubscriberConnectionString用作解决方法-请在此处查看我的博客文章: http : //erikej.blogspot.dk/2013/02/sql-server-compact-code-snippet-of-week_19.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.