使用oData和EF时设置数据库名称
我有一个场景,其中有多个具有相同架构的数据库,并且客户端可以选择要查询的数据库。从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该通过 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:
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
这是我的想法: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();
}