Skip to main content

Microsoft Silverlight

Unanswered Question WCF & LINQ To SQL problemRSS Feed

(0)

dlusignan
dlusignan

Member

Member

3 points

15 Posts

WCF & LINQ To SQL problem

Hi! When I retreive data, on the server side everything looks normal. All my childrens are in my parent object. When the data arrives on the client side, all my children, of the parent object, are null. Exemple: SERVER-SIDE [parent].[childrens] = List[object]; CLIENT-SIDE [parent].[childrens] = null; Anyone who know why?

Daniel Lusignan

amyo
amyo

Contributor

Contributor

3630 points

495 Posts

Re: WCF & LINQ To SQL problem

When you use LINQ object to transfer via WCF,

Remember about the differed query execution.

Example: Suppose you are selecting Order and there is an OrderDetail list property.

By default if you select Order object it does not load the list of OrderDetail until you refer that.

Solution:

Change the loading option of your LINQ data context.

You can do it like below:

        [OperationContract]
        public Order GetOrder(int orderId)
        {
            var db = new DBDataContext();
           
            var orderloadOption = new DataLoadOptions();
            orderloadOption.LoadWith(o => o.OrderDetails);
            db.LoadOptions = orderloadOption;


            return db.Orders.Where(order=>order.OrderID==orderId).SingleOrDefault();
        }

 

Hope this will help you.

Please Mark as Answer if this helps you.

Amyo Kabir
Solution Architect & Sr. Developer
Blog

dlusignan
dlusignan

Member

Member

3 points

15 Posts

Re: WCF & LINQ To SQL problem

Hi! thanks for the answer. Your exemple is perfect for me because at this point of operation, I have all my data. In the return object, all the informations are there. It's only when it arrives on the client-side there's no childs entities.

Daniel Lusignan

amyo
amyo

Contributor

Contributor

3630 points

495 Posts

Re: WCF & LINQ To SQL problem

If you are using LINQ to SQL entity object to transfer via WCF.

Please check my solution (change default DataLoadOptions. before executing)

Amyo Kabir
Solution Architect & Sr. Developer
Blog

radarjr
radarjr

Member

Member

6 points

3 Posts

Re: WCF & LINQ To SQL problem

I have a very similar issue to that posted above.  I also have a parent-child association using WCF and Linq to SQL.  When I query the database on the server side, in debug mode I can see the child table public members with a public parent member (entity ref I assume), and it's associated public members beneath it.  I need the members from both child and parent, and all that's returned to the client is child members.  I have added the solution you suggested, but there's still no parent data on client-side.  I'm not sure if your solution is what I need or if the one here is

http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_23781859.html ; however, the one on the link along with the expert comment following it seems more like what I'm trying to get.  I need to select all the data from the child table, and one item (description) from the parent table which corresponds to the child row (like childID = parentID).  Can you help me get oriented.

 

CGPortalData cg = GetDB();
            cg.DeferredLoadingEnabled = false;

            var dbContext = new System.Data.Linq.DataContext("");
            var orderLoadOptions = new System.Data.Linq.DataLoadOptions();
            orderLoadOptions.LoadWith(p => p.Description);
            dbContext.LoadOptions = orderLoadOptions;


            var points = from p in cg.pois
                         where p.CategoryID == cat
                         from pts in p.points
                         where pts.ObjectID == p.ID
                         select pts;

            point[] rv = points.ToArray();

            return rv;
   In the above, I need all the pts members and pts.poi.description.  I hope this makes sense, because I'm confused.

 

amyo
amyo

Contributor

Contributor

3630 points

495 Posts

Re: WCF & LINQ To SQL problem

radarjr:
var dbContext = new System.Data.Linq.DataContext(""); var orderLoadOptions = new System.Data.Linq.DataLoadOptions(); orderLoadOptions.LoadWith(p => p.Description); dbContext.LoadOptions = orderLoadOptions;

This is ok.

Now you have to use dbContext DataContext for your query.

Now it will look like:

            var points = from p in dbContext.pois
                         where p.CategoryID == ..

 

