Cannot get collection on entity class
Suppose I have the following tables with correct relationships:
Employee(empID, ...) Address(AddresID, ...) EmployeeAddress(EmpID, AddressID, ...)
Then changed the generated code for GetEmployee via .NET RIA services, like so:
public IQueryable<Employee> GetEmployee()
{
var Employee = this.Context.Employee.Include("EmployeeAddress").Include("Address");
return Employee;
}
An [Include] attribute has been added for EmployeeAddress in Employee and Address in EmployeeAddress.
When running the silverlight client side code with the following code:
EntityQuery<Employee> query = from e in ctx.GetEmployeeQuery()
select e;
I have nothing. If I remove the include from GetEmployee like:
public IQueryable<Employee> GetEmployee()
{
var Employee = this.Context.Employee;
return Employee;
}
It works great.
For a search member in Employee like Gender
public IQueryable<Employee> GetEmployee()
{
var Employee = this.Context.Employee.Include("GenderLookup");
return Employee;
}
It works great. Here Employee.Gender is the only object. Is it because Employee.EmployeeAddress is a collection and not a single object?
I cannot understand the reason. How to solve it?
You must use IncludeAttribute on the property representing the collection in the buddy metadata class. Most examples of RIA services demonstrate this.
This is eager loading, not lazy loading. I assume you meant, as lazy loading from a distributed server is usually not a good idea.
Are you using RIA services to get data from your server to your client? If so, you will need to use the metadata and the [Association] attribute for RIA to recognize the association.
[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 above example assumes that both your Employee class and your Address class have an EmployeeId property that RIA Services can use to create an association.
Additional Information
- MSDN Documentation
- Brad Abrams blog post
I'm pretty sure you cannot edit the GetEmployee () method. Rather, you should create a new method: 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 creating associations in the model viewer or in the DB itself.