如何动态更改实体框架的连接字符串? [英] How to change connection string dynamically for entity framework?

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

问题描述

大家好,



在我的一个项目中,我们需要动态更改数据库连接。网站是使用asp.net mvc 4和实体框架构建的。目前我们通过添加新项来创建实体框架类,并仅从向导应用连接字符串。现在我们需要的是管理员可以更改服务器设置,并将其保存在根目录中的一个xml文件中。我们需要连接服务器细节并将新的连接字符串应用于实体框架类。



有人可以给我一个想法。

Hi all,

in one of my project we have a need to change database connection dynamically. website is build using asp.net mvc 4 and entity framework. currently we create entity framework class by adding it from add new item and apply the connection string from wizard only. now what we need is admin can change server settings and that will be saved in one of the xml file in the root directory. we need to concatenate the server details and apply a new connection string to entity framework class.

can someone please give me idea.

推荐答案

我不知道你是什么类型的问题你确实提供了有关你需求的足够信息。现在,您需要开始编码。这就是全部。



请参阅:连接字符串和配置文件 [ ^ ]。它可能对你有帮助。





I have no idea what kind of issue do you have... You did provide enough information about your needs. Now, you need to start coding. That's all.

Please see: Connection Strings and Configuration Files[^]. It might help you.


Quote:

问题是现在我的连接字符串没有修复,我们需要根据与Web配置文件分开的xml文件更改数据源初始目录。如何根据该xml文件更新Web配置文件连接字符串。

problem is that now my connection string is not fixed and we need to change datasource initial catalog based on xml file which is separate from Web config file. how to update web config file connection string based on that xml file.





我能想象的最简单的方法是:

1)创建 [ ^ ] DataTable [ ^ ] object

2)添加专栏 [< a href =http://msdn.microsoft.com/en-us/library/system.data.datatable.columns(v=vs.100).aspxtarget =_ blanktitle =New Window> ^ ]和 [ ^ ](数据)

3)保存DataTable数据到Xml文件 [ ^ ]

这就是全部! ;)



提示:要从xml获取数据,请使用 DataTable,ReadXml方法 [ ^ ]



那里还有很多其他方法可以实现,例如使用XML DOM文档,XMLWriter等。



[/ EDIT]



The simplest way i can imagine is:
1) create[^] DataTable[^] object
2) add columns[^] and rows[^] (data)
3) save DataTable data to Xml file[^]
That's all! ;)

Tip: to get data from xml, use DataTable,ReadXml method[^]

There is many other ways to achieve that, for example using XML DOM document, XMLWriter, etc.

[/EDIT]


这篇关于如何动态更改实体框架的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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