编程语言
首页 > 编程语言> > c#-WCF数据服务的动态实体模型

c#-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类添加属性的方法,以便它可以表示查询时所存在的数据库表的形状.

例如,假设数据库包含一个名为“人口”的表,我希望能够支持以下OData查询:

http://localhost/WcfDataService.svc/Tables('Population')?$filter=Code eq 'CA'

其中Code是包含美国州代码的char(2)列.

到目前为止,任何尝试使用ExpandoObject(而不是Table类)或让Table类从DynamicObject派生的尝试都未能创建可行的OData feed,从而导致以下“请求错误”:

The exception message is ‘Internal Server Error. The type ‘ServiceLibrary.Table’ is not supported.’

堆栈跟踪显示抛出异常

System.Data.Services.Providers.ReflectionServiceProvider.BuildHierarchyForEntityType

有没有一种创建Table类的方法,该类动态地公开属性(代表相应数据库表的列),WCF数据服务可以使用该类来浏览SQL数据库?

解决方法:

OData Provider Toolkit包含R / W无类型数据提供程序的示例实现,可以轻松对其进行修改以返回元数据和表数据.

标签:dynamic,odata,wcf,c,wcf-data-services
来源: https://codeday.me/bug/20191208/2092114.html