tags:

views:

35

answers:

2

I have a WCF Service which retreives data via Linq2SQL. I am getting all the child records, just not the related parent records

i.e Customer record has related Orders, and there is a status on the customer. I am getting the customer and the orders, but I cannot get the status record to get the status description

 using (MyDataContext context = new MyDataContext(CommonCode.DBConnectionString))
        {
            context.DeferredLoadingEnabled = false;
            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<Customer>(u => u.Orders);
            options.LoadWith<Customer>(u => u.CustomerStatus);

            context.LoadOptions = options; 
            context.ObjectTrackingEnabled = false; // retrieving data read only

            return context.Customers.SingleOrDefault<Customer>(
                cus=> cus.CustomerId == passedId );
        }

I have tried playing with the Deferred Loading and object tracking (which I do not need on) and are running out of ideas.

Can anyone help on how I can return the Customer Status record please.

A: 

This doesnt really answer the question (annoying I know!) but maybe its better to define custom objects in your data contracts and populate them manually for passing over the service. This way if you change your Linq2Sql model you wont break your WCF client.

Additionally, this isnt as long winded as it sounds. In Linq2SQL you can something like..

from c in context.Customers
where (...)
select new CustomerStatusRecord() { CustomerId = c.Id, Status = c.CustomerStatus.Status ... }

CustomerId and Status are the fields in your newly defined data contract.

This de-coupling method would be my preferred way to do it but I understand why for convenience its easier to pass the whole data context object over the service.

Last thought: If you in the future added 50 odd new fields to the customer table, using the way you have described would really increase the data transfer across your service. However if its decoupled you have absolute control over what goes over the wire. If you leave it as it is maybe you might end up passing sensitive data across.

RemotecUk
A: 

I found out what it was! Linq to SQL will put the [DataMember()] attribute on the Child Records, But NOT the Parents. I had to add the [DataMember()] attribute under the association .

The Down Side is this is a Manual Entry to the context.designer.cs which will get overwritten if the dbml is changed :-(

    [Association(Name="CustomerStatus_Customer", Storage="_CustomerStatus", ThisKey="CustomerStatusId", OtherKey="CustomerStatusId", IsForeignKey=true)]
    [DataMember()]
    public CustomerStatus CustomerStatus
    {
        get
        {
            return this._CustomerStatus.Entity;
        }
        set
        { 
          ...
         }
     }
Traci