Amyo Kabir
Solution Architect & Sr. Developer
Blog

radarjr
radarjr

Member

Member

6 points

3 Posts

Re: WCF & LINQ To SQL problem

Thank you for your assistance. 

Okay.  Right now, dbContext does not connect to my DB, cg does.  And here comes the issue.  CG is of CGPortal class that implements devart.data.linq.datacontext; so, what I thought I could do is:   cg.loadoptions   but loadoptions doesn't exist for cg.  Don't I inherently have access to members of implemented classes?  If I directly say:  var dbContex = new Devart.Data.Linq.DataContext(""), I have access dbContext.LoadOptions, but dbContext is not connected to my DB; therefore can't have dbContext.pois

 So, I tried:

Devart.Data.Linq.DataContext dbContext = new CGPortal();   //No table access or loadOptions

...which accepts a null connection string and still connects.  I thought that would give me access to my tables and LoadOptions, but that gives me neither.  If I tried:

System.Data.Linq.DataContext dbContext = new CGPortal();   //LoadOptions, but no table access

...provides LoadOptions, but no table access.  I think if I could somehow access members of the implemented devart.data.linq.datacontext this would all be behind me.  Am I missing something?

 Thank you again

radarjr
radarjr

Member

Member

6 points

3 Posts

Re: Re: WCF & LINQ To SQL problem

Nevermind, I just found that Devart dropped support for LoadOptions.  Back to using MS.  Thanks for your help though.

Hiisu
Hiisu

Member

Member

2 points

1 Posts

Re: WCF & LINQ To SQL problem

Hi all,

Daniel, were you able to resolve your issue?  I believe that I am in the same situation and have followed the prescribed steps. My application models an expense reporting system and is set up as follows:

  • LINQ to SQL classes are used to generate User, ExpenseReport, and ExpenseItem classes.  Relationships have been defined such that a User has an ExpenseReport EntitySet named MyReports.  Every ExpenseReport also has an ExpenseItems EntitySet that provides line item details.  It is desirable to retrieve all of the ExpenseItems at the same time that the ExpenseReports are retrieved.
  • A WCF service exposes a ServiceContract for retrieving a list of ExpenseReport objects.
  • DeferredLoadingEnabled = false and DataLoadOptions are used to pre-fetch the ExpenseItems while retrieving a Customer's ExpenseReports using eager loading.
  • The DBML Serialization Mode is configured for Unidirectional.
  • All LINQ to SQL types generated in ExpenseDB.designer.cs are properly decorated with either the [DataContract] or [DataMember] attribute by the designer.

The operation contract looks like:

[OperationContract]
List<ExpenseReport> GetExpenseReports(int userID);

It seems that all of the desired data is fetched with the single query, but by the time data has been returned through the WCF OperationContract, the pre-fetched data in ExpenseItems is lost.  Here's what happens on the server:

  List<ExpenseReport> GetReports(int userID)
        {
            List<ExpenseReport> result = null;

            using (ExpenseDBDataContext dc = new ExpenseDBDataContext())
            {
                dc.DeferredLoadingEnabled = false;

                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ExpenseReport>(report => report.ExpenseItems);
                dc.LoadOptions = dlo;

                User user = dc.Users.Where(x => x.UserID == userID).Single();

                //result contains the expected ExpenseReport items, and result[0].ExpenseItems contains two fully populated items.
                result = user.MyReports.ToList();
            }

            return result;
        }

The caller on the client receives the result here:

   CachedReports = server.GetExpenseReports(userID);

But CachedReports[0].ExpenseItems is now null.  Do I have something configured incorrectly with the LINQ to SQL designer, The WCF service contract, or the client that would result in only the ExpenseReport items being serialized?  Is there some disconnect between the ORM and the WCF OperationContract that does not preserve data beyond the parent class ExpenseReport despite full serialization being configured?  Do I need to explicitly retrieve the ExpenseItem data, and isn't this what LoadWith() is doing?

Thanks in advance for the much appreciated feedback,

Heath

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities