[已解决]在实体数据模型上更改数据库服务器 [英] [SOLVED]Changing Database Server on Entity Data Model

查看:56
本文介绍了[已解决]在实体数据模型上更改数据库服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人们,我有一个问题:
场景:
我已经在VS 2010中使用C#开发了一个应用程序,并且我有一个指向特定服务器上的数据库的数据模型.一个特定的方案是我的Office和我的家:在Office中,服务器的名称是FS-OFFICE \ SQLSERVER,在我的家中的名称是SERVER \ SQLEXPRESS
两种情况下的数据库都是相同的.

我想做的是,根据软件的实现位置,能够以编程方式或使用配置文件来选择服务器,因为我无法更改服务器名称.
有可能吗?

提前谢谢.
最好的问候!

解决方案:感谢Henry Minute,更改app.config文件上的目标服务器对我来说非常有用.只需更改数据源"中的字符串即可.

Hi Folks, i have a question:
Scenario:
I have developed an application in VS 2010 with C# and i have a data model that points to a database on a specific server. An specific scenario is my Office and my Home: In the Office the Server''s name is FS-OFFICE\SQLSERVER and in my home is SERVER\SQLEXPRESS
The databases are the same in both cases.

What i want to do is, depending on the place of implementation of the software, be able of select the server programatically or with a config file instead, because i haven''t the possibility to change the servers names.
That is possible?

Thanks in advance.
Best Regards!

SOLUTION: Thanks to Henry Minute changing the target server on the app.config file works great for me. Just changing the string in "Data Source" works.

<connectionStrings>
<add name="FFPFrameWorkEntities" connectionString="metadata=res://*/Entities.FFP_FrameWork.csdl|res://*/Entities.FFP_FrameWork.ssdl|res://*/Entities.FFP_FrameWork.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=FS-OFFICE\sqlexpress;Initial Catalog=FFPFrameWork;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>



谢谢mod!我还学习了如何显示代码!



Thanks Mod! I also learned how to present the code!

推荐答案

在配置文件中可以有多个连接字符串:
You can have more than one connection string in the config file:
<connectionStrings>
  <add name="Home" connectionString="Data Source=SERVER\SQLEXPRESS;AttachDbFilename=D:\data\MyStuff.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

    providerName="System.Data.SqlClient" />
  <add name="Office" connectionString="Data Source=FS-OFFICE\SQLSERVER;AttachDbFilename=H:\data\MyStuff.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

    providerName="System.Data.SqlClient" />
</connectionStrings>



并访问以下内容:



and access it something like:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Home"].ConnectionString);






OR

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Office"].ConnectionString);



希望这可以帮助. :)



Hope this helps. :)


在使用Visual Studio构建实体数据模型时,连接字符串已存储在app.config中.浏览本文的代码,实体框架性能 [
When building and Entity Data Model with Visual Studio the connection string is already stored in the app.config. Browser the code for this article, Entity Framework Performance[^] and you will see the app.config file with the connection strings.

Also, one of the constructors for the DataContext class takes an IDbConnection, so you could create your own and pass it to the ctor.


这篇关于[已解决]在实体数据模型上更改数据库服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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