WCF 数据服务的动态实体模型
我想使用 WCF 数据服务将 SQL 数据库的内容公开为 OData 源。
只要 SQL 数据库模式不变,一切都会正常进行。一旦添加数据库表或更改,实体模型就会过时。重新编译数据服务不是一个选项,因为架构每天可能会更改多次。
我正在定义一个表数据服务:
public class TablesDataService
{
private static List<Table> _tables;
static TablesDataService()
{
_tables = new List<Table>();
// query the database and add a table entity model for each table
}
public IQueryable<Table> Tables
{
get { return _tables.AsQueryable<Table>(); }
}
}
它使用以下 POCO 来表示单个表:
[DataServiceKey("Name")]
public class Table
{
public Table(string name)
{
Name = name;
}
public string Name { get; private set; }
}
WCF 数据服务用于 WcfDataService.svc 中的以下类:
public class WcfDataService : DataService<TablesDataService>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("Tables", EntitySetRights.All);
}
}
因为 SQL 数据库中的每个表都有一组不同的列,所以我正在寻找一种动态向 Table 类添加属性的方法,以便它可以表示查询时存在的数据库表的形状。
例如,假设数据库包含一个名为 Population 的表,我希望能够支持以下 OData 查询:
http://localhost/WcfDataService.svc/Tables('Population')?$filter=Code eq 'CA'
其中 Code 是包含美国州代码的 char(2) 列。
到目前为止,任何使用 ExpandoObject(而不是 Table 类)或从 DynamicObject 派生 Table 类的尝试都无法创建可用的 OData feed,从而导致以下“请求错误”:
异常消息是“内部服务器错误”。不支持“ServiceLibrary.Table”类型。
堆栈跟踪显示内部抛出的异常
System.Data.Services.Providers.ReflectionServiceProvider.BuildHierarchyForEntityType
有没有办法创建一个动态公开属性(表示相应数据库表的列)的 Table 类,WCF 数据服务可以使用该类来浏览 SQL 数据库?
I want to expose the contents of a SQL database as an OData feed using WCF Data Service.
Everything works well as long as the SQL database schema is unchanged. Once a database table is added or altered the Entity model is out of date. Recompiling the data service is not an option as the schema can change several times a day.
I am defining a table data service:
public class TablesDataService
{
private static List<Table> _tables;
static TablesDataService()
{
_tables = new List<Table>();
// query the database and add a table entity model for each table
}
public IQueryable<Table> Tables
{
get { return _tables.AsQueryable<Table>(); }
}
}
which uses the following POCO to represent an individual table:
[DataServiceKey("Name")]
public class Table
{
public Table(string name)
{
Name = name;
}
public string Name { get; private set; }
}
The WCF Data Service uses for following class in WcfDataService.svc:
public class WcfDataService : DataService<TablesDataService>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("Tables", EntitySetRights.All);
}
}
Because each table in the SQL database has a different set of columns, I am looking for a way to add properties to the Table class dynamically so it can represent the shape of the database table as it exists at the time of the query.
For example, assuming the database contains a table called Population, I'd like to be able to support the following OData query:
http://localhost/WcfDataService.svc/Tables('Population')?$filter=Code eq 'CA'
where Code is a char(2) column containing the US state codes.
So far, any attempts to use either an ExpandoObject (instead of the Table class) or having the Table class derive from DynamicObject have failed to create a workable OData feed, resulting the following 'Request Error':
The exception message is 'Internal Server Error. The type 'ServiceLibrary.Table' is not supported.'
with the stack trace showing the exception being thrown inside
System.Data.Services.Providers.ReflectionServiceProvider.BuildHierarchyForEntityType
Is there a way to create a Table class that exposes properties (representing the columns of the corresponding database table) dynamically that can be used by a WCF Data Service to browse a SQL database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OData Provider Toolkit 包含 R/ 的示例实现W 非类型化数据提供程序,可以轻松修改以返回元数据和表数据。
The OData Provider Toolkit contains a sample implementation of a R/W Untyped Data Provider which can easily be modified to return metadata and table data.