如何使用动态连接字符串为SQL Server CE? [英] How to use dynamic connection string for SQL Server CE?

查看:180
本文介绍了如何使用动态连接字符串为SQL Server CE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的SQL Server CE 4.0在我的Windows应用程序,并使用实体框架来创建它的一个典范。

I used SQL Server CE 4.0 in my windows app and use Entity Framework to create a model of it.

它工作正常,但我的问题是它没有按'吨有一个构造函数来更改连接字符串,默认情况下它读取从的app.config 文件的连接字符串。

It works fine but my problems is that it doesn't have a constructor to change the connection string, and by default it reads the connection string from the app.config file.

 using (var Context = new MyEntitiesModel(//has no constructor))
 {
        ...
 }

我创建一个动态连接字符串以及

I create a dynamic connection string and

  using (var Context = new MyEntitiesModel())
   {
         Context.Database.Connection.ConnectionString = entityConnection.ConnectionString;
   }



它正常工作,通过这样的方式,但如果我删除其它连接字符串中的的app.config 文件,它给了我这个。

It works fine by this way but if I remove another connection string in app.config file it gave me this.

错误=无效metasource。 ...

由于默认的构造函数使用它

because the default constructor uses it

我该如何处理呢?

推荐答案

创建您自己的构造函数。 MyEntitiesModel 在部分类,你可以添加类的自己部分的一部分,并添加构造函数接受一个连接字符串。

Create your own constructor. MyEntitiesModel is partial class you can add your own partial part of the class and add constructor accepting a connection string.

public partial class MyEntitiesModel {
    public MyEntitiesModel(string connectionString) : base(connectionString) { }
}

这篇关于如何使用动态连接字符串为SQL Server CE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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