views:

610

answers:

3

I created my dbml file with the tables that i need. One of the method generated is as such, names have been changed and it is not syntactically correct. But my code compiles with the original method created.

[Association(Name="Clients_ToEmployee", Storage="_ToEmployees", ThisKey="ClientID", OtherKey="ClientID")]
[DataMember(Order=70, EmitDefaultValue=false)]
public EntitySet<ClientToEmployee> ClientToEmployees
{
    get
    {
        if ((this.serializing &&(this._ToEmployees.HasLoadedOrAssignedValues == false)))
        {
            return null;
        }
        return this._ToEmployees;
    }
    set
    {
        this._ToEmployees.Assign(value);
    }
}

When i try to serialize my resultset using

 DataContractSerializer ser =
                new DataContractSerializer(clientObj.GetType());
var memStream = new System.IO.MemoryStream();
ser.WriteObject(memStream ,clientObj);
memStream .Seek(0, System.IO.SeekOrigin.Begin);
var streamReader = new System.IO.StreamReader(memStream );
var xml = streamReader.ReadToEnd();

It looks like at this code, see below, it always returns null

if ((this.serializing && (this._ToEmployees.HasLoadedOrAssignedValues == false)))

If i set a break point and step through the code then the above statement

this._ToEmployees.HasLoadedOrAssignedValues

is true and I get my object rather then a null being returned.

Why does it behave correctly when I am stepping through the code? Should I introduce a delay so that the Entityset object is populated?

A: 

You'd have to include more of the code for us to be certain, but consider the possibility that it's telling you the truth. Maybe there are none. Maybe you are stepping through a different code path than is executing outside the debugger for some reason. We can't tell because you didn't post the code.

I recommend you simplify the scenario and try again.

I also recommend against returning LINQ to SQL or LINQ to Entities objects from a web service. Microsoft has unfortunately chosen to include implementation-dependent data in the base classes, so they don't serialize cleanly.

John Saunders
+1  A: 

I had the same problem in my WCF service. In my case it was the fact that while debugging, Linq had enough time to lazy load the child records. When not in debug mode, it was just returning the parent records. Most of my methods were fine, but one had a lot of parent records with a lot of child records and none of the child records made it. So I put this code in my WCF method:

DataLoadOptions dlOptions = new DataLoadOptions();
dlOptions.LoadWith<ParentTable>(Parent => Parent.Child);
dc.LoadOptions = dlOptions;
TheCodeMonk
A: 
DataLoadOptions dlOptions = new DataLoadOptions();
dlOptions.LoadWith<ParentTable>(Parent => Parent.Child);
dc.LoadOptions = dlOptions;

It works.

I really expend a lot of time to find the solution...!!