使用oData和EF时设置数据库名称

发布于 2024-11-25 06:26:50 字数 718 浏览 3 评论 0原文

我有一个场景,其中有多个具有相同架构的数据库,并且客户端可以选择要查询的数据库。从 silverlight 执行 oData 查询时是否可以包含数据库名称,以便可以重用相同的服务?

假设我在客户端(silverlight/wp7)执行此查询(见下文),如何让此查询针对用户首次启动应用程序时选择的数据库运行?

私有 DataServiceCollection _employees;
私有无效LoadEmployees()
{
DataModelContainer dmc = new DataModelContainer(new Uri("http://localhost:63832/DataService.svc"));
var query = (来自 dmc.Employees
中的 e 其中 e.StartDate == BaseDate 选择 e);
_employees = new DataServiceCollection(dmc);
_employees.LoadCompleted += new EventHandler(_employees_LoadCompleted);
_employees.LoadAsync(查询);
}

I have a scenario where there are multiple dbs that have the same schema and clients can pick which database to query. Is there a way to include the database name when doing an oData query from silverlight so it can reuse the same services?

Lets say I have this query (see below) being executed at the client (silverlight/wp7), how do I get this query to run against the database that the user picked when they first launched the app?

private DataServiceCollection _employees;
private void LoadEmployees()
{
DataModelContainer dmc = new DataModelContainer(new Uri("http://localhost:63832/DataService.svc"));
var query = (from e in dmc.Employees
where e.StartDate == BaseDate
select e);
_employees = new DataServiceCollection(dmc);
_employees.LoadCompleted += new EventHandler(_employees_LoadCompleted);
_employees.LoadAsync(query);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-12-02 06:26:50

您应该通过 connectionstrings 元素在配置文件中执行此操作。

例如:

<configuration>
<!-- Other configuration settings -->

<connectionStrings>

  <add name="Sales" 
       providerName="System.Data.SqlClient"
       connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />

  <add name="NorthWind" 
       providerName="System.Data.SqlClient" 
       connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />

</connectionStrings>

要根据来自客户端的查询字符串动态检索连接字符串,请使用 ConfigurationManager 类。以下链接将在这方面为您提供帮助:

http://msdn .microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

www.dotnet-guide.com/configurationmanager-class.html

You should be doing that in your configuration files, via connectionstrings element.

eg:

<configuration>
<!-- Other configuration settings -->

<connectionStrings>

  <add name="Sales" 
       providerName="System.Data.SqlClient"
       connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />

  <add name="NorthWind" 
       providerName="System.Data.SqlClient" 
       connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />

</connectionStrings>

To retrieve the connection strings dynamically, based on your query string from the client, make use of the ConfigurationManager class. The following links will help you in this regard:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

www.dotnet-guide.com/configurationmanager-class.html

黯然#的苍凉 2024-12-02 06:26:50

这是我的想法:AddQueryOption 将向查询字符串添加一个值,在我的例子中是我想要运行查询的数据库的键。

var query =(来自 dmc.Employees.AddQueryOption("dbKey", SelectedDB) 中的 e)
其中 e.StartDate == BaseDate 选择 e);
_employees = new DataServiceCollection(dmc);
_employees.LoadCompleted += new EventHandler(_employees_LoadCompleted);
_employees.LoadAsync(查询);
在DataService

类中,我重写 CreateDataSource 方法并返回带有查询的正确连接字符串的 DataModelContainer
protected 重写 DataModelContainer CreateDataSource()
{
DataModelContainer dmc = new DataModelContainer(GetConnectionString());
返回dmc;
}
私有字符串 GetConnectionString()
{
字符串 dbKey = HttpContext.Current.Request.Params["dbKey"].ToString();
字符串 cnnKey = "DataModelContainer" + dbKey;
返回 ConfigurationManager.ConnectionStrings[cnnKey].ToString();
}

Here is what I came up with: The AddQueryOption will add a value to the query string, in my case the key of the db that I want to run the query against.

var query = (from e in dmc.Employees.AddQueryOption("dbKey", SelectedDB)
where e.StartDate == BaseDate select e);
_employees = new DataServiceCollection(dmc);
_employees.LoadCompleted += new EventHandler(_employees_LoadCompleted);
_employees.LoadAsync(query);
}

In the DataService class I overrride the CreateDataSource method and return a DataModelContainer with the correct connection string for the query
protected override DataModelContainer CreateDataSource()
{
DataModelContainer dmc = new DataModelContainer(GetConnectionString());
return dmc;
}
private string GetConnectionString()
{
string dbKey = HttpContext.Current.Request.Params["dbKey"].ToString();
string cnnKey = "DataModelContainer" + dbKey;
return ConfigurationManager.ConnectionStrings[cnnKey].ToString();
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文