Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的OData联想#_C#_Wcf Data Services_Odata - Fatal编程技术网

C# C语言中的OData联想#

C# C语言中的OData联想#,c#,wcf-data-services,odata,C#,Wcf Data Services,Odata,我在OData.org上读到关于OData资源的文章时,偶然发现了这个关于关联的部分 关联定义两个或多个实体类型之间的关系 (例如,部门的员工工作表)。社团实例 在关联集中分组。导航属性是特殊的 绑定到特定关联的实体类型的属性 和可用于引用实体的关联 最后,所有实例容器(实体集和关联集) 在实体容器中分组 将上述段落转换为OData术语,则 OData服务由实体集或导航属性表示 在标识实体集合的实体类型上。对于 例如,URI标识的实体集 还是收藏 中“产品”导航属性标识的实体的名称 标识ODat

我在OData.org上读到关于OData资源的文章时,偶然发现了这个关于关联的部分

关联定义两个或多个实体类型之间的关系 (例如,部门的员工工作表)。社团实例 在关联集中分组。导航属性是特殊的 绑定到特定关联的实体类型的属性 和可用于引用实体的关联

最后,所有实例容器(实体集和关联集) 在实体容器中分组

将上述段落转换为OData术语,则 OData服务由实体集或导航属性表示 在标识实体集合的实体类型上。对于 例如,URI标识的实体集 还是收藏 中“产品”导航属性标识的实体的名称 标识OData服务公开的条目提要

我正在使用Visual Studio 2012制作C#中的OData服务,并希望使用前面提到的URL功能。然而,我不知道如何设置它。有人知道怎么做吗

这是我的密码:

    public class AssociationTest : DataService<ServiceContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
}
public class ServiceContext
{
    CategoryList _categories = new CategoryList();
    public IQueryable<Category> CategorySet
    {
        get{return _categories.AsQueryable();}
    }
    ProductList _products = new ProductList();
    public IQueryable<Product> ProductSet
    {
        get{return _products.AsQueryable();}
    }

}
[System.Data.Services.Common.DataServiceKeyAttribute("ID")]
public class Category
{
    public string Type
    {
        get;
        set;
    }
    public int ID
    {
        get;
        set;
    }
}
public class CategoryList : List<Category>
{
    public CategoryList()
        : base()
    {
        Add(new Category { Type = "Hardware", ID = 0 });
        Add(new Category { Type = "Software", ID = 1 });
    }
}
[System.Data.Services.Common.DataServiceKeyAttribute("ID")]
public class Product
{
    public string Name
    {
        get;
        set;
    }
    public int ID
    {
        get;
        set;
    }
    public int CategoryID
    {
        get;
        set;
    }
}
public class ProductList:List<Product>
{
    public ProductList()
        : base()
    {
        Add(new Product { Name = "Computer", ID = 0, CategoryID = 0 });
        Add(new Product { Name = "Phone", ID = 1, CategoryID = 0 });
        Add(new Product { Name = "Outlook", ID = 2, CategoryID = 1 });
        Add(new Product { Name = "Excel", ID = 3, CategoryID = 1 });
    }
}
公共类AssociationTest:DataService
{
//此方法只调用一次以初始化服务范围策略。
公共静态void InitializeService(DataServiceConfiguration配置)
{
config.SetEntitySetAccessRule(“*”,EntitySetRights.AllRead);
config.SetServiceOperationAccessRule(“*”,ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion=DataServiceProtocolVersion.V3;
}
}
公共类ServiceContext
{
CategoryList_categories=新的CategoryList();
公共可查询类别集
{
获取{return _categories.AsQueryable();}
}
ProductList _products=新产品列表();
公共可查询产品集
{
获取{return _products.AsQueryable();}
}
}
[System.Data.Services.Common.DataServiceKeyAttribute(“ID”)]
公共类类别
{
公共字符串类型
{
得到;
设置
}
公共整数ID
{
得到;
设置
}
}
公共类类别列表:列表
{
公共类别列表()
:base()
{
添加(新类别{Type=“Hardware”,ID=0});
添加(新类别{Type=“Software”,ID=1});
}
}
[System.Data.Services.Common.DataServiceKeyAttribute(“ID”)]
公共类产品
{
公共字符串名
{
得到;
设置
}
公共整数ID
{
得到;
设置
}
公共int类别ID
{
得到;
设置
}
}
公共类产品列表:列表
{
公共产品列表()
:base()
{
添加(新产品{Name=“Computer”,ID=0,CategoryID=0});
添加(新产品{Name=“Phone”,ID=1,CategoryID=0});
添加(新产品{Name=“Outlook”,ID=2,CategoryID=1});
添加(新产品{Name=“Excel”,ID=3,类别ID=1});
}
}

您可以让服务模型直接指向产品的类别,而不是使用外键来建模产品->类别之间的关系。我的意思是,与此相反:

public class Product
{
    // ...
    public int CategoryID
    {
        get;
        set;
    }
}
您可以这样建模:

public class Product
{
    // ...
    public Category ProductCategory
    {
        get;
        set;
    }
}

如果以这种方式设置模型,WCF数据服务反射提供程序应自动在产品上创建导航属性以及模型中的必要关联。

而不是使用外键建模产品->类别之间的关系,您可以让您的服务模型直接指向产品的类别。我的意思是,与此相反:

public class Product
{
    // ...
    public int CategoryID
    {
        get;
        set;
    }
}
您可以这样建模:

public class Product
{
    // ...
    public Category ProductCategory
    {
        get;
        set;
    }
}

如果您以这种方式设置模型,WCF数据服务反射提供程序应自动在产品上创建导航属性以及模型中的必要关联。

可以在以下位置找到该段落:您是在使用WCF数据服务、ASP.NET Web API还是手工制作OData服务器?如果我正确理解您的问题,那么我想你想要的东西就可以开箱即用了。例如,如果您使用Entity Framework作为提供程序,那么遵循《快速入门指南》应该会生成一个服务,该服务会生成带有导航链接的有效负载,就像您复制的文本中的链接一样。还是我误解了?这是相同的想法,只是我不想将我的数据服务建立在实体数据模型的基础上。我试图将它建立在一个包含IQueryable列表的类的基础上。我将包括我的一些代码。该段落可在以下网址找到:您是在使用WCF数据服务、ASP.NET Web API,还是在手工制作OData服务器?如果我正确理解您的问题,那么我认为您想要的内容将一目了然。例如,如果您使用Entity Framework作为提供程序,那么遵循《快速入门指南》应该会生成一个服务,该服务会生成带有导航链接的有效负载,就像您复制的文本中的链接一样。还是我误解了?这是相同的想法,只是我不想将我的数据服务建立在实体数据模型的基础上。我试图将它建立在一个包含IQueryable列表的类的基础上。我将包含一些代码。