单个Wcf数据服务的多个数据上下文 [英] Multiple Data context for a single Wcf data service
问题描述
我创建了一个Wcf数据服务,其Datacontext是Linq to Sql类,直接映射到特定数据库。现在我想添加一个自定义类/集合(我在其中随机生成一些数字,而不是与db交互)
在现有的Linq to sql上下文中进行演示。
I have created a Wcf Data Service whose Datacontext is a Linq to Sql class, that is mapped directly to a particular database. Now I wanted to add a custom class /collection (where in I randomly generate some numbers, rather than interacting with db) inside the existing Linq to sql context to give a demo.
linqtosql上下文中的所有类都与DB表一起映射。我可以在现有的datacontextclass中添加自定义数据源类并为其编写Iqueryables吗?
All classes inside the linqtosql context are mapped with the DB tables. Can I add a custom data source class and write Iqueryables for it within the existing datacontextclass?
我应该如何进行以便我不需要为每个创建单独的wcf数据服务数据来源...
How should I go about so that I don't need to create a separate wcf data service for each Data source...
请帮助紧急。
谢谢
推荐答案
你应该能够做到这一点。 LINQ to SQL工具(O / R设计器)生成强类型DataContext类作为分部类。这使您可以扩展此上下文以添加其他IQueryable< TEntity>未映射到
数据库的属性。您必须确保新的IQueryable< TEntity>公开一个实体类型,您可以使用DataServiceEntity或DataServiceKey属性来表示,类似于以下完全组成的示例:
You should be able to do this. The LINQ to SQL tools (O/R Designer) generates the strongly-typed DataContext class as a partial class. This enables you to extend this context to add other IQueryable<TEntity> properties that are not mapped to the database. You must make sure that the new IQueryable<TEntity> exposes an entity type, which you can denote with the DataServiceEntity or DataServiceKey attribute, something like the following totally made up example:
public partial class MyTypedDataContext
{
public IQueryable<SomeType> MyTypes
{
get
{
List<SomeType> types = new List<SomeType>();
for (int i=0; i <2; i++)
{
types.Add(new SomeType(i, "string" + i.ToString()));
}
return types.AsQueryable<SomeType>();
}
}
}
[System.Data.Services.Common.DataServiceKey("Key")]
public class SomeType
{
public SomeType(int key, string prop)
{
this.Key = key;
this.Prop = prop;
}
public int Key {get; set;}
public string Prop { get; set;}
}
这篇关于单个Wcf数据服务的多个数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!