locked
Multiple Data context for a single Wcf data service RRS feed

  • Question

  • Hi,

    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.

    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?

    How should I go about so that I don't need to create a separate wcf data service for each Data source...

    Please help its urgent.

     

    Thanks

    Thursday, March 10, 2011 7:01 PM

All replies

  • 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;}
          
      }
    

    Also remember to call SetEntitySetAccessRule to enable access to this new entity set in the data service.

    You can find more information about using the reflection provider (which is basically how WCF Data Services exposes the DataContext from LINQ to SQL) in the topic Reflection Provider (WCF Data Services).

    Cheers,

    Glenn Gailey


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, March 11, 2011 5:20 AM