8 March, 2010
5 Comments
1 category
If you have a table with related tables then you can get the child entries by editing the DomainService and the metadata:
Add the include attribute ([Include()]) to the property containing the child table reference and use the DataLoadOptions to specify the child tables to include in the query result view.
In this case a Task table contains a reference to a Project table and the Project table contains a reference to the Customer table:
The domainservice class TTSDomainService
// TODO: Consider // 1. Adding parameters to this method and constraining returned results, and/or // 2. Adding query methods taking different parameters. public IQueryable<Task> GetTasks() { // Define the child entries to get DataLoadOptions options = new DataLoadOptions(); options.LoadWith<Task>(t => t.Project); options.LoadWith<Project>(p => p.Customer); this.DataContext.LoadOptions = options; // Get the tasks ordered by customer, project, task return from t in this.DataContext.Tasks orderby t.Project.Customer.Name, t.Project.Name, t.Name select t; }
The domainservice meta data class: TTSDomainService.metadata.cs
// The MetadataTypeAttribute identifies TaskMetadata as the class // that carries additional metadata for the Task class. [MetadataTypeAttribute(typeof(Task.TaskMetadata))] public partial class Task { // This class allows you to attach custom attributes to properties // of the Task class. // // For example, the following marks the Xyz property as a // required field and specifies the format for valid values: // [Required] // [RegularExpression("[A-Z][A-Za-z0-9]*")] // [StringLength(32)] // public string Xyz; internal sealed class TaskMetadata { // Metadata classes are not meant to be instantiated. private TaskMetadata() { } public Nullable<int> CategoryAbbrID; public int CategoryID; public Nullable<decimal> EstDuration; public Nullable<int> HourType; public string Name; [Include()] public Project Project; public int ProjectID; public EntitySet<TaskTimeSpan> TaskTimeSpans; } }
See:
Category: Uncategorized
This is great, thanks, but how do you update data in the child tables?
Basically, with your method, I am able to load realted/child data to show in a grid or so, but how can I now make updates into those child tables?
Thanks
–tolga
Great! I was banging my head on the wall because all samples on the net use .Include()… Thanks!
How do u define the DataContext because in my DomainService Class I only have the ObjectContext which does not have any LoadOptions property.
I’m using SL4 and EntityFramework
So how do you implement the same thing in SL4?
Having trouble getting the include attribute to work in vb…