Objects in middle tier that are not in DB RRS feed

  • Question

  • Hi,

    I am trying to create a custom entity that does not exist in my database. Previously I have been using the entity data model and domain service classes to shuttel stuff from my db to the UI, but now I want to use LINQ to query some tables in the entity data model and return a custom entity to the UI. I have tried manually creating an entity in the data model but then it complains that it is not mapped. Of course it's not mapped because I want to use LINQ in the domain service to populate it.

    In the domain service class, if I query a few entities and return some new entity how do I return that to the UI?


    Saturday, August 15, 2009 2:20 PM


All replies

  • Exposing IQueryable as a return value on your domain service query method works fine with EF or L2S, because they implement a LINQ-provider. When you want to use POCO's like this "virtual entity", you should return IEnumerable and use a query method which accepts parameters. This will give you full control on how to process the incoming query request.

    So in this scenario you will use a plain vanilla DomainService based domain service and you implement the database mapping manually. This sample uses POCO *and* IQueryable http://blogs.msdn.com/brada/archive/2009/07/22/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-6-poco-and-authentication-provider.aspx, but that's because the sample is simple and all the server side entities are available in a List. Well, in the real world this wouldn't be the case.....


    Saturday, August 15, 2009 2:38 PM
  • Hi,

    For Writing LINQ queries to EDM Or LINQ-To-SQL, You can generate your own custom class which will map the attributes of your return entity.This is a general case when you have output comming from EDM which is not mapped to any of the entity. (Anonymous Class)

    For example, I am doing a join between Three tables and accessing 2 columns each from all the three tables, then I can have a custom class which represents all the six properties mapping to my return entity output.



    Saturday, August 15, 2009 3:47 PM
  • Thanks for your replies. Can you post an example or two of the LINQ query you wrote to combine tables and return the combined object as the newly defined one? I am looking to use the "Context" variable in my LinqToEntitiesDomainService class.

    Saturday, August 15, 2009 6:03 PM
  • Hi,

    Check this out- The First link is really cool-





    "Please mark as answered, if this answers your question"

    Thursday, August 20, 2009 7:10 AM
  • Thanks, that was helpful. I still ran into some problems:

    1. LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[POSServer.Entities.tblInvoiceItem] DefaultIfEmpty[tblInvoiceItem](System.Collections.Generic.IEnumerable`1[POSServer.Entities.tblInvoiceItem])' method, and this method cannot be translated into a store expression.

    This seems to be from using the DefaultIfEmpty function to achieve outer joins. There is probably a workaround.

    2. The entity or complex type 'SystemSoupModel.vwInvoiceSummary' cannot be constructed in a LINQ to Entities query.

    There is a workaround, but just too many workarounds at this point. I've decided to just use views in the database until things settle down and there are less workarounds. Btw the workaround for #2 is:

    var anonproducts =

          from p in db.Products

          select new {Id = n.Id, Name = n.Name};

    var products =

          from p in anonproducts.AsEnumerable()

          select new Product {Id = p.Id, Name = p.Name};

    Friday, August 21, 2009 2:04 PM