如何使用ADO.NET实体数据模型的SQL连接字符串 [英] How to use a SQL connection string with ADO.NET Entity Data Model

查看:305
本文介绍了如何使用ADO.NET实体数据模型的SQL连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ADO.NET实体数据模型,我可以随时更改我指出的数据库。更改数据库可能需要一个全新的连接字符串。一些数据库在不同的服务器上。所以我需要能够传递我的ADO.NET实体数据模型一个自定义的连接字符串,如server = severaddress; database = database1; User ID = test; Password = test1234;



已编辑:我的实体类实现ObjectContext。我可以使用的三个构造函数是默认值,传入connectionString,传入一个EntityConnection。当我使用过载构造函数时,我收到错误,表示它不能在连接字符串中识别服务器。



我需要使用自定义连接字符串来实例化我的存储库,或者可以在使用前设置它。

解决方案

ObjectContext在其构造函数中接受实体连接字符串。实体连接字符串由三部分组成:




  • 元数据位置(由EDMX文件生成的XML的映射位置)

  • 数据存储提供者

  • 数据存储连接字符串(即您想要提供的)



你有几种方式来达到你想要的目的。一般来说,你需要的是连接字符串的两个部分:

  string format =metadata = res:// * / Model。 csdl | res://*/Model.ssdl | res://*/Model.msl; provider = System.Data.SqlClient; provider connection string = \{0} \; 
string connectionString =server = severaddress; database = database1; UserID = test; Password = test1234;

var context = ModelContext(String.Format(format,connectionString));

该格式描述了作为程序集和Sql提供程序中资源的Model.edmx元数据的位置。第二部分是您的连接字符串。



请注意,只有当您的所有数据库具有相同的模式并使用相同的提供者时,这才有效。


I am trying to use the ADO.NET Entity Data Model in a way that I can on the fly change which database I point too. Changing databases may require an entirely new connection string. Some databases are on different servers. So I need the ability to pass my ADO.NET Entity Data Model a custom connection string formated like so 'server=severaddress;database=database1;User ID=test;Password=test1234;'

Edited: My Entity Class implements ObjectContext. The three constructers I can use is the default, pass in connectionString, pass in an EntityConnection. When ever I use the overload constructers I get errors saying it doesn't recognize "server" in the connectionstring.

I need to either instantiate my repository with a custom connection string, or be able to set it before use.

解决方案

ObjectContext accepts entity connection string in its constructor. Entity connection string consists of three parts:

  • Metadata location (location of mapping XMLs produced by EDMX file)
  • Data storage provider
  • Data store connection string (that is what you want to provide)

You have several ways to achieve what you want. Generally what you need is combine two parts of connection string:

string format = "metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"";
string connectionString = "server=severaddress;database=database1;UserID=test;Password=test1234;"

var context = ModelContext(String.Format(format, connectionString));

The format describes location of metadata from Model.edmx included as resources in assembly and Sql provider. Second part is your connection string.

Be aware that this will only work if all your databases have same schema and use same provider.

这篇关于如何使用ADO.NET实体数据模型的SQL连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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