单个Wcf数据服务的多个数据上下文

编程入门 行业动态 更新时间:2024-10-27 13:29:22
本文介绍了单个Wcf数据服务的多个数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个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) generatesthe 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数据服务的多个数据上下文

本文发布于:2023-11-07 09:08:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   多个   上下文   Wcf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!