简体   繁体   English

从实体框架上下文获取SQL Server Compact文件的路径?

[英]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.

相关问题 在Entity Framework和SQL Server Compact Edition数据库中获取错误 - get Error in Entity Framework and SQL Server Compact edition database 没有配置文件的实体框架6 SQL Server Compact 3.5 - Entity Framework 6 SQL Server Compact 3.5 without Config file 具有实体框架的SQL Server Compact Edition - SQL Server Compact Edition with Entity Framework 将Entity Framework 6与SQL Server Compact 4一起使用 - Using Entity Framework 6 with SQL Server Compact 4 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? .NET Compact Framework-SQL Server Compact或平面文件作为备份 - .NET Compact Framework - SQL Server Compact or Flat File as backup 如何从 SQL 服务器紧凑型 C# 在 Entity Framework 中更快地读取数据 - How to read data faster in Entity Framework from SQL Server Compact C# 使用Entity Framework Async方法和SQL Server Compact阻止行为 - Blocking behaviour with Entity Framework Async methods and SQL Server Compact 将SQL Server Compact 4.0.0.1与Entity Framework 4.3一起使用 - Using SQL Server Compact 4.0.0.1 with Entity Framework 4.3 在带有实体框架 6.4 的 SQL 服务器紧凑型中无法使用 IDENTITY_INSERT - Not working IDENTITY_INSERT in SQL Server Compact with Entity Framework 6.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM