EF 5 + SQL CE 4:如何指定数据库文件自定义位置? [英] EF 5 + SQL CE 4: How to specify custom location for database file?
问题描述
我开发,需要一个小的本地数据库中的客户端系统。
我想避免安装了SQL Express,并已决定去与SQLCE 4。
我用的EntityFramework 5的数据访问和创建我的自定义背景。
一切工作的发展得很好,我可以使用的app.config要么设置特定文件的位置是动态的。数据来源= | DataDirectory目录| \MyDatabase.sdf
但在部署我希望数据库位于用户的文档文件夹:\My Documents\ApplicationName\MyDatabase.sdf
我该怎么办呢?
我需要的其实是可以设置自定义的连接字符串中的代码!
这是我试过到目前为止:
私人MyApplicationDataContext(字符串的connectionString)
:底座(的connectionString)
{
}
公共静态MyApplicationDataContext的CreateInstance()
{
var目录= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
VAR路径= Path.Combine(目录,@ApplicationName\MyDatabase.sdf);
// VAR的connectionString =的String.Format(供应商= System.Data.SqlServerCe.4.0;提供连接字符串= \数据源= {0} \,路径);
变种的connectionString =的String.Format(数据源= {0},通路);
返回新MyApplicationDataContext(的connectionString);
}
正如你可以看到我试过2种连接字符串,但既造成异常。
不支持关键字:供应商
块引用>
和
提供者未返回ProviderManifestToken字符串。
块引用>
解决方案嗯,我终于得到了它的权利!
我包括调整后的代码,如果别人有同样的问题。
诀窍是设置连接字符串的Database.DefaultConnectionFactory私人MyApplicationDataContext()
{ }
公共静态MyApplicationDataContext的CreateInstance()
{
var目录= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
VAR路径= Path.Combine(目录,@ApplicationName\MyDatabase.sdf);
//设置连接字符串
变种的connectionString =的String.Format(数据源= {0},路径);
Database.DefaultConnectionFactory =新SqlCeConnectionFactory(System.Data.SqlServerCe.4.0,,的connectionString);
返回新MyApplicationDataContext();
}
I am developing a client system that needs a small local database. I want to avoid installation of SQL Express and have decided to go with SqlCe 4.
I use EntityFramework 5 for data access and have created my custom context. Everything works fine in development where I can use app.config to either set specific file location or dynamic "Data Source=|DataDirectory|\MyDatabase.sdf".
But on deploy I want the database to be located in the users documents folder: \My Documents\ApplicationName\MyDatabase.sdf
How can I do that?
All I need is actually to be able to set custom connection string in code!
This is what I tried so far:
private MyApplicationDataContext(string connectionString) : base(connectionString) { } public static MyApplicationDataContext CreateInstance() { var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var path = Path.Combine(directory, @"ApplicationName\MyDatabase.sdf"); //var connectionString = string.Format("provider=System.Data.SqlServerCe.4.0;provider connection string=\"Data Source={0}\"", path); var connectionString = string.Format("Data Source={0}", path); return new MyApplicationDataContext(connectionString); }
As you can see I tried two kinds of connection strings but both caused exceptions.
Keyword not supported: 'provider'.
and
The provider did not return a ProviderManifestToken string.
解决方案Ah, I finally got it right!
I include the adjusted code if someone else has the same problem. The trick was to set the connection string on the Database.DefaultConnectionFactory
private MyApplicationDataContext() { } public static MyApplicationDataContext CreateInstance() { var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var path = Path.Combine(directory, @"ApplicationName\MyDatabase.sdf"); // Set connection string var connectionString = string.Format("Data Source={0}", path); Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "", connectionString); return new MyApplicationDataContext(); }
这篇关于EF 5 + SQL CE 4:如何指定数据库文件自定义位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!