views:

252

answers:

3

Suppose I have following tables with the right relationships built:

Employee(empID, ...)
Address(AddresID, ...)
EmployeeAddress(EmpID, AddressID, ...)

Then modified the generated code for GetEmployee by .NET RIA Services like:

public IQueryable<Employee> GetEmployee()
{           
 var Employee = this.Context.Employee.Include("EmployeeAddress").Include("Address");
 return Employee;
}

Attribute [Include] has been added for EmployeeAddress in Employee and Address in EmployeeAddress.

When running the code at silverlight client side with following code:

EntityQuery<Employee> query = from e in ctx.GetEmployeeQuery()
                              select e;

I got nothing. If I remove the include from GetEmployee, like:

public IQueryable<Employee> GetEmployee()
{           
 var Employee = this.Context.Employee;
 return Employee;
}

It works fine.

For lookup member in Employee, like Gender

 public IQueryable<Employee> GetEmployee()
{           
 var Employee = this.Context.Employee.Include("GenderLookup");
 return Employee;
}

It works fine. Here Employee.Gender is a single object. Is it because Employee.EmployeeAddress is a collection, not a single object?

Can't figure out the reason. How to resolve it?

A: 

You have to use the IncludeAttribute on the property representing the collection on the "buddy" metadata class. Most RIA services examples demonstrate this.

This is eager loading, not lazy loading. I'm guessing that's what you meant, since lazy loading from a distributed server is not generally a good idea.

Craig Stuntz
Thanks for you correction. I have [Include] for all buddy metadata. Will try it again.
KentZhou
Still not working.
KentZhou
A: 

Are you using RIA services to get data from your server to your client? If so, then you'll need to use meta-data and the [Association] attribute so that RIA Services recognizes the relationship.

[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
    public int EmployeeId {get; set; }
    public EmployeeAddress Address {get; set; }
}

public partial class EmployeeAddress
{
    public int EmployeeId {get; set; }
}

public class EmployeeMetaData
{
    [Include]
    [Association("EmployeeAddress", "EmployeeId", "EmployeeId")]
    public EmployeeAddress Address {get; set;}
}

The example above assumes that both your Employee class and your Address class have an "EmployeeId" property that RIA Services can use to create the association.

More information

Brad Tutterow
A: 

I am pretty sure that you cannont edit the GetEmployee() method. Rather you should create a new method called: GetEmployeeAddress()

public IQueryable<Employee> GetEmployeeAddress(string address)
{ 
    var Employee = this.Context.Employee.Where((x) => x.ID = address_ID)  
}

Something like that. Brad is also right but I would suggest making the associations in the model viewer or on the DB itself.

johnnywhoop