EF 5 + SQL CE 4:如何指定数据库文件自定义位置? [英] EF 5 + SQL CE 4: How to specify custom location for database file?

查看:151
本文介绍了EF 5 + SQL CE 4:如何指定数据库文件自定义位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,需要一个小的本地数据库中的客户端系统。
我想避免安装了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